案例功能,点击界面图片跟随变化
自定义surfaceiew
1 package caicai.animation; 2 3 import android.R.color; 4 import android.content.Context; 5 import android.graphics.Bitmap; 6 import android.graphics.BitmapFactory; 7 import android.graphics.Canvas; 8 import android.graphics.Color; 9 import android.graphics.Paint; 10 import android.util.Log; 11 import android.view.MotionEvent; 12 import android.view.SurfaceHolder; 13 import android.view.SurfaceHolder.Callback; 14 import android.view.SurfaceView; 15 16 public class MySurfaceView extends SurfaceView implements Callback, Runnable { 17 private Paint paint;// 画布 18 private SurfaceHolder sfh; // 用于控制SurfaceView 19 private Canvas canvas;// 画布 20 private boolean flag;// 关闭线程标志 21 private Thread th;// 新建线程 22 private int x = 0, y = 0;// 图片坐标 23 private Bitmap bitmap;// 图片资源 24 25 public MySurfaceView(Context context) { 26 super(context); 27 sfh = this.getHolder();// 实例SurfaceHolder 28 sfh.addCallback(this);// 为SurfaceView添加状态监听 29 bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tanke);//实例化图片 30 paint = new Paint();//实例一个画笔 31 paint.setColor(Color.WHITE);//设置画笔颜色为白色 32 setFocusable(true);//设置焦点 33 } 34 35 @Override 36 public void surfaceCreated(SurfaceHolder holder) { 37 flag = true; 38 //实例线程 39 th = new Thread(this); 40 //启动线程 41 th.start(); 42 } 43 44 public void mydraw() { 45 try { 46 canvas = sfh.lockCanvas();//锁定画布 47 if (canvas != null) { 48 canvas.drawRGB(0, 0, 0);//刷屏,或者叫重置画布 49 canvas.drawBitmap(bitmap, x, y, paint);//绘图 50 } else { 51 Log.i("tt", "获取不到画布");//释放画布 52 } 53 } catch (Exception e) { 54 55 } finally { 56 if (canvas != null) 57 sfh.unlockCanvasAndPost(canvas); 58 } 59 } 60 /** 61 * SurfaceView视图状态发生改变,响应此函数 62 */ 63 @Override 64 public void surfaceChanged(SurfaceHolder holder, int format, int width, 65 int height) { 66 // TODO Auto-generated method stub 67 68 } 69 /** 70 * SurfaceView视图消亡时,响应此函数 71 */ 72 @Override 73 public void surfaceDestroyed(SurfaceHolder holder) { 74 flag = false;//结束游戏,设置线程关闭标志为false 75 76 } 77 78 public void logic() { 79 80 }; 81 82 @Override 83 public void run() { 84 while (flag) { 85 long start = System.currentTimeMillis(); 86 mydraw(); 87 logic(); 88 long end = System.currentTimeMillis(); 89 try { 90 if (end - start < 50) { 91 Thread.sleep(50 - (end - start)); 92 } 93 } catch (InterruptedException e) { 94 e.printStackTrace(); 95 } 96 } 97 } 98 //获取点击坐标 99 @Override 100 public boolean onTouchEvent(MotionEvent event) { 101 x = (int) event.getX(); 102 y = (int) event.getY(); 103 return true; 104 } 105 106 }
新建主界面
1 package com.gsf; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Window; 6 import android.view.WindowManager; 7 8 public class MainActivity extends Activity { 9 @Override 10 public void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 //设置全屏 13 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 14 requestWindowFeature(Window.FEATURE_NO_TITLE); 15 //显示自定义的SurfaceView视图 16 setContentView(new MySurfaceView(this)); 17 } 18 }
推广网站趣淘鼓浪屿(www.qtgly.com)谢谢支持