public class MoveActActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置不显示title
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new MapView(this));
}
class MapView extends View {
/**
* 小图块的宽高
*/
public static final int TITLE_WIDTH = 16;
public static final int TITLE_HEIGHT = 25;
/**
* 屏幕宽高度
*/
private int bw = 0;
private int bh = 0;
/**
*屏幕总块数
*/
private int bwc = 0;
private int bhc = 0;
Bitmap mBitmap = null;
Bitmap bitmap[];
Bitmap myBitmap;
Canvas canvas;
public MapView(Context context) {
// TODO Auto-generated constructor stub
super(context);
//获取图片
mBitmap = ReadBitmap(context, R.drawable.action);
//获得图片宽高
bw = mBitmap.getWidth();
bh = mBitmap.getHeight();
//计算屏幕摆放数量
bwc = bw / TITLE_WIDTH;
bhc = bh / TITLE_HEIGHT;
//把获取图片切割成小块,放在数组中
Bitmap no1 = Bitmap.createBitmap(mBitmap, 0, 0, TITLE_WIDTH,
TITLE_HEIGHT);
Bitmap no2 = Bitmap.createBitmap(mBitmap, 0, 1 * TITLE_HEIGHT,
TITLE_WIDTH, TITLE_HEIGHT);
Bitmap no3 = Bitmap.createBitmap(mBitmap, 0, 2 * TITLE_HEIGHT,
TITLE_WIDTH, TITLE_HEIGHT);
Bitmap no4 = Bitmap.createBitmap(mBitmap, 0, 3 * TITLE_HEIGHT,
TITLE_WIDTH, TITLE_HEIGHT);
bitmap = new Bitmap[] { no1, no2, no3, no4};
}
public void onDraw(Canvas canvas){
super.onDraw(canvas);
Paint mPaint = new Paint();
DrawAnimation(bitmap,mPaint,canvas);
}
//以下方法 为何不能画出动画效果??
private void DrawAnimation(Bitmap[] bitmap2, Paint paint, Canvas canvas) {
for(int i = 0; i < 4 ; i++){
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bitmap2[i], 100,100, paint);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 根据ID读取资源图片
* @param context
* @param resId
* @return
*/
private Bitmap ReadBitmap(Context context, int resId) {
// TODO Auto-generated method stub
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = false;
opt.inInputShareable = false;
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is);
}
}
}
分享到: