安卓通过bitmap和自定义view实现涂鸦画板

1.获取画板视图的大小,用来初始化一个位图

使用view的post函数,当view加载完成时会调用,传入的位图大小就是view的大小

tuYaVieew.post(new Runnable() {
    @Override
    public void run() {
        tuYaVieew.setBitmap(Bitmap.createBitmap(tuYaVieew.getMeasuredWidth(),tuYaVieew.getMeasuredHeight(), Bitmap.Config.ARGB_8888));
    }
});

//外部设置位图,执行初始化画板
public void setBitmap(Bitmap bitmap)
{
    this.bitmap = bitmap;
    bitmapcanvas = new Canvas(bitmap);
    bitmapcanvas.drawColor(Color.WHITE);
    initView();
}

2.初始化画笔

//初始化画板,画笔
private void initView()
{
    setFocusable(true);
    setFocusableInTouchMode(true);
    setKeepScreenOn(true);
    paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
    paint.setStrokeWidth(10);
    paint.setColor(Color.BLUE);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeCap(Paint.Cap.ROUND);
}

3.设置监听事件,当在view上滑动时,会把涂鸦保存在自己设置位图的画板上

//绘制涂鸦
@Override
public boolean onTouchEvent(MotionEvent event) {

    switch (event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
        {
            x = event.getX();
            y = event.getY();
            break;
        }
        case MotionEvent.ACTION_MOVE:
        {
            float stopx,stopy;
            stopx = event.getX();
            stopy = event.getY();
            bitmapcanvas.drawLine(x,y,stopx,stopy,paint);
            x = stopx;
            y = stopy;
            invalidate();
            break;
        }
    }
    return true;
}

4.更新显示的视图,把保存的bitmap显示出来

//更新视图
@Override
protected void onDraw(Canvas canvas) {
    if(bitmap != null)
    canvas.drawBitmap(bitmap,0,0,paint);
}

5.完整代码

package com.nihaoa.tuya;

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


public class TuYaVieew extends View {

    private Paint paint;
    private Bitmap bitmap;
    private Canvas bitmapcanvas;
    float x,y;

    public TuYaVieew(Context context) {
        super(context);
        initView();
    }

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

    public TuYaVieew(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public TuYaVieew(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }


    //更新视图
    @Override
    protected void onDraw(Canvas canvas) {
        if(bitmap != null)
        canvas.drawBitmap(bitmap,0,0,paint);
    }

    //绘制涂鸦
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {
                x = event.getX();
                y = event.getY();
                break;
            }
            case MotionEvent.ACTION_MOVE:
            {
                float stopx,stopy;
                stopx = event.getX();
                stopy = event.getY();
                bitmapcanvas.drawLine(x,y,stopx,stopy,paint);
                x = stopx;
                y = stopy;
                invalidate();
                break;
            }
        }
        return true;
    }

    //初始化画板,画笔
    private void initView()
    {
        setFocusable(true);
        setFocusableInTouchMode(true);
        setKeepScreenOn(true);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        paint.setStrokeWidth(10);
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
    }

    //外部设置位图,执行初始化画板
    public void setBitmap(Bitmap bitmap)
    {
        this.bitmap = bitmap;
        bitmapcanvas = new Canvas(bitmap);
        bitmapcanvas.drawColor(Color.WHITE);
        initView();
    }

    //注入画笔
    public void setPaint(Paint paint)
    {
        this.paint = paint;
    }
    //获取位图
    public Bitmap getBitmap()
    {
        return this.bitmap;
    }

}



你可能感兴趣的:(android,android,view,bitmap,surfaceviw)