Android使用SurfaceView实现墨迹天气的风车效果
SurfaceView也是继承自View,它和我们以前接触到的View(Button、TextView等)最大的不同是,SurfaceView可以有一个单独的线程进行绘制,这个线程区别于UI线程(主线程),因此SurfaceView绘制并不占用主线程资源。
SurfaceView实现通常是自定义,继承SurfaceView并实现SurfaceHolder.Callback接口。使用SurfaceView,所有的绘图工作必须得在Surface 被创建之后才能开始,所以Callback 中的surfaceCreated 和surfaceDestroyed 就成了绘图处理代码的边界。
实现SurfaceHolder.Callback接口需要重写的方法:
//在surface的大小发生改变时激发
(1)public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){}
//在创建时激发,一般在这里调用画图的线程。
(2)public void surfaceCreated(SurfaceHolder holder){}
//销毁时激发,一般在这里将画图的线程停止、释放。
(3)public void surfaceDestroyed(SurfaceHolder holder) {}
SurfaceHolder有几个重要方法:
// 锁定画布,一般在锁定后就可以通过其返回的画布对象Canvas,在其上面画图等操作了。
(1)、abstract Canvas lockCanvas();
// 锁定画布的某个区域进行画图等..因为画完图后,会调用下面的unlockCanvasAndPost来改变显示内容。
// 相对部分内存要求比较高的游戏来说,可以不用重画dirty外的其它区域的像素,可以提高速度。
(2)、abstract Canvas lockCanvas(Rect dirty);
// 结束锁定画图,并提交改变。(3)、abstract void unlockCanvasAndPost(Canvas canvas);
//给SurfaceView当前的持有者一个回调对象。
(4)、abstract void addCallback(SurfaceHolder.Callback callback);
下面使用SurfaceView实现墨迹天气的风车效果:
public class WindmillView extends SurfaceView implements SurfaceHolder.Callback, Runnable { private SurfaceHolder holder; private boolean isRunning = true; /** * 屏幕的像素 */ private int screenWidth; private int screenHeiht; private Bitmap windPoint; /** * 风车图片 */ private Bitmap Windmill; /** * 背景图片 */ private Bitmap viewBg; public WindmillView(Context context) { super(context); this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); holder = getHolder(); holder.addCallback(this); holder.setFormat(PixelFormat.RGBA_8888); // 顶层绘制SurfaceView设成透明 getViewSize(context); LoadWindmillImage(); } private void LoadWindmillImage() { viewBg = BitmapFactory.decodeResource(getResources(), R.drawable.bg_na); Windmill = BitmapFactory.decodeResource(getResources(), R.drawable.na_windmill); windPoint = BitmapFactory.decodeResource(getResources(), R.drawable.na_point); float percent = percentumW(); Log.v("icers", screenWidth + ""); int _witdh = (int) (250 / percent);//250是风车基点左侧像素 Log.v("icers", _witdh + ""); Windmill = Bitmap.createScaledBitmap(Windmill, _witdh * 2, _witdh * 2, true); } // 获取屏幕的分辨率 private void getViewSize(Context context) { DisplayMetrics metrics = new DisplayMetrics(); WindowManager windowManager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); windowManager.getDefaultDisplay().getMetrics(metrics); this.screenHeiht = metrics.heightPixels; this.screenWidth = metrics.widthPixels; Log.d("Windmill", "Windmill:"+screenHeiht+"|"+screenWidth); } /** * 获取背景图和风车的比率 ,从而根据这个比例改变各个手机上面的风车图片大小 * * * @return */ private float percentumW() { float bg_width = viewBg.getWidth(); return bg_width/screenWidth ; } /** * 获取背景图和风车的比率 ,从而根据这个比例改变各个手机上面的风车图片大小 * * * @return */ private float percentumH() { float bg_height = viewBg.getHeight(); return bg_height/(screenHeiht); } @Override public void run() { float rotate = 0;// 旋转角度变量 while (isRunning) { Log.i("icer", "Running"); Canvas canvas = null; synchronized (this) { try { canvas = holder.lockCanvas(); if (canvas != null) { Paint paint = new Paint(); paint.setAntiAlias(true); // 对图片抗锯齿 paint.setFilterBitmap(true); RectF rect = new RectF(0, 0, screenWidth, screenHeiht ); canvas.drawBitmap(viewBg, null, rect, paint); Matrix matrix = new Matrix(); matrix.postRotate((rotate += 2) % 360f, Windmill.getWidth() / 2, Windmill.getHeight() / 2); int _dy = (int) (500 /percentumH()); //500是风车基点到背景定点的像素 matrix.postTranslate(0, (_dy - (Windmill.getHeight()/2))); canvas.drawBitmap(Windmill, matrix, paint); int _dx = (int) (250 / percentumW());//250是风车基点左侧像素 canvas.drawBitmap(windPoint,_dx-windPoint.getWidth()/2,_dy-windPoint.getHeight()/2,paint); Thread.sleep(3); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (canvas != null) { holder.unlockCanvasAndPost(canvas); } } } } } public void setRunning(boolean state) { isRunning = state; } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder arg0) { new Thread(this).start(); } @Override public void surfaceDestroyed(SurfaceHolder arg0) { // TODO Auto-generated method stub isRunning = false; } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); WindmillView view=new WindmillView(this); setContentView(view); }
整个过程:继承SurfaceView并实现SurfaceHolder.Callback接口 ----> SurfaceView.getHolder()获得SurfaceHolder对象 ---->SurfaceHolder.lockCanvas()获得Canvas对象并锁定画布----> Canvas绘画 ---->SurfaceHolder.unlockCanvasAndPost(Canvas canvas)结束锁定画图,并提交改变,将图形显示。
/**
参考:
http://www.cnblogs.com/plokmju/p/android_SurfaceView.html
http://developer.android.com/reference/android/view/SurfaceView.html
http://blog.csdn.net/luoshengyang/article/details/8661317
http://blog.csdn.net/hellogv/article/details/5986835
http://developer.android.com/reference/android/view/SurfaceHolder.html