Applying shake animation while retaining the scale animation

半腔热情 提交于 2020-07-09 19:57:28

问题


I am applying a scale animation on RecyclerView. I am applying this animation on OnFocusChangeListener event. After this animation is applied, I also want to apply a shake animation to the same item while it retains the previous scale.

but in my case the item is scaled back to normal size before applying the shake animation.

Here is the problem I face.

scale animation 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 animation 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 - This is where I scale the object

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 - This is where I apply shake animation.

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

I want to apply the shake animation while the item is zoomed and I want to keep it zoomed until focus is changed.

Appreciate your help


回答1:


After lot of reading I found the solution myself. by changing the shake.xml as below the problem is solved.

<?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>


来源:https://stackoverflow.com/questions/61971180/applying-shake-animation-while-retaining-the-scale-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!