欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android SurfaceView画板操作

程序员文章站 2022-06-20 13:13:43
本文实例为大家分享了android surfaceview画板操作的具体代码,供大家参考,具体内容如下画板——画路径package com.example.review.view;import and...

本文实例为大家分享了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);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。