请教:Matrix 的缩放(Bitmap)

男主角 2011-03-27 02:04:46
package com.Feier.android.Matrix;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.view.KeyEvent;
import android.view.View;

public class GameView extends View implements Runnable{

private Bitmap mBitmapQQ=null;
int mBitmapQQ_Width=0;
int mBitmapQQ_Height=0;
float Angle=0.0f;
Matrix mMatrix=new Matrix();

public GameView(Context context){
super(context);
//装载资源
mBitmapQQ=((BitmapDrawable)getResources().getDrawable(R.drawable.button1)).getBitmap();
mBitmapQQ_Width=mBitmapQQ.getWidth();
mBitmapQQ_Height=mBitmapQQ.getHeight();

new Thread(this).start();
}

public void onDraw(Canvas canvas){
super.onDraw(canvas);

//重置mMatrix
mMatrix.reset();
//设置旋转
mMatrix.setRotate(Angle);
//按mMatrix的旋转构建新的Bitmap
Bitmap mBitmapQQ2=Bitmap.createBitmap(mBitmapQQ,0,0,mBitmapQQ_Width,mBitmapQQ_Height,mMatrix,true);
//绘制旋转之后的图像
GameView.drawImage(canvas,mBitmapQQ2,(320-mBitmapQQ_Width)/2,10);

mBitmapQQ2=null;
}

public boolean onKeyDown(int keyCode,KeyEvent event){
if (keyCode==KeyEvent.KEYCODE_DPAD_LEFT){
Angle--;
}
else if(keyCode==KeyEvent.KEYCODE_DPAD_RIGHT){
Angle++;
}
return true;
}

public void run(){
while(!Thread.currentThread().isInterrupted()){
try{
Thread.sleep(100);
}
catch(Exception e){
Thread.currentThread().interrupt();
}
postInvalidate();
}
}

/**
* 绘制一个Bitmap
* @param canvas画布
* @param btimap画布
* @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.Feier.android.Matrix;

import android.app.Activity;
import android.os.Bundle;

public class Activity01 extends Activity {

private GameView mGameView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mGameView=new GameView(this);
setContentView(mGameView);
//setContentView(R.layout.main);
}
}


请问,代码附上,麻烦大虾看下。
显示是肯定不会出问题。但是,在GameView中设置好的界面,按键按下事件,在主活动中,没有显示。求解。
...全文
316 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lly598985 2011-07-24
  • 打赏
  • 举报
回复
在activity中添加:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ( mGameView== null )
{
return false;
}
return mGameView.onKeyDown(keyCode,event);
}
试试看
yuanzi 2011-07-24
  • 打赏
  • 举报
回复
在按键处理中调用刷新试试 postInvalidate

80,360

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