Android SurfaceView画板操作

作者:汪星没有熊 时间:2022-07-10 15:12:23 

本文实例为大家分享了Android SurfaceView画板操作的具体代码,供大家参考,具体内容如下

画板——画路径

package com.example.review.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
* 画板画路径
*/
public class HuabanView extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder surfaceHolder;
private Path path = new Path();

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

public HuabanView(Context context, AttributeSet attrs) {
 super(context, attrs);
 surfaceHolder = getHolder();
 surfaceHolder.addCallback(this);//获得surfaceview的生命周期
}

public HuabanView(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
}

public HuabanView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
 super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
 new HuabanThread().start();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

@Override
public boolean onTouchEvent(MotionEvent event) {
 float x = event.getX();
 float y = event.getY();
 int action = event.getAction();
 if (action == MotionEvent.ACTION_DOWN) {//按下
  path.moveTo(x, y);
 } else if (action == MotionEvent.ACTION_MOVE) {//移动
  path.lineTo(x, y);
 }
 return true;
}

class HuabanThread extends Thread {
 @Override
 public void run() {
  super.run();
  //TODO:画笔
  Paint paint = new Paint();
  paint.setColor(Color.BLACK);
  paint.setStrokeWidth(20);
  paint.setStyle(Paint.Style.STROKE);
  paint.setAntiAlias(true);
  //TODO:画布
  while (true) {
   Canvas canvas = surfaceHolder.lockCanvas();
   //避免空指针
   if (canvas == null){
    return;
   }
   canvas.drawColor(Color.WHITE, PorterDuff.Mode.CLEAR);
   canvas.drawColor(Color.WHITE);
   canvas.drawPath(path,paint);
   surfaceHolder.unlockCanvasAndPost(canvas);
  }
 }
}
public void close(){
 path.reset();
}
}

画板——画动态直线

package com.example.review.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
* 画板画路径
* 画动态直线
*/
public class LineView extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder surfaceHolder;
private Path path = new Path();
private int x = 0;

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

public LineView(Context context, AttributeSet attrs) {
 super(context, attrs);
 surfaceHolder = getHolder();
 surfaceHolder.addCallback(this);//获得surfaceview的生命周期
}

public LineView(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
}

public LineView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
 super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
 new HuabanThread().start();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

class HuabanThread extends Thread {
 @Override
 public void run() {
  super.run();
  //TODO:画笔
  Paint paint = new Paint();
  paint.setColor(Color.BLACK);
  paint.setStrokeWidth(20);
  paint.setStyle(Paint.Style.STROKE);
  paint.setAntiAlias(true);
  //TODO:画布
  while (true) {
   Canvas canvas = surfaceHolder.lockCanvas();
   //避免空指针
   if (canvas == null){
    return;
   }
   canvas.drawColor(Color.WHITE, PorterDuff.Mode.CLEAR);
   canvas.drawColor(Color.WHITE);
   canvas.drawLine(0,100,x++,100,paint);
   surfaceHolder.unlockCanvasAndPost(canvas);
  }
 }
}

public void close(){
 path.reset();
}

}

基本图形

//圆
canvas.drawOval(50,100,150,200,paint);
//半圆
canvas.drawArc(500,500,700,700,20,180,true,paint);
//矩形
canvas.drawRect(100,300,250,400,paint);
//三角形
canvas.drawLine(100,450,0,600,paint);
canvas.drawLine(0,600,400,600,paint);
canvas.drawLine(100,450,400,600,paint);
//梯形
canvas.drawLine(100,700,200,700,paint);
canvas.drawLine(100,700,0,900,paint);
canvas.drawLine(0,900,400,900,paint);
canvas.drawLine(200,700,400,900,paint);

//文字
canvas.drawText("截图",100,1000,paint);

来源:https://blog.csdn.net/weixin_45697390/article/details/105804970

标签:Android,SurfaceView,画板
0
投稿

猜你喜欢

  • 基于jQuery获取table数据发送到后端

    2023-07-22 22:07:43
  • java生成图片验证码实例代码

    2022-01-06 04:01:37
  • Winform让DataGridView左侧显示图片

    2021-09-24 03:50:38
  • Mybatis中连接查询和嵌套查询实例代码

    2021-08-23 13:24:31
  • Android蓝牙通信聊天实现发送和接受功能

    2022-02-07 21:58:56
  • Android Java try catch 失效问题及解决

    2023-06-17 17:07:33
  • 详谈java中int和Integer的区别及自动装箱和自动拆箱

    2023-01-18 23:25:20
  • c# socket编程udp客户端实现代码分享

    2023-06-16 05:03:31
  • SpringBoot处理接口幂等性的两种方法详解

    2021-12-23 10:32:32
  • Qt for Android开发实例教程

    2023-06-27 10:00:39
  • SpringBoot AOP方式实现多数据源切换的方法

    2023-04-08 20:58:08
  • 解读Spring-Context的作用及用法

    2023-09-27 06:45:02
  • Android音视频开发之MediaExtactor使用教程

    2023-03-20 19:25:11
  • Android实现的RecyclerView适配器

    2023-07-12 23:02:21
  • SpringBoot里使用Servlet进行请求的实现示例

    2021-09-17 11:47:37
  • 大前端代码重构之事件拦截iOS Flutter Vue示例分析

    2021-12-11 20:16:53
  • 一键移除ButterKnife并替换为ViewBinding的旧项目拯救

    2023-08-19 12:30:28
  • 基于Java GUI 事件处理方式

    2023-11-25 13:08:55
  • C#实现控制线程池最大数并发线程

    2023-02-23 14:50:43
  • Java中的拦截器、过滤器、监听器用法详解

    2021-08-08 16:26:03
  • asp之家 软件编程 m.aspxhome.com