我想在用户做一个滑动手势后开始动画。我有一个课,我设置了动画和一个正在检测滑动的类。我的问题是,我不知道如何将它们结合起来 - 我不希望我的动画在没有手势的情况下开始。我是否需要在GestureDetector的if语句中启动动画方法?如果我需要这样做,我该如何从那里开始动画?android-开始动画
public class MainActivity extends Activity implements OnGestureListener
{
private ImageView imageView;
private BitmapDrawable ball;
float x1,x2;
float y1, y2;
Context mContext = getApplicationContext();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageview1);
}
public boolean onTouchEvent(MotionEvent touchevent)
{
switch (touchevent.getAction())
{
// when user first touches the screen we get x and y coordinate
case MotionEvent.ACTION_DOWN:
{
x1 = touchevent.getX();
y1 = touchevent.getY();
break;
}
case MotionEvent.ACTION_UP:
{
x2 = touchevent.getX();
y2 = touchevent.getY();
//if left to right sweep event on screen
if (x1 < x2 && (x2-x1)>=(y1-y2) && (x2-x1)>=(y2-y1))
{
imageView.animate()
.translationX(-imageView.getWidth()) //in this case Image goes to the left
.setDuration(180) //it's optional
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
})
.start();
Toast.makeText(this, "Left to Right Swap Performed", Toast.LENGTH_LONG).show();
}
// if right to left sweep event on screen
if (x1 > x2 && (x1-x2)>=(y1-y2) && (x1-x2)>=(y2-y1))
{
imageView.animate()
.translationX(imageView.getWidth()) //in this case Image goes to the right
.setDuration(180) //it's optional
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
})
.start();
Toast.makeText(this, "Right to Left Swap Performed", Toast.LENGTH_LONG).show();
}
// if UP to Down sweep event on screen
if (y1 < y2 && (y2-y1)>=(x1-x2) && (y2-y1)>=(x2-x1))
{
Toast.makeText(this, "UP to Down Swap Performed", Toast.LENGTH_LONG).show();
}
//if Down to UP sweep event on screen
if (y1 > y2 && (y1-y2)>=(x1-x2) && (y1-y2)>=(x2-x1))
{
Toast.makeText(this, "Down to UP Swap Performed", Toast.LENGTH_LONG).show();
}
break;
}
}
return false;
}
ViewPropertyAnimator?我可以将它应用到检测类中吗?是的,我想通过刷动画动画 –
你想实现什么样的动画? – nullbyte
我认为这是一个ViewAnimation。动画工作时,图像从右侧中心移动,但动画从我开始应用程序开始,因为我不知道如何将它连接到GestureDetector,因此动画正在等待滑动 –