学习安卓小码哥自定义控件的笔记(一)

这几天学习了一下小码哥的安卓教程,记录一下安卓的自定义控件的一些笔记.

package com.example.wtz.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class CanvasView extends View {
    public CanvasView(Context context) {
        super(context);
    }

    public CanvasView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画线
        Paint paint = new Paint();
        canvas.drawLine(0, 0, 100, 100, paint);
        //画文字
        paint.setTextSize(20);
        paint.setColor(Color.RED);
        canvas.drawText("王天泽的test", 200, 200, paint);

        //画点
        paint.setStrokeWidth(5);
        canvas.drawPoint(150, 150, paint);

        //画弧度
        paint.setStyle(Paint.Style.STROKE);
        RectF rectF = new RectF(100, 100, 200, 200);
        canvas.drawArc(rectF, 30, 60, true, paint);

        //画图片
        //1.用项目中已经有的图片
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        //2.从0开始创建Bitmap
        Bitmap createdBmp = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 100; j++) {
                createdBmp.setPixel(i, j, Color.RED);
            }
        }
        canvas.drawBitmap(createdBmp, 150, 300, paint);
    }
}

在这里插入图片描述

package com.example.wtz.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class CanvasPathView extends View {
    public CanvasPathView(Context context) {
        super(context);
    }

    public CanvasPathView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint paint = new Paint();
        //如果需要绘制path的话,需要设置paint的style为STROKE
        paint.setStyle(Paint.Style.STROKE);
        //复杂的线条需要通过Path来画
        Path path = new Path();
        //移动接下来的要绘制的内容的起点位置
        path.moveTo(50, 50);
        //直线
        path.lineTo(100, 100);
        //二阶贝塞尔曲线
        path.quadTo(200, 200, 300, 100);
        canvas.drawPath(path, paint);
    }
}

在这里插入图片描述

package com.example.wtz.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class TranslateCanvasView extends View {
    public TranslateCanvasView(Context context) {
        super(context);
    }

    public TranslateCanvasView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        canvas.translate(100, 100);
        //保存当前绘制的坐标系
        canvas.save();
        canvas.rotate(90);
        //销毁坐标系,还原到上一次保存的坐标系效果
        canvas.restore();
        canvas.drawLine(0, 0, 100, 100, paint);
    }
}

在这里插入图片描述

package com.example.wtz.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.icu.util.Measure;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.View;

public class MeasureView extends View {

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

    public MeasureView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.v(getClass().getSimpleName(), "onMeasure:widthMeasureSpec=" + widthMeasureSpec + ",onMeasure:heightMeasureSpec=" + heightMeasureSpec);
        //MeasureSpec 由两个int值组成的
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
//        setMeasuredDimension(50, 50);
        //UNSPECIFIED 父控件对你的尺寸不做任何限制
        //ATMOST代表父控件对你的尺寸设置了最大值

        switch (widthMode) {
            case MeasureSpec.UNSPECIFIED:
                break;
            case MeasureSpec.AT_MOST:
                break;
            case MeasureSpec.EXACTLY:
                break;
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.RED);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值