import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;public class ThunderView extends View implements OnKeyListener{
private int screenwidth;
private int screenheight;
private Bitmap plane;
private Bitmap background;
public int startY;
public int startX;
final int RIGHT = 1;
final int LEFT = 2;
public ThunderView(Context context, int width,int height) {
super(context);
setFocusable(true);
this.screenheight=height;
this.screenwidth=width;
plane = BitmapFactory.decodeResource(getResources(), R.drawable.plane);
background = BitmapFactory.decodeResource(getResources(), R.drawable.back_img);
startY = 1700;
startX=screenwidth/2-plane.getWidth()/2;
final Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
if (msg.what == 0x123)
{
//重新开始移动
if (startY <= 0)
{
startY =1700;
}
else
{
startY -= 3;
}
}
invalidate();
}
};
new Timer().schedule(new TimerTask()
{
@Override
public void run()
{
handler.sendEmptyMessage(0x123);
}
}, 0 , 50);
}

protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Bitmap backbitmap = Bitmap.createBitmap(background, 0, startY, screenwidth, screenheight);
canvas.drawBitmap(backbitmap, 0, 0, null);
canvas.drawBitmap(plane, startX, screenheight*7/8-plane.getHeight(), null);
}
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch(event.getKeyCode()){
case KeyEvent.KEYCODE_DPAD_LEFT:
startX-=4;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
startX+=4;
break;
}
invalidate();
return true;
}
}
这是我写的一个view,结果运行时点左右键均无反应求赐教