我想在用戶做一個滑動手勢後開始動畫。我有一個課,我設置了動畫和一個正在檢測滑動的類。我的問題是,我不知道如何將它們結合起來 - 我不希望我的動畫在沒有手勢的情況下開始。我是否需要在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,因此動畫正在等待滑動 –