1、视图

 <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"
> <Button
android:id="@+id/bt_clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清空"
/>
<com.zyhui.zyhsurfaceview.ZyhSurfaceView
android:id="@+id/zyh_sfv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/> </LinearLayout>

2、MainActivity

 package com.zyhui.zyhsurfaceview;

 import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity; public class MainActivity extends Activity implements OnClickListener { private ZyhSurfaceView zyh_sfv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
zyh_sfv = (ZyhSurfaceView) findViewById(R.id.zyh_sfv);
Button button = (Button) findViewById(R.id.bt_clear);
button.setOnClickListener(this);
} @Override
public void onClick(View v) {
zyh_sfv.clear();
} }

3、自定义surfaceView控件

 package com.zyhui.zyhsurfaceview;

 import java.util.ArrayList;
import java.util.List;
import java.util.Random; import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView; //====================================
//1、继承SurfaceView
//2、监听SurfaceView的生命周期
//3、自定义线程
//4、自定义线程需要的参数
//5、监听线程的生命周期
//6、通过SurfaceView的生命周期来执行我们的线程
//7、添加触摸事件
//
//主要是通过线程把图片画进holder中,这样它就会显示出来;
//如果直接在HandlerThread线程中更新ui控件是不行,它会报错的
//====================================
public class ZyhSurfaceView extends SurfaceView implements Callback { private DrawingThread drawingThread; public ZyhSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
initParams();
} public ZyhSurfaceView(Context context) {
super(context);
initParams();
} private void initParams(){
//监听SurfaceView的生命周期
getHolder().addCallback(this);
} @Override
public void surfaceCreated(SurfaceHolder holder) {
drawingThread = new DrawingThread(getHolder(),
BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
drawingThread.start();
} @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
//Log.i("zyh", width + "-----" + height);
this.drawingThread.updateSize(width, height);//更新宽和高
} @Override
public void surfaceDestroyed(SurfaceHolder holder) {
this.drawingThread.quit();
this.drawingThread = null;
} @Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
this.drawingThread.addItem(event.getX(), event.getY());
}
return super.onTouchEvent(event);
} //清空图片
public void clear(){
this.drawingThread.clear();
} //自定义线程
private class DrawingThread extends HandlerThread implements android.os.Handler.Callback{
private static final int MSG_ADD = 101;//添加消息
private static final int MSG_MOVE = 102;//消息移动
private static final int MSG_CLEAR = 103;//清空 //定义SurfaceView的宽和高
private int drawingWidth,drawingHeight; //缓存视图
private SurfaceHolder drawingHolder;
//画笔
private Paint paint;
//需要绘制的图片
private Bitmap iconBitmap;
//图片对象数组
private List<DrawingItem> locations;
//更新ui的Handler
private Handler receiver;
//线程是否在运行
private boolean isRunning = false; public DrawingThread(SurfaceHolder drawingHolder, Bitmap bitmap) {
super("DrawingThread");
this.drawingHolder = drawingHolder;
this.iconBitmap = bitmap;
this.locations = new ArrayList<DrawingItem>();
this.paint = new Paint(Paint.ANTI_ALIAS_FLAG);
} //监听线程的生命周期
@Override
protected void onLooperPrepared() {
super.onLooperPrepared();
this.receiver = new Handler(getLooper(),this);
this.isRunning = true;
this.receiver.sendEmptyMessage(MSG_ADD);
} @Override
public boolean quit() {
this.isRunning = false;
this.receiver.removeCallbacksAndMessages(null);//移除消息
return super.quit();
} public void updateSize(int width, int height){
this.drawingWidth = width;
this.drawingHeight = height;
} public void addItem(float x, float y){
Message msg = Message.obtain(receiver, MSG_ADD, (int)x, (int)y);
this.receiver.sendMessage(msg );
} public void clear(){
this.receiver.sendEmptyMessage(MSG_CLEAR);
} private class DrawingItem{
private int x,y;
private boolean isVertical,isHorizontal;
public DrawingItem(int x, int y, boolean isVertical,
boolean isHorizontal) {
this.x = x;
this.y = y;
this.isVertical = isVertical;
this.isHorizontal = isHorizontal;
} } @Override
public boolean handleMessage(Message msg) {
switch(msg.what){
case MSG_ADD:
Random random = new Random();
DrawingItem drawingItem = new DrawingItem(msg.arg1,msg.arg2,
random.nextBoolean(),random.nextBoolean());
locations.add(drawingItem);
break;
case MSG_MOVE:
if(!isRunning){
return true;
}
//获取枷锁画布
Canvas lockCanvas = this.drawingHolder.lockCanvas();
if(lockCanvas == null){
break;
}
lockCanvas.drawColor(Color.BLACK);
for(DrawingItem item : locations){
item.x += item.isHorizontal ? 5 : -5;
if(item.x > this.drawingWidth - iconBitmap.getWidth()){
item.isHorizontal = false;
}else{
item.isHorizontal = true;
} item.y += item.isVertical ? 5 : -5;
if(item.y > this.drawingHeight - iconBitmap.getHeight()){
item.isVertical = false;
}else{
item.isVertical = true;
} //绘图
lockCanvas.drawBitmap(iconBitmap, item.x, item.y, paint);
} this.drawingHolder.unlockCanvasAndPost(lockCanvas);//释放
//Log.i("zyh", Thread.currentThread().getName());
break;
case MSG_CLEAR:
//清空图片
locations.clear();
break;
default:
break;
} if(isRunning){
this.receiver.sendEmptyMessage(MSG_MOVE);//促使图片的移动
}
return false;
}
}
}

