在项目实战中我们可能会遇到一些变动比较快,需要经常变化的视图显示,而此时普通的View在性能和效率上没有办法完全满足我们的需求,Android此时提供了一个特殊的视图类surfaceview。
.surfaceview 本身就是一个view,所以它符合view所有的特性(设置是否可见,宽高这些属性 都满足)。
.surfaceview界面默认是没有任何内容的,需要通过canvas来绘制。
.提供了独立于主线程紫外的线程中绘制(可以不再UI线程中绘制,可以在一个一个线程中不听的更新界面
并且不会引起ANR)
.使用surfaceview 和普通控件使用一样,但是必须的实现surfaceholder的回调(当surfaceview发生变化时自动 回调)
. 获取surfaceHolder 通过getHolder()获得,在通过lockcanvas()把用于绘图的canvas返回,当绘制结束可 以通过unlockCanvasAndPost()提交刚才修改的界面。
相关函数以及用途:
下面 我们做一个实时更新的动画,效果图如下:
代码简单直接贴代码:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_my_surface_view" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.chenzhu.myprojectblog.MySurfaceViewActivity"> <SurfaceView android:id="@+id/mySurfaceView" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
package com.example.chenzhu.myprojectblog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import java.util.Random; public class MySurfaceViewActivity extends AppCompatActivity { SurfaceView surfaceView ; SurfaceHolder surfaceHolder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_surface_view); surfaceView = (SurfaceView)findViewById(R.id.mySurfaceView); surfaceHolder =surfaceView.getHolder();//获取surfaceholder的实例 surfaceHolder.addCallback(new SurfaceHolder.Callback() {//为surfaceview设置回调 @Override public void surfaceCreated(SurfaceHolder surfaceHolder) { //默认的画一张图片在surfaceview上 Canvas canvas = surfaceHolder.lockCanvas(); canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher), 50 ,50 ,new Paint()); surfaceHolder.unlockCanvasAndPost(canvas); //这里我们开一个子线程来更改surfaceview上的界面 new MyThread(surfaceHolder).start(); } @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { } @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { } }); } class MyThread extends Thread{ SurfaceHolder surfaceHolder; Random random = new Random(0); boolean refresh = true; public MyThread(SurfaceHolder surfaceHolder){ this.surfaceHolder = surfaceHolder; } @Override public void run() { super.run(); //开启一个死循环模拟一直更改surfaceview while (refresh){ Canvas canvas = surfaceHolder.lockCanvas(); if(canvas==null){ return; } int left = random.nextInt(canvas.getMaximumBitmapWidth()/40); int top = random.nextInt(canvas.getMaximumBitmapHeight()/20); Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher); canvas.drawBitmap(bitmap,left,top,new Paint()); surfaceHolder.unlockCanvasAndPost(canvas); Log.d("CZLOG",""+left+"-----------"+top+"sum"); try { sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } }