请教OnKeyListener触发间隔 在View上添加了planeView.setOnKeyListener监听,当按下左键时,控制view中的一个组件向左移动。测试时发现一直按着左键时,第一次触发事件和第二次触发事件的时间间隔较长,第二次以后就很短。导致组件移动不平滑,请问如何解决该问题。谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 部分代码PlanView:[code]public class PlaneView extends View { public float currentX; public float currentY; Bitmap plane; private int speed=12; public PlaneView(Context context) { super(context); plane = BitmapFactory.decodeResource(context.getResources(), R.drawable.plane); setFocusable(true); } @Override public void onDraw(Canvas canvas){ super.onDraw(canvas); Paint p = new Paint(); canvas.drawBitmap(plane, currentX, currentY,p); } }[/code] MainActivity:final PlaneView planeView = new PlaneView(this); setContentView(planeView); planeView.setBackgroundResource(R.drawable.back); WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); //获得屏幕宽和高 int screenWidth = display.getWidth(); int screenHeight = display.getHeight(); //设置飞机的初始位置 planeView.currentX = screenWidth / 2; planeView.currentY = screenHeight - 40; planeView.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { switch (event.getKeyCode()) { case KeyEvent.KEYCODE_DPAD_DOWN: planeView.currentY += speed; break; case KeyEvent.KEYCODE_DPAD_UP: planeView.currentY -= speed; break; case KeyEvent.KEYCODE_DPAD_LEFT: planeView.currentX -= speed; break; case KeyEvent.KEYCODE_DPAD_RIGHT: planeView.currentX += speed; break; } planeView.invalidate(); return true; } }); 全屏下如何实现屏蔽home键? 求一个android http上传文件到servlet,tomcat服务器demo,能运行的 android中怎么获得listview中Button点击行的值 android 直接用webview打开.swf android移植libpcap的编译错误 android 截屏问题 android类richtextbox 的控件 html5上传文件的问题 facebook好友列表无法显示 大家有没有遇到Android5.0系统的notification问题? 求解:关于findViewById获取Button对象有空指针问题 做过android串口通信的来看一下!
PlanView:
[code]
public class PlaneView extends View {
public float currentX;
public float currentY;
Bitmap plane;
private int speed=12;
public PlaneView(Context context) {
super(context);
plane = BitmapFactory.decodeResource(context.getResources(), R.drawable.plane);
setFocusable(true);
} @Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
Paint p = new Paint();
canvas.drawBitmap(plane, currentX, currentY,p);
}
}
[/code]
setContentView(planeView);
planeView.setBackgroundResource(R.drawable.back);
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
//获得屏幕宽和高
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
//设置飞机的初始位置
planeView.currentX = screenWidth / 2;
planeView.currentY = screenHeight - 40;
planeView.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (event.getKeyCode())
{
case KeyEvent.KEYCODE_DPAD_DOWN:
planeView.currentY += speed;
break;
case KeyEvent.KEYCODE_DPAD_UP:
planeView.currentY -= speed;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
planeView.currentX -= speed;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
planeView.currentX += speed;
break;
}
planeView.invalidate();
return true;
}
});