在了解 Android 中Paint 和 Canvas 的简单使用后,下面做一个简单的涂鸦功能

先看下效果图:
 

那么来看一下是如何完成的:

/** * author: wu * date: on 2018/12/11. * describe:涂鸦 */public class MyView3 extends View {    private Paint myPaint;    private Path myPath;    public MyView3(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);        myPaint = new Paint();        myPath = new Path();    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        myPaint.setColor(Color.BLUE);        myPaint.setStrokeWidth(5);        myPaint.setStyle(Paint.Style.STROKE);        canvas.drawPath(myPath, myPaint);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()) {            //按下            case MotionEvent.ACTION_DOWN:                myPath.moveTo(event.getX(), event.getY());                return true;            case MotionEvent.ACTION_MOVE:                myPath.lineTo(event.getX(), event.getY());                //刷新页面                invalidate();                break;            case MotionEvent.ACTION_UP:                break;        }        return super.onTouchEvent(event);    }}

代码非常简单,首先创建画笔,设置画笔颜色、风格;其次是根据手指动的时候绘制出对象的图像,然后实时更新页面即可。

然后在布局文件中加入下面代码即可:

    

简单的涂鸦的功能就完成了。

更多相关文章

  1. android 零散笔记不定期更新 v16
  2. Android中因为没有使用wifi模块 因此:将WIDGETS-->Settings sh...
  3. android 零散笔记不定期更新 v16
  4. 修改android桌面图标默认大小
  5. android 中apk如何防止反编译?
  6. gif文件导出png
  7. android 零散笔记不定期更新 v16
  8. Windows下git下载android source
  9. android 屏幕上面画线

随机推荐

  1. Android(安卓)SDK 2.2 开发环境安装
  2. 向android studio导入android源生app
  3. Android(安卓)学习笔记--android基本注意
  4. Android中的四种Activity
  5. AndroidStudio 备忘录之Spinner(下拉列表)
  6. android Shape实现边框圆角
  7. 【Android】 ListView之setEmptyView的问
  8. android 电池(二):android关机充电流程、充
  9. Android版本与Linux内核版本的关系
  10. android TabHost小结