zoukankan      html  css  js  c++  java
  • 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;
        }
    
    }

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

  • 相关阅读:
    在WCF中使用Flag Enumerations
    WCF开发教程资源收集
    [转]WCF 4 安全性和 WIF 简介
    Asp.Net Web API 2 官网菜鸟学习系列导航[持续更新中]
    Asp.Net Web API 2第十八课——Working with Entity Relations in OData
    Asp.Net Web API 2第十七课——Creating an OData Endpoint in ASP.NET Web API 2(OData终结点)
    Asp.Net Web API 2第十六课——Parameter Binding in ASP.NET Web API(参数绑定)
    Asp.Net Web API 2第十五课——Model Validation(模型验证)
    函数 生成器 生成器表达式
    函数的进阶
  • 原文地址:https://www.cnblogs.com/moqi2013/p/3447137.html
Copyright © 2011-2022 走看看