1、定义 bitmap
public Bitmap viewBg;// 背景图
2、给他图片资源
viewBg = BitmapFactory.decodeResource(res, R.drawable.shuaibao_bg);
3、就是正事了,自己看吧
canvas.save();
Matrix matrix=new Matrix();
int width = viewBg.getWidth();//获取资源位图的宽
int height = viewBg.getHeight();//获取资源位图的高
float w = scalX/viewBg.getWidth();
float h = scalY/viewBg.getHeight();
matrix.postScale(w, h);//获取缩放比例
Bitmap dstbmp = Bitmap.createBitmap(viewBg,0,0,
width,height,matrix,true);//根据缩放比例获取新的位图
canvas.drawBitmap(dstbmp,0,0, mPaint); //在屏幕上画出位图
canvas.restore();
下面是最全面的代码:
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import cn.com.ldci.madc.firecrackers.R;
import cn.com.ldci.madc.firecrackers.ui.shuaibao.ShuaiBaoActivity;
public class ShuaiBaoView extends SurfaceView implements SurfaceHolder.Callback{
ShuaiBaoActivity main = new ShuaiBaoActivity();
//状态
public final static int START = 0;//摔炮开始运动
public final static int PAUSE =1;//软件暂停
public final static int HIT =2;//定义摔炮撞墙是的状态
//监听软件状态
public int state = PAUSE;
//定义图片
public Bitmap imgBao;
public Bitmap imgBaoend;
public Bitmap imgBaobg;
Bitmap dstbmp ;
// private Animation mAnimationTranslate;//定义平移的动画
//定义摔炮位置
public int x;
public int y;
int initX ;
public int initY ;
private float scalX;
private float scalY;
//定义摔炮运行速度
public int speed = 15;
//定义线程的运行参数
public boolean isStop = true;
SurfaceHolder holder;
Intent intent;
Context context;
public ShuaiBaoView(Context context,int x,int y) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
initY = y-30;//初始化摔炮y轴初始坐标
this.x = x/2-5;;//初始化摔炮x轴坐标
this.y = initY;//初始化摔炮Y周卓表
scalX = x;//保存屏幕宽
scalY = y;//保存屏幕高
holder = getHolder();//获取holder
holder.addCallback(this);
Resources res = context.getResources();
imgBao = BitmapFactory.decodeResource(res,R.drawable.shuaibao_bao);
imgBaoend = BitmapFactory.decodeResource(res,R.drawable.shuaipao_baoend);
imgBaobg = BitmapFactory.decodeResource(res,R.drawable.shuaibao_bg);
Matrix matrix=new Matrix();
matrix.postScale(scalX/imgBaobg.getWidth(),
scalY/imgBaobg.getHeight());//获取缩放比例
dstbmp = Bitmap.createBitmap(imgBaobg,0,0,imgBaobg.getWidth(),
imgBaobg.getHeight(),matrix,true);//按缩放比例生成适应屏幕的新的bitmap;
intent = new Intent("com.soundmanagerbao.android.music");
setFocusable(true);
}
public void onDraw(Canvas canvas){
super.onDraw(canvas);
//绘制图片
canvas = holder.lockCanvas(null);//获取画布
Paint mPaint = new Paint();//获取画笔
//画自适应背景
canvas.save();
canvas.drawBitmap(dstbmp, 0, 0, mPaint); //画出图片
canvas.restore();
switch(state){
case PAUSE:{//暂停
canvas.save();
canvas.drawBitmap(imgBao, x, initY,mPaint);
canvas.restore();
}
break;
case START://开始
{
canvas.save();
canvas.drawBitmap(imgBao, x, y,mPaint);
canvas.restore();
y-=speed;
if(y<=120){
state = HIT;
}
}
break;
case HIT:{//撞击
canvas.save();
canvas.drawBitmap(imgBaoend, x, 115,mPaint);
canvas.drawBitmap(imgBaoend, x, 115,mPaint);
canvas.restore();
context.startService(intent);
state = PAUSE;
}
break;
}
holder.unlockCanvasAndPost(canvas);
}
class MyThread implements Runnable{//画图,刷新
@Override
public void run() {
// TODO Auto-generated method stub
while(isStop){
Canvas canvas1 = null;
onDraw(canvas1);
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
isStop = true;
new Thread(new MyThread()).start();//开始线程
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
isStop = false;//返回时关闭线程
}
}