android 自定义画布,Android自定义SurfaceView实现画板功能

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

86abba2e8555e0e534498d298834d352.png

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

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" >

android:id="@+id/surfaceview"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="20"

/>

android:layout_width="match_parent"

android:layout_height="55dip"

android:orientation="horizontal"

android:padding="5dip" >

android:onClick="onClick"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:text="clean" />

对吧,界面不是很复杂,下面再看看这个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();

}

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值