在保留比例动画的同时应用抖动动画。

问题描述 投票:1回答:1

我在RecyclerView上应用了一个缩放动画,我在OnFocusChangeListener事件上应用了这个动画。我在OnFocusChangeListener事件上应用这个动画。在应用这个动画之后,我还想在同一个项目上应用一个摇动动画,同时它保留之前的比例。

但在我的例子中,在应用摇动动画之前,项目被缩回到正常大小。

这是我面临的问题。enter image description here

缩放动画 zoom_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true"
    android:interpolator="@android:anim/linear_interpolator"
    android:zAdjustment="top">
    <scale
        android:duration="200"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.2"
        android:toYScale="1.2" />
</set>

抖动动画 shake.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="100"
        android:fromXDelta="-5%"
        android:repeatCount="3"
        android:repeatMode="reverse"
        android:toXDelta="5%" />
</set>

OnFocusChangeListener - 这是我缩放对象的地方。

holder.channelCardView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {

                    Animation animZoomIn = AnimationUtils.loadAnimation(mContext, R.anim.zoom_in);
                    view.startAnimation(animZoomIn);
                } else {
                    Animation animZoomOut = AnimationUtils.loadAnimation(mContext, R.anim.zoom_out);
                    view.startAnimation(animZoomOut);
                }
            }
        });

OnKeyListener - 这是我应用抖动动画的地方。

holder.channelCardView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && getItemCount() == position + 1) {
                    if(getItemCount() == position + 1 && count[0] >= 2) {
                        Animation animShake = AnimationUtils.loadAnimation(mContext, R.anim.shake);
                        holder.channelCardView.startAnimation(animShake);
                    }
                    else {
                        count[0]++;
                    }
                }
                else {
                    count[0] = 0;
                }
                return false;
            }
        });

我想在项目被缩放时应用摇动动画,并且我想保持它的缩放,直到焦点被改变。

感谢你的帮助

android android-animation
1个回答
1
投票

经过大量的阅读,我自己找到了解决方法。通过修改下面的shake.xml,问题就解决了。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillEnabled="true"
    android:fillAfter="true">

    <scale
        android:duration="200"
        android:fromXScale="1.2"
        android:fromYScale="1.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.2"
        android:toYScale="1.2" />
    <translate

        android:duration="100"
        android:fromXDelta="-3%"
        android:repeatCount="3"
        android:repeatMode="restart"
        android:toXDelta="3%" />

</set>
© www.soinside.com 2019 - 2024. All rights reserved.