Animation中缩放View,中断动画后在低版本手机上背景无法还原的问题;

Animation代码如下

import android.view.animation.Animation;
import android.view.animation.Transformation;

public class MetroAnimation extends Animation {
    private int width;
    private int height;

    public MetroAnimation(int width, int height) {
        this.width = width / 2;
        this.height = height / 2;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        float scale = (float) Math.sqrt(0.85);
        t.getMatrix().postScale(scale, scale, width, height);
    }

}

这个动画的作用是为一个View设置一个点击缩小的效果,放开后应该恢复原状;

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/view"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_centerInParent="true"
        android:background="#A96E5C"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="测试" />
    </LinearLayout>

</RelativeLayout>

Activity(调用动画):

public class MainActivity extends Activity implements View.OnTouchListener {
    private View v;
    private MetroAnimation a;

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

        v = findViewById(R.id.view);
        v.setOnTouchListener(this);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (a == null) {
            a = new MetroAnimation(v.getMeasuredWidth(), v.getMeasuredHeight());
        }

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            a.setDuration(1000 * 60 * 60);
            v.startAnimation(a);
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            // v.clearAnimation();
            // 最初使用这种方法来停止动画使view还原,但是我在低版本Android手机上测试的时候,
            //viewGroup的大小并没有还原,只有里面的内容还原了
            
            //这个方法解决了以上问题
            a.setDuration(0);
        }

        return true;
    }

}

但是这样感觉有点迟钝啊!!!还有更好发办法么???

原文地址:https://www.cnblogs.com/moqi2013/p/3447137.html