gpt4 book ai didi

java - android - 滑动时动画

转载 作者:行者123 更新时间:2023-12-01 09:13:57 24 4
gpt4 key购买 nike

在我的新程序中,我需要可以滑到一边的对象。我已经有了我的动画,它正在工作,我尝试检测另一个类中用户的滑动。我的问题是我不知道如何连接它们。当正确识别滑动手势时,应该启动特定的动画。

我的动画 View 类:

private Runnable r = new Runnable() {
@Override
public void run() {
if(continueAnimation) {
invalidate();
}
}
};


protected void onDraw(Canvas c) {

if (x<0) {
x = this.getWidth()/2-100;
y = this.getHeight()/2-100;
}

else {
x += xVelocity;

if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
boolean continueAnimation = false;
}
}

c.drawBitmap(ball.getBitmap(), x, y, null);

if(continueAnimation)
{
h.postDelayed(r, FRAME_RATE);
}

else {
x = this.getWidth()-ball.getBitmap().getWidth();
}

}

我的SwipeTouchListener:

    public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;

public OnSwipeTouchListener (Context ctx){
gestureDetector = new GestureDetector(ctx, new GestureListener());
}

@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}

private final class GestureListener extends SimpleOnGestureListener {

private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;

@Override
public boolean onDown(MotionEvent e) {
return true;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
result = true;
}
else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
result = true;

} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
}

最佳答案

您可以像这样将 GestureDetector 添加到您的 View 类中,并替换 onFling() 中的代码

public class AnimatedViewClass extends View {

GestureDetector gestureDetector;

public AnimatedViewClass(Context context) {
super(context);
gestureDetector = new GestureDetector(getContext(), new GestureDetectorListener());
}

@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}

private void onSwipeRight(){
// swipe right detected
// do stuff
invalidate();
}


private void onSwipeLeft(){
// swipe left detected
// do stuff
invalidate();
}

private class GestureDetectorListener extends
GestureDetector.SimpleOnGestureListener {

private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

// onSwipeRight()
// onSwipeLeft()

return super.onFling(e1, e2, velocityX, velocityY);
}

@Override
public boolean onDown(MotionEvent e) {
return super.onDown(e);
}
}


private Runnable r = new Runnable() {
@Override
public void run() {
if(continueAnimation) {
invalidate();
}
}
};


protected void onDraw(Canvas c) {

if (x < 0) {
x = this.getWidth() / 2 - 100;
y = this.getHeight() / 2 - 100;
} else {
x += xVelocity;

if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
boolean continueAnimation = false;
}
}

c.drawBitmap(ball.getBitmap(), x, y, null);

if (continueAnimation) {
h.postDelayed(r, FRAME_RATE);
} else {
x = this.getWidth() - ball.getBitmap().getWidth();
}

}


}

关于java - android - 滑动时动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40718063/

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