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中设置好的界面,按键按下事件,在主活动中,没有显示。求解。
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中设置好的界面,按键按下事件,在主活动中,没有显示。求解。
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ( mGameView== null )
{
return false;
}
return mGameView.onKeyDown(keyCode,event);
}
试试看