package com.example.examples_05_07;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
public class GameView extends View implements Runnable {
Bitmap mBitQQ;//声明一个位图
Bitmap mBitDestTop;//声明一个位图
int miDTX=0;
public GameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
miDTX=0;
/*从资源文件转载图片*/
//getResources()->得到Resources
//getDrawable()->得到资源中的Drawable对象,参数为资源索引ID
//getBitmap()->得到Bitmap
//得到位图,将得到的图片存储在位图中
mBitQQ=((BitmapDrawable)getResources().getDrawable(R.drawable.qq)).getBitmap();
mBitDestTop=((BitmapDrawable)getResources().getDrawable(R.drawable.desktop)).getBitmap();
//开启线程
new Thread(this).start();
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
//清屏效果
canvas.drawColor(Color.BLACK);
//在屏幕0,0处绘制该图像
GameView.drawImage(canvas, mBitQQ, 0, 0);
/*在指定位置区域进行裁剪*/
//getWidth()->得到图片的宽度
//getHeight()->得到图片的高度
GameView.drawImage(canvas, mBitDestTop, miDTX, mBitQQ.getHeight(), mBitDestTop.getWidth(), mBitDestTop.getHeight()/2,
0, 0);
}
public void run() {
// TODO Auto-generated method stub
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO: handle exception
Thread.currentThread().interrupt();
}
postInvalidate();
}
}
/**--------------------------------------
* 绘制图片
* @param x:屏幕x坐标
* @param y:屏幕y坐标
* @param w:要绘制图片的宽度
* @param h:要绘制图片的高度
* @param bx:图片上的x坐标
* @param by:图片上的y坐标
* */
public static void drawImage(Canvas canvas,Bitmap bitmap,int x,int y,int w,int h,int bx,int by) {
Rect src=new Rect();//图片裁剪区域
Rect dest=new Rect();//屏幕裁剪区域
//设置图片裁剪区域
src.left=bx;
src.top=by;
src.right=bx+w;
src.bottom=by+h;
//设置屏幕裁剪区域
dest.left=x;
dest.top=y;
dest.right=x+w;
dest.bottom=y+h;
//绘制图片
canvas.drawBitmap(bitmap, src, dest, null);
src=null;
dest=null;
}
/**
* 绘制一个Bitmap
* @param bitmap:要绘制的图片
* @param x:屏幕上的x坐标
* @param y:屏幕上的y坐标
* */
public static void drawImage(Canvas canvas,Bitmap bitmap,int x,int y) {
canvas.drawBitmap(bitmap, x, y, null);
}
}
package com.example.examples_05_07;
import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
public class MainActivity extends Activity {
GameView mgameView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mgameView=new GameView(this);
setContentView(mgameView);
}
public boolean onKeyDown(int keyCode,KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_DPAD_LEFT)
{
if(mgameView.miDTX>0)
{
mgameView.miDTX--;
}
}
else if (keyCode==KeyEvent.KEYCODE_DPAD_RIGHT) {
if(mgameView.miDTX+mgameView.mBitDestTop.getWidth()<320)
{
mgameView.miDTX+=10;
}
}
return true;
}
//触笔事件
public boolean onTouchEvent(MotionEvent event) {
return true;
}
}
BitMap画图,布布扣,bubuko.com
BitMap画图
原文:http://www.cnblogs.com/danmao/p/3807238.html