gpt4 book ai didi

java - 启动位图

转载 作者:行者123 更新时间:2023-11-29 21:56:28 24 4
gpt4 key购买 nike

我有一个绘制我的位图的表面 View 类,我在让一个对象从屏幕的中心底部启动到用户点击屏幕的地方时遇到了一些麻烦,下面是一个代码 fragment onTouch 方法从用户触摸屏幕获取坐标,然后 run 方法绘制位图。岩石位图是我需要从屏幕底部移动到进行触摸的地方。

           public boolean onTouch(View arg0, MotionEvent event) {


switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
sX = event.getX();
sY = event.getY();
fX = fY = dX = dY = sclX = sclY = aniX = aniY = 0;
break;
case MotionEvent.ACTION_UP:
fX = event.getX();
fY = event.getY();
dX = fX - sX;
dY = fY - sY;
sclX = dX / 30;
sclY = dY / 30;

break;
}
return true;
}

//the code that draws and updates the bitmaps on screen
public void run() {
while (running) {
if (!sh.getSurface().isValid())
continue;

canvas = sh.lockCanvas();
canvas.drawRGB(02, 02, 150);
if (x != 0 && y != 0)
canvas.drawBitmap(cross, x - (cross.getWidth() / 2), y- (cross.getHeight() / 2), null);

canvas.drawBitmap(sling,(canvas.getWidth() / 2) - (sling.getHeight() / 2),canvas.getHeight() - sling.getHeight(), null);
if (fX != 0 && fY != 0) {
canvas.drawBitmap(rock, (canvas.getWidth()/2) - aniX,(canvas.getHeight()- sling.getHeight()) - aniY,null);

}

aniX = aniX + sclX;
aniY = aniY + sclY;

sh.unlockCanvasAndPost(canvas);
}

}

最佳答案

您在 onTouch() 中遗漏了一些重要的东西。

public boolean onTouch(View arg0, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
sX = event.getX();
sY = event.getY();
fX = fY = dX = dY = sclX = sclY = aniX = aniY = 0;
break;
case MotionEvent.ACTION_UP:
fX = event.getX();
fY = event.getY();
dX = fX - sX;
dY = fY - sY;
sclX = dX / 30;
sclY = dY / 30;

break;
}
return true;
}

这段代码有逻辑问题。假设您按下 100/100,sXsY 在向下的情况下设置为 100。在向上的情况下,您将在 fXfY 中获得向上坐标,它们也是 100。这导致 dX = fX - sX => dX = 100 - 100。所以你的 sclX 也将是 0。这意味着根本没有动画。

您应该使用屏幕中间作为计算的基础。源是屏幕中心,目标是您的触摸。

关于java - 启动位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13101020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com