<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>
/**
* 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();
}
}
/**
* 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();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有