当前位置: 移动技术网 > 移动技术>移动开发>Android > Android自定义SurfaceView实现画板功能

Android自定义SurfaceView实现画板功能

2019年07月24日  | 移动技术网移动技术  | 我要评论

接触了这么久的view,总不能一直停留在view里,现在开始呢,就要学习一个新的知识点:surfaceview,实际上surfaceview与view的原理都差不多,只是效率和渲染方式上,surfaceview要优于view,这也是我们写这个的原因。今天就看看这个surfaceview,好了,下面就是今天要说的效果。

界面很简单,就是一个按钮以及一个画板,先看看界面的代码吧

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
  android:orientation="vertical"
 tools:context="com.example.xinxindemo.mainactivity" >

 <com.example.xinxindemo.view.secondsurfaceview
  android:id="@+id/surfaceview"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="20"
   />

 <linearlayout
  android:layout_width="match_parent"
  android:layout_height="55dip"
  android:orientation="horizontal"
  android:padding="5dip" >

  <button
   android:onclick="onclick"
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
   android:text="clean" />
</linearlayout>

</linearlayout>

对吧,界面不是很复杂,下面再看看这个secondsurfaceview是怎么实现的;

/**
 * 2016年7月26日17:20:13
 * @author 小瓶盖 blog 
 *
 */
public class secondsurfaceview extends surfaceview implements surfaceholder.callback,runnable{

 /**
  * 是否处于绘制状态
  */
 private boolean misdrawing;
 /**
  * 帮助类
  */
 private surfaceholder mholder;
 /**
  * 画布
  */
 private canvas mcanvas;
 /**
  * 路径
  */
 private path mpath;
 /**
  * 画笔
  */
 private paint mpaint;

 public secondsurfaceview(context context, attributeset attrs, int defstyle) {
  super(context, attrs, defstyle);
  initview();
 }

 public secondsurfaceview(context context, attributeset attrs) {
  super(context, attrs);
  initview();
 }

 public secondsurfaceview(context context) {
  super(context);
  initview();
 }
 @override
 public boolean ontouchevent(motionevent event) {

  int x=(int) event.getx();
  int y=(int) event.gety();
  switch (event.getaction()) {
  case motionevent.action_down:
   mpath.moveto(x, y);
   break;
  case motionevent.action_move:
   mpath.lineto(x, y);
   break;
  case motionevent.action_up:

   break;
  default:
   break;
  }

  return true;
 }

 private void initview() {
  mholder=getholder();
  mholder.addcallback(this);
  setfocusable(true);
  setfocusableintouchmode(true);
  this.setkeepscreenon(true);

  mpath=new path();

  mpaint=new paint();
  mpaint.setantialias(true);
  mpaint.setcolor(color.black);
  mpaint.setstyle(style.stroke);
  mpaint.setstrokewidth(15);
 }

 @override
 public void run() {
  long start =system.currenttimemillis();
  while(misdrawing){
   draw();
  }
  long end =system.currenttimemillis();
  if (end-start<100) {
   try {
    thread.sleep(100-(end-start));
   } catch (exception e) {
    e.printstacktrace();
   }
  }
 }

 @override
 public void surfacechanged(surfaceholder arg0, int arg1, int arg2, int arg3) {


 }

 @override
 public void surfacecreated(surfaceholder arg0) {
  misdrawing=true;
  new thread(this).start();
 }

 @override
 public void surfacedestroyed(surfaceholder arg0) {
  misdrawing=false;

 }
 private void draw(){
  try {
   mcanvas=mholder.lockcanvas();
   mcanvas.drawcolor(color.white);
   mcanvas.drawpath(mpath, mpaint);
  } catch (exception e) {
   e.printstacktrace();
  }finally{
   if (mcanvas!=null) {
    mholder.unlockcanvasandpost(mcanvas);
   }
  }
 }
 /**
  * 清除内容
  */
 public void clean(){
  initview();
 }
}

然后就是mainactivity.java

/**
 * 2016年7月26日17:20:13
 * @author 小瓶盖 blog *
 */
public class mainactivity extends activity{
 secondsurfaceview surfaceview;

 @override
 protected void oncreate(bundle savedinstancestate) {

  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  surfaceview=(secondsurfaceview)findviewbyid(r.id.surfaceview);
 }

 public void onclick(view v){
  surfaceview.clean();
 }

}

源码下载:

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网