android canvas\paint\path簡單使用(自定義view必學)

許佳佳233發表於2016-04-01

最近學到自定義view,也是涉及到了canvas、paint、path的使用,此內容比較簡單。

寫此文章,一方面是筆者自己知識點的記錄。另一方面希望能給部分新手啟發。


此三者一般會在自定義view的onDraw()中用到:

canvas:決定view的佈局(位置,畫布顏色,形狀)

paint:決定view的屬性(顏色,字型大小,風格)

path:路徑(path的用法深入比較複雜,此處由於是入門,就不多加闡述混淆新手了)


大致繪製步驟:

首先定義paint的屬性,然後獲取到view的寬高(一般繪製會根據view的大小來適配),隨後根據獲取到的view的大小使用canvas來繪製出形狀。


效果:(程式碼在文章結尾)



MainAcitivity:

package com.example.double2.canvaspainttest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
}

MyView:

package com.example.double2.canvaspainttest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Double2號 on 2016/4/1.
 */
public class MyView extends View {


    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //把整張畫布繪製成白色
        canvas.drawColor(Color.WHITE);
        Paint paint = new Paint();
        //去鋸齒
        //設定paint的顏色
        //設定paint的風格
        //設定畫筆寬度
        paint.setAntiAlias(true);
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(4);
        //獲取到view的寬度
        int viewWidth=this.getWidth();
        int viewHight=this.getHeight();
        //繪製圓形
        //drawCircle(float cx, float cy, float radius, Paint paint)
        canvas.drawCircle(viewWidth / 10 + 60, viewHight / 10 + 10, viewWidth / 10 + 10, paint);
        //繪製矩形
        //drawRect(float left, float top, float right, float bottom, Paint paint)
        canvas.drawRect(100, viewHight / 5 + 20, viewWidth / 5 + 10, viewHight * 2 / 5 + 20, paint);

        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);
        //繪製圓角矩形
        //RectF(float left, float top, float right, float bottom)
        RectF rel=new RectF(10,viewHight/2 +40,viewWidth/5+10,viewHight*3/5+40);
        canvas.drawRoundRect(rel, 40, 10, paint);

        paint.setTextSize(48);
        //drawText(String text, float x, float y, Paint paint)
        canvas.drawText("xujiajia",viewWidth/2,viewHight/2,paint);

        //通過path繪製一個三角形
        Path path=new Path();
        path.moveTo(300, 400);
        path.lineTo(500, 100);
        path.lineTo(800, 200);
        path.close();
        canvas.drawPath(path, paint);



    }
}

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <com.example.double2.canvaspainttest.MyView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</RelativeLayout>


原始碼地址:http://download.csdn.net/detail/double2hao/9478649

相關文章