【问题标题】:Animating on the Fling event在 Fling 事件上制作动画
【发布时间】:2014-04-01 18:54:14
【问题描述】:
package com.example.flingtry;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnClickListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity implements OnClickListener 
{
    private static final int SWIPE_MIN_DISTANCE = 10;
    private static final int SWIPE_MAX_OFF_PATH = 50;
    private static final int SWIPE_THRESHOLD_VELOCITY = 10;
    private GestureDetector gestureDetector;
    TextView img;
    AnimationDrawable ribinclickanimation;
    Button btn1;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gestureDetector = new GestureDetector(new MyGestureDetector());
        img= (TextView) findViewById(R.id.img);
        img.setBackgroundResource(R.anim.clickframeanimation);
        ribinclickanimation= (AnimationDrawable) img.getBackground();
        btn1= (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(this);
        // Set the touch listener for the main view to be our custom gesture listener
        img.setOnTouchListener(new View.OnTouchListener() 
        {
            public boolean onTouch(View v, MotionEvent event) 
            {
                if (gestureDetector.onTouchEvent(event)) 
                {
                    return true;
                }
                return false;
            }
        });
    }
    class MyGestureDetector extends SimpleOnGestureListener 
    {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
        {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) 
            {
                ribinclickanimation.stop();
                Toast.makeText(getApplicationContext(), "Helloooo", Toast.LENGTH_SHORT).show();
                img.setBackgroundResource(R.anim.clickframeanimation);
                ribinclickanimation= (AnimationDrawable) img.getBackground();
                ribinclickanimation.start();
                return false;
            }
            return false;
        }

        // It is necessary to return true from onDown for the onFling event to register
        @Override
        public boolean onDown(MotionEvent e) 
        {
                return true;
        }
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) 
        {
            // beware, it can scroll to infinity
            return true;
        }
    }
    public void onClick(View arg0) 
    {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Helloooo", Toast.LENGTH_SHORT).show(); 
    }
}

此代码用于使用手势在 fling 事件上为图像设置动画。但这里的问题是,在拖动完成后动画需要时间。我想要的是当我向下拖动时,图像应该同时动画。

【问题讨论】:

  • 您希望动画在您仍在拖动时工作吗?
  • 是的..否则它的工作完美。只要我拖动它动画
  • 然后您可以在 onScroll 方法中插入代码,该方法在您滚动时被调用。只有在投掷动作完成时才会调用投掷。
  • 看看这个link。我认为它描述了您想要实现的目标。
  • @13hsoj 什么都没发生……它的效果与 fling 的效果相同。!

标签: android android-animation android-gesture


【解决方案1】:

建议使用 onTouch 代替手势监听器

为 MotionEvent.ACTION_DOWN 编写案例:MotionEvent.ACTION_UP: 和 MotionEvent.ACTION_MOVE

一旦在 MotionEvent.ACTION_MOVE 中检测到移动方向 根据您想要的方向使用 view.setTranslationX 或 view.setTranslationY .... 在某些拖动/移动后休息你想要的东西,你可以在 MotionEvent.ACTION_UP 中用视图编写

这只是 google 的说明,以获取可用代码的示例代码加载

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    相关资源
    最近更新 更多