//画图程序:handleMessage
//画出的图之所以能够显示是因为通过SurfaceView的holder来获取画布

surfaceView画图的更多相关文章

  1. SurfaceView 使用demo 飞机游戏小样

    本demo 主要使用了surfaceview 画图. 1.在线程中对canvas操作. 2.实现画图 3.surfaceView 继承了view 可以重写ontouchevent方法来操作输入. 代码 ...

  2. 如何自学Android--转

    原文地址:http://blog.csdn.net/lavor_zl/article/details/51217319 1. Java知识储备 本知识点不做重点讲解: 对于有基础的同学推荐看<J ...

  3. Android 涂鸦最佳实践

    Android中实现手势画图一般都两种方式,一是直接在View上绘制,而是使用SurfaceView. 两者还是有一些差别的.简介下. View:显示视图,内置画布,提供图形绘制函数.触屏事件.按键事 ...

  4. 如何自学 Android 的?

    http://android.jobbole.com/83380/ 1. Java知识储备 本知识点不做重点讲解:对于有基础的同学推荐看<Java编程思想>,巩固基础,查漏补全,了解并熟悉 ...

  5. Android如何自学----转自lavor从segmentfault

    如何自学Android 1. Java知识储备 本知识点不做重点讲解: 对于有基础的同学推荐看<Java编程思想>,巩固基础,查漏补全,了解并熟悉更多细节知识点. 对于没有基础的同学推荐看 ...

  6. Android代码速查,写给新手的朋友们[转]

    原文地址:http://www.open-open.com/lib/view/open1397286499090.html 0 android 创建按钮 Button button = new But ...

  7. Android SurfaceView实现静态于动态画图效果

    本文是基于Android的SurfaceView的动态画图效果,实现静态和动态下的正弦波画图,可作为自己做图的简单参考,废话不多说,先上图, 静态效果: 动态效果: 比较简单,代码注释的也比较详细,易 ...

  8. android surfaceView 的简单使用 画图,拖动效果

    前面说到了画图,其实更好的就是使用 surfaceView了. surfaceView 继承于 View,View里面嵌套了一个专门用于画图的 surface, 对于一个View的onDraw()方法 ...

  9. Android 画图类View与SurfaceView之学习

    在开发游戏开发中,android相应的提供了几个重要的模块: 1.显示界面的视图:  Android 提供 View 和 SurfaceView 2.控制游戏整体结构: android 提供 Acti ...

随机推荐

  1. hdoj 1874 畅通工程续(单源最短路+dijkstra)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 思路分析:该问题给定一个无向图.起始点和终点,要求求出从起始点到终点的最短距离: 使用Dijks ...

  2. 柯里化函数之Javascript

    柯里化函数之Javascript 定义 依据定义来说,柯里化就是将一个接收"多个"參数的函数拆分成一个或者很多个接收"单一"參数的函数.定义看起来是比較抽象的. ...

  3. 【Android病毒分析报告】 - ZxtdPay 吸费恶魔

    本文章由Jack_Jia编写,转载请注明出处.  文章链接:http://blog.csdn.net/jiazhijun/article/details/11581543 作者:Jack_Jia    ...

  4. C# - String与StringBuilder

    A String object is called immutable (read-only), because its value cannot be modified after it has b ...

  5. CSS的float与clear

    首先要知道,div是块级元素,在页面中独占一行,自上而下排列,也就是传说中的流.如下图: 可以看出,即使div1的宽度很小,页面中一行可以容下div1和div2,div2也不会排在div1后边,因为d ...

  6. C# Best Practices - Creating Good Properties

    Coding Properties Code in the Getter Check the user's credentials Check application state Format the ...

  7. WRTnode 的 HTTP Web 开关实验(2016-05-16)

    前言 这里是节取自 物联网的任意门——WRTnode2R 评测 中的 http web 开关灯实验,所以有一些前置设置如果没有描述清楚可参考该处. 正文 步骤一:编辑一个 html 文件,放在 /ww ...

  8. xml校验问题

    struts2使用xml校验 按照书本输入dtd约束文件 "-//OpenSymphony Group//XWork Validator 1.0.2//EN""http: ...

  9. delphi数字签名验证及能够获取数字签名文件信息(利用wintrust.dll的导出函数,翻译一下)

    unit TrustCheck; interface uses Windows,SysUtils,jwaWinTrust,JwaWinCrypt; function CheckFileTrust(co ...

  10. 用Xamarin 实现园友的 :Android浮动小球与开机自启动

    原文:用Xamarin 实现园友的 :Android浮动小球与开机自启动 前两天看园子里有筒子写了个 Android浮动小球与开机自启动  , 感觉这种被 360 玩烂的功能原来是如此的简单啊... ...