SurfaceView实现涂鸦效果:

该博客介绍了如何利用SurfaceView在Android应用中实现涂鸦效果。内容包括XML布局的设计和Activity中的关键代码实现,让读者能够理解并实现自定义的画板功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

XML布局:

      <SurfaceView
       android:id="@+id/main_surface"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

Activity中的代码:

public class MainActivity extends AppCompatActivity {
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private Path path=new  Path();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //查找控件
        surfaceView=findViewById(R.id.main_surface);
        //获得SurfaceHolder
        surfaceHolder=surfaceView.getHolder();
        //鼠标的监听
        surfaceView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.e("#####","1111111111111");
                if(motionEvent.getAction()==MotionEvent.ACTION_DOWN){
                    //鼠标落下的时候
                    Log.e("#####","22222222");

                    path.moveTo(motionEvent.getX(),motionEvent.getY());
                }else if(motionEvent.getAction()==MotionEvent.ACTION_MOVE){
                    //鼠标移动的时候
                    Log.e("#####","3333333");

                    path.lineTo(motionEvent.getX(),motionEvent.getY());
                }
                return true;
            }
        });
        //重写方法
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                //while(true)属于耗时操作  在子线程中执行
                new MyThread().start();
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

            }
        });
    }
    class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
            //画笔
            Paint paint = new Paint();
            //画笔的大小
            paint.setTextSize(30);
            paint.setStyle(Paint.Style.STROKE);
            while (true){
                 //三原色
                int a =(int)(Math.random()*255);
                int a1 =(int)(Math.random()*255);
                int a2 =(int)(Math.random()*255);
                //执行三原色
                int argb = Color.argb(255, a, a1, a2);
                paint.setColor(argb);
                //开启画布
                Canvas canvas = surfaceHolder.lockCanvas();
                //清空画布
                canvas.drawColor(Color.WHITE, PorterDuff.Mode.CLEAR);
                //货值路径
                Log.i("lyj",""+path);
                canvas.drawPath(path,paint);
                //关闭画布
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值