开发学院手机开发Android 开发 Android canvas的save()和restore()用法 阅读

Android canvas的save()和restore()用法

 2010-06-19 17:38:00 来源:WEB开发网   
核心提示:在读Android SDK里面的samples里面游戏的时候,很多时候都看到对canvas的save()和restore()运用,Android canvas的save()和restore()用法,下面是个小程序,使用了这两个方面,使得旋转红色方块的时候,保证蓝色方块不受影响

在读Android SDK里面的samples里面游戏的时候,很多时候都看到对canvas的save()和restore()运用。

下面是个小程序,使用了这两个方面,使得旋转红色方块的时候,保证蓝色方块不受影响。

代码如下:

Java代码

package com.ray.test;

import android.app.Activity;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.os.Bundle;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

public class Test extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(new MyView(this));

}

class MyView extends SurfaceView implements SurfaceHolder.Callback{

private SurfaceHolder mHolder;

private Canvas canvas;

public MyView(Context context) {

super(context);

mHolder = getHolder();

mHolder.addCallback(this);

}

@Override

public void surfaceChanged(SurfaceHolder holder, int format, int width,

int height) {

}

@Override

public void surfaceCreated(SurfaceHolder holder) {

canvas = mHolder.lockCanvas();

Paint mPaint = new Paint();

mPaint.setColor(Color.BLUE);

canvas.drawRect(100, 200, 200, 300, mPaint);

canvas.save();

canvas.rotate(45);

mPaint.setColor(Color.RED);

canvas.drawRect(150, 10, 200, 60, mPaint);

canvas.restore();

mHolder.unlockCanvasAndPost(canvas);

}

@Override

public void surfaceDestroyed(SurfaceHolder holder) {

// TODO Auto-generated method stub

}

}

}

Tags:Android canvas save

编辑录入:coldstar [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接