0

所以我的问题是我怎样才能制作一个动画在触摸时开始,一旦它没有被触摸它就会播放另一个动画..(对不起我的英语..)

主.java:

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView is = (ImageView) findViewById(R.id.img1);
        is.setBackgroundResource(R.animator.animup);
        final AnimationDrawable animup = (AnimationDrawable) is.getBackground();
        final ImageView iv = (ImageView) findViewById(R.id.img1);
        iv.setBackgroundResource(R.animator.anim);  
        final AnimationDrawable anim = (AnimationDrawable) iv.getBackground();
        is.setOnTouchListener(new OnTouchListener() {



                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_DOWN) { 
                        anim.setVisible(true, true);
                        anim.start();

                        //perform your animation when button is touched and held
                    } else if (event.getAction() == MotionEvent.ACTION_UP) {
                        anim.setVisible(true, true);
                        animup.start();

                        //perform your animation when button is released
                    }
                    return false;




                }
            });

    };

    ;
  }

+另一个问题...... 已编辑

4

1 回答 1

1

我认为这个链接会对你有所帮助。

编辑

要实现触摸和释放,您必须使用onTouchListener而不是onClickListener.

下面是代码-

button.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            //perform your animation when button is touched and held
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
          //perform your animation when button is released
        }
    }
};

编辑 2

else if块中,您没有将可见性设置anim为 false。我觉得这就是问题所在。将您的代码重写为-

button.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            anim.setVisible(true,true);
            anim.start();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
         anim.stop(); //perform your animation when button is released
         animup.setVisible(true,true);
         animup.start();
       }
    }
};
于 2013-09-14T22:05:58.827 回答