温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

详解Android中SurfaceView画板操作

发布时间:2020-07-20 17:33:25 来源:亿速云 阅读:231 作者:小猪 栏目:移动开发

这篇文章主要详解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);

看完上述内容,是不是对详解Android中SurfaceView画板操作有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI