SurfaceView基础

为什么要使用SurfaceView来实现动画?

因为View的绘图存在以下缺陷:

View缺乏双缓冲机制
当程序需要更新View上的图像时,程序必须重绘View上显示的整张图片
新线程无法直接更新View组件

SurfaceView的绘图机制

一般会与SurfaceView结合使用
调用SurfaceView的getHolder()方法即可获得SurfaceView关联的SurfaceHolder

SurfaceHolder提供了如下方法来获取Canvas对象

Canvas lockCanvas():锁定整个SurfaceView对象,获取该Surface上的Canvas
Canvas lockCanvas(Rect dirty):锁定SurfaceView上Rect划分的区域,获取该Surface上的Canvas
unlockCanvasAndPost(canvas):释放绘图、提交所绘制的图形,需要注意,当调用SurfaceHolder上的unlockCanvasAndPost方法之后,该方法之前所绘制的图形还处于缓冲之中,下一次lockCanvas()方法锁定的区域可能会“遮挡”它

public class SurfaceViewTest extends Activity

{
// SurfaceHolder负责维护SurfaceView上绘制的内容
private SurfaceHolder holder;
private Paint paint;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    paint = new Paint();
    SurfaceView surface = (SurfaceView) findViewById(R.id.show);
    // 初始化SurfaceHolder对象
    holder = surface.getHolder();
    holder.addCallback(new Callback()
    {
        @Override
        public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
                int arg3)
        {
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder)
        {
            // 锁定整个SurfaceView
            Canvas canvas = holder.lockCanvas();
            // 绘制背景
            Bitmap back = BitmapFactory.decodeResource(
                SurfaceViewTest.this.getResources()
                , R.drawable.sun);
            // 绘制背景
            canvas.drawBitmap(back, 0, 0, null);
            // 绘制完成,释放画布,提交修改
            holder.unlockCanvasAndPost(canvas);
            // 重新锁一次,"持久化"上次所绘制的内容
            holder.lockCanvas(new Rect(0, 0, 0, 0));
            holder.unlockCanvasAndPost(canvas);
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder)
        {
        }
    });
    // 为surface的触摸事件绑定监听器
    surface.setOnTouchListener(new OnTouchListener()
    {
        @Override
        public boolean onTouch(View source, MotionEvent event)
        {
            // 只处理按下事件
            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                int cx = (int) event.getX();
                int cy = (int) event.getY();
                // 锁定SurfaceView的局部区域,只更新局部内容
                Canvas canvas = holder.lockCanvas(new Rect(cx - 50,
                        cy - 50, cx + 50, cy + 50));
                // 保存canvas的当前状态
                canvas.save();
                // 旋转画布
                canvas.rotate(30, cx, cy);
                paint.setColor(Color.RED);
                // 绘制红色方块
                canvas.drawRect(cx - 40, cy - 40, cx, cy, paint);
                // 恢复Canvas之前的保存状态
                canvas.restore();
                paint.setColor(Color.GREEN);
                // 绘制绿色方块
                canvas.drawRect(cx, cy, cx + 40, cy + 40, paint);
                // 绘制完成,释放画布,提交修改
                holder.unlockCanvasAndPost(canvas);
            }
            return false;
        }
    });
}
}

上面的程序为SurfaceHolder添加了一个CallBack实例,该Callback中定义了如下三个方法:

void surfaceChanged(SurfaceHolder holder, int format, int width, int height):当一个surface的格式或大小发生改变时回调该方法。
void surfaceCreated(SurfaceHolder holder):当surface被创建时回调该方法
void surfaceDestroyed(SurfaceHolder holder):当surface将要被销毁时回调该方法

你可能感兴趣的:(SurfaceView基础)