我正在尝试制作一个基于位图的菜单。菜单本身应该通过屏幕触摸移动事件来移动,基本上我想拖动视图上的按钮。该按钮还包括碰撞检测,因此每当它们接触时,它们就会彼此反弹。
但我在绘制位图时遇到了一些问题。目前我正在使用一个矩形来缩放我的位图,以适应我设备的窗口。我想要的和当前不能得到的是为了让我的位图移动得更平滑,而不会闪烁。是打开总帐的唯一选择吗?还是我在代码中遗漏了一些重要的东西?
这是在我用于绘制每个按钮的表面视图中,其中MenuButton是保存位图并根据触摸和拖动移动来更新其位置的类。
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
for(MenuButton menuButton : menuButtonSprites) {
menuButton.onDraw(canvas);
}
}我希望位图缩放到每个设备的宽度,为此,我使用一个矩形来使位图适合。
public MenuButton(MenuView v, Bitmap bmp, int yPosition){
this.menuView = v;
this.menuButton = bmp;
this.xMax = v.getWidth();
this.yPosistion = yPosition;
menuButtonRectangle = new Rect(xMin, this.yPosistion-yMin, xMax, this.yPosistion+yMax);
}
public void update(int y){
if(menuButtonPressed)
{
this.yPosistion = y;
menuButtonRectangle.set(xMin, yPosistion-yMin, xMax, yPosistion+yMax);
}
}
public void onDraw(Canvas canvas){
canvas.drawBitmap(menuButton, null, menuButtonRectangle, null);
}我还有一个更新绘图的线程
public void run() {
long ticksPS = 1000 / FPS;
long startTime;
long sleepTime;
Canvas c = null;
while (running) {
startTime = System.currentTimeMillis();
try {
c = view.getHolder().lockCanvas();
synchronized (view.getHolder()) {
view.onDraw(c);
}
}
finally {
if (c != null) {
view.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
try {
if (sleepTime > 0)
sleep(sleepTime);
else
sleep(10);
}
catch (Exception e) {
}
}
}我真的不知道我做错了什么,也不知道为什么我的按钮不能流畅地移动。这是使用canvas的一个缺点,还是我错过了一些真正重要的东西:D?
发布于 2012-03-21 22:12:54
通常,此问题发生在绘制过程中存在同步问题时。这可能是由于较高的帧速率,或者也可能是较低的帧速率。这类问题可以通过双缓冲或调整帧速率来修复。
双缓冲意味着,我们将创建一个屏幕大小的空位图并获取图形对象,而不是直接将Image绘制到主画布上。将所有东西绘制到位图上,然后直接将这个位图绘制到主画布上。
https://stackoverflow.com/questions/9805027
复制相似问题