Android : 画布的使用 简单应用

示例图:

Android : 画布的使用 简单应用_第1张图片

MyView.java:

package com.example.demo;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

import com.example.myoomtest.R;

public class MyView extends View {

    public MyView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        //画笔
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        //设置画笔的风格,如 Style.FILL(实心)、
        // Style.FILL_AND_STROKE(同时显示实心和空心)或 Style.STROKE(空心)。
        paint.setStyle(Paint.Style.FILL);
        //设置空心 边框 宽度
        paint.setStrokeWidth(20.0f);

        //在画布上画条线
        /***
         * xxxxxx
         Y 000000
         Y 000000
         Y 000000
         * */
        canvas.drawLine(0,50,500,50,paint);//画一条直线
        canvas.drawLine(100,0,100,200,paint);//画一条竖线
        canvas.drawLine(300,0,600,200,paint);//画一条斜线

        Paint paint2 = new Paint();
        paint2.setStyle(Paint.Style.STROKE);//(空心)
        paint2.setStrokeWidth(10);//边框大小
        paint2.setARGB(100,120,255,100);
        /**
         * 画矩形 rect
         * */
        canvas.drawRect(100,300,300,500,paint2);


        Paint paint3 = new Paint();
        paint3.setStyle(Paint.Style.STROKE);//(空心)
        paint3.setStrokeWidth(10);//边框线大小
        paint3.setARGB(100,120,255,100);
        /**
         * x , y  位置
         * 圆的 半径
         * 画圆形 circle
         * */
        canvas.drawCircle(600f,600f,60f,paint3);
        canvas.drawCircle(700f,700f,60f,paint);


        /**画文字
         * 基准线
         * */
        Paint paint4 = new Paint();
        paint4.setARGB(100,120,255,0);
        paint4.setTextSize(200);//文字大小
        paint4.setStrokeWidth(10);//边框线大小
        //基准线
        canvas.drawLine(0,1000,1000,1000,paint4);
        canvas.drawText("appblmqq",20,1000,paint4);

        /**
         * 绘制图片到画布
         * */
        Paint paint6 = new Paint();
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.kk),200,1200,paint6);
    }


}

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

        setContentView(new MyView(this));
    }

}

你可能感兴趣的:(Android,相关,android,笔记)