【问题标题】:How do i set an onClickListener on a view that gets animated?如何在动画视图上设置 onClickListener?
【发布时间】:2019-01-22 22:27:57
【问题描述】:

我正在尝试创建一个简单的气泡弹出应用程序,气泡从屏幕底部浮动到顶部。我希望用户能够点击这些气泡并让它发出声音。我已经在我的应用程序中加入了声音并设置了onClickListeners(),但是当我单击气泡图像时它不会发出声音。我知道这是由于调用动画方法时调整了图像位置。

我在网上找到了一些答案,但其中大部分对我来说太复杂了,因为我是新手,所以无法实施。

public class MainActivity extends AppCompatActivity {

    private ImageView bubble, bubble1, bubble2, bubble3, bubble4, bubble5, bubble6, bubble7, bubble8;

    //Created an array that stores the images
    private ImageView[] IMGS= new ImageView[9];
    private SoundPlayer sound;


    private int deviceHeight;

    private Animation mAnimation;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sound = new SoundPlayer(this);


        //linked up views
        bubble = findViewById(R.id.bubble);
        bubble1 = findViewById(R.id.bubble1);
        bubble2 = findViewById(R.id.bubble2);
        bubble3 = findViewById(R.id.bubble3);
        bubble4 = findViewById(R.id.bubble4);
        bubble5 = findViewById(R.id.bubble5);
        bubble6 = findViewById(R.id.bubble6);
        bubble7 = findViewById(R.id.bubble7);
        bubble8 = findViewById(R.id.bubble8);

        //assigned the array to the imageviews
        IMGS[0] = bubble;
        IMGS[1] = bubble1;
        IMGS[2] = bubble2;
        IMGS[3] = bubble3;
        IMGS[4] = bubble4;
        IMGS[5] = bubble5;
        IMGS[6] = bubble6;
        IMGS[7] = bubble7;
        IMGS[8] = bubble8;

        getDeviceHeight();
        animateBubble();
    }





    public void animateBubble(){
        //looped thru the array of Imageviews and animated the images stored in it.

        for (ImageView img : IMGS) {
            mAnimation = new TranslateAnimation(0, 0, 0, -deviceHeight);
            mAnimation.setDuration(4000);
            mAnimation.setFillAfter(true);
            bubble.setAnimation(mAnimation);
            bubble.setVisibility(View.VISIBLE);
            img.setAnimation(mAnimation);
            img.setVisibility(View.VISIBLE);

            mAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    sound.playPopSound();


                }

                @Override
                public void onAnimationEnd(Animation animation) {



                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            }


    }


    public void getDeviceHeight(){
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        deviceHeight = displayMetrics.heightPixels;
    }


    public void playSound(View view) {
        sound.playPopSound();
    }
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    您不应该使用 TranslateAnimation,它是一种旧的动画视图方式(可能应该被弃用)。这种类型的动画实际上并不会移动 View,而只会移动 View 的绘制,这就是为什么单击气泡的当前可见位置不起作用的原因。

    相反,您可以使用 API 14 中引入的动画 API。更多详情请点击此处: https://developer.android.com/guide/topics/graphics/prop-animation https://developer.android.com/training/animation/reposition-view

    您的动画的简化版本可以这样编写:

    img.animate().translationY(-deviceHeight).setDuration(4000).start()
    

    【讨论】:

      【解决方案2】:

      正如前面的答案所说,您正在使用值动画器,它仅对图形的坐标值进行动画处理。您应该使用 ObjectAnimation ,它的所有属性都随视图移动。为了让您的情况在开始时播放流行声音,您可以使用

      img.animate().translationY(-deviceHeight).setDuration(4000).withStartAction(new Runnable() {
                      @Override
                      public void run() {
                          sound.playPopSound();
                      }
                  });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多