android之简易画板(涂鸦)实例(透明背景)

 

一、创建变量 

//DoodleView
private Paint mPaint = new Paint();
private List<Path> mPathList = new ArrayList<>(); // 保存涂鸦轨迹的集合
private float mLastX, mLastY;
private Path mCurrentPath; // 当前的涂鸦轨迹
private Canvas canvas;
private ImageView imageView;
private Bitmap bitmap;

二、初始化变量

void initDoodleView(){
        imageView = findViewById(R.id.doodleView_container);
        imageView.setOnTouchListener(this);

        // 设置画笔
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(3);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        //创建黑色背景的bitmap
        //bitmap = Bitmap.createBitmap(imageView.getWidth(),imageView.getHeight(), Bitmap.Config.RGB_565);
        //获取drawable中的透明图片创建透明背景的bitmap
        Bitmap bitmapT = BitmapFactory.decodeResource(getResources(),R.drawable.transparent);
        //把bitmapT从坐标(0,0)开始裁剪成需要的大小
        bitmap = Bitmap.createBitmap(bitmapT, 0, 0, imageView.getWidth(), imageView.getHeight());
        canvas = new Canvas(bitmap);
        imageView.setImageBitmap(bitmap);

}

三、在onTouch事件中添加涂鸦路径

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
        int x = (int)motionEvent.getX();//motionEvent.getRawX();
        int y = (int)motionEvent.getY();
        view.performClick();

        switch(motionEvent.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                 mCurrentPath = new Path(); // 新的涂鸦
                 mPathList.add(mCurrentPath); // 添加的集合中
                 mCurrentPath.moveTo(motionEvent.getX(), motionEvent.getY());
                 mLastX = motionEvent.getX();
                 mLastY = motionEvent.getY();
                 view.invalidate(); // 刷新
                
                break;
            case MotionEvent.ACTION_UP:
                 mCurrentPath.quadTo(
                      mLastX,
                      mLastY,
                      (motionEvent.getX() + mLastX) / 2,
                      (motionEvent.getY() + mLastY) / 2); // 使用贝塞尔曲线 让涂鸦轨迹更圆滑
                 mCurrentPath = null; // 轨迹结束
                 view.invalidate(); // 刷新

                break;
            case MotionEvent.ACTION_MOVE:
                 mCurrentPath.quadTo(
                         mLastX,
                         mLastY,
                         (motionEvent.getX() + mLastX) / 2,
                         (motionEvent.getY() + mLastY) / 2); // 使用贝塞尔曲线 让涂鸦轨迹更圆滑
                 mLastX = motionEvent.getX();
                 mLastY = motionEvent.getY();

                 canvas.drawPath(mCurrentPath, mPaint);
                 view.invalidate(); // 刷新

                break;
        }

        return true;
    }

四、清除Imageview中的所有涂鸦

//bitmap = Bitmap.createBitmap(imageView.getWidth(),imageView.getHeight(), Bitmap.Config.RGB_565);
bitmap = Bitmap.createBitmap(bitmapT, 0, 0, imageView.getWidth(),imageView.getHeight());
canvas = new Canvas(bitmap);
if (bitmap!=null && imageView!=null)
    imageView.setImageBitmap(bitmap);

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值