本文介绍了动画上一扔事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();
    }
}

这code用于使用手势一扔事件的动画形象。但这里的问题是,它需要时间的动画发生拖动完成后..我想是因为我拖累图像应在同一时间的动画。

This code is used for animating image on fling event using Gestures. but the problem here is that it takes time for animation to happen after the drag is done.. what i want is as i drag down the image should animate at the same time .

推荐答案

建议是使用onTouch代替姿态听者

suggestion is use onTouch instead of gesture listner

写的情况下为MotionEvent.ACTION_DOWN:MotionEvent.ACTION_UP:和MotionEvent.ACTION_MOVE

write cases for MotionEvent.ACTION_DOWN: MotionEvent.ACTION_UP: and MotionEvent.ACTION_MOVE

一旦检测方向的MotionEvent.ACTION_MOVE移动使用view.setTranslationX或view.setTranslationY取决于你想要的方向....休息你想要什么一定拖累后/移动,你可以写在MotionEvent.ACTION_UP可欣赏

Once you detect the direction to move in MotionEvent.ACTION_MOVEuse view.setTranslationX or view.setTranslationY depending on direction you want ....Rest what you want after certain drag/move you can write in MotionEvent.ACTION_UP with view

这只是说明谷歌它为code样品code加载可

This is just instruction google it for sample code loads of code available

这篇关于动画上一扔事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 15:55