首页 > 代码库 > [2017-8-2]Android Learning Day8

[2017-8-2]Android Learning Day8

自定义动画效果

新建一个customAnim类

 1 package com.liwenchi.myapplication;
 2 
 3 import android.view.animation.Animation;
 4 import android.view.animation.Transformation;
 5 
 6 import static java.lang.Math.*;
 7 
 8 /**
 9  * Created by VULCAN on 2017/8/2.
10  */
11 
12 public class CustomAnim extends Animation {
13 
14     @Override
15     protected void applyTransformation(float interpolatedTime, Transformation t) {
16         //interpolatedTime代表补间动画时间,从0~1,0代表开始,1代表结束
17         //t代表变化对象
18         float p = interpolatedTime;
19         super.applyTransformation(interpolatedTime, t);
20         //System.out.println(interpolatedTime);
21         //t.setAlpha(interpolatedTime);设置透明度
22         //t.getMatrix().setTranslate(0, -300*p);设置位置
23         t.getMatrix().setTranslate((float) (sin(p*2*6.28*p)*50), 0);
24     }
25 }

其中,重写applyTransformation()方法

MainActivity.java里创建一个CustomAnim动画

 1 package com.liwenchi.myapplication;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.view.animation.AlphaAnimation;
 7 import android.view.animation.Animation;
 8 import android.view.animation.AnimationSet;
 9 import android.view.animation.AnimationUtils;
10 import android.view.animation.RotateAnimation;
11 import android.view.animation.ScaleAnimation;
12 import android.view.animation.TranslateAnimation;
13 import android.widget.Button;
14 import android.widget.CheckBox;
15 import android.widget.CompoundButton;
16 import android.widget.TextView;
17 
18 public class MainActivity extends AppCompatActivity {
19 
20     private CustomAnim ca;
21 
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.activity_main);
26         slove();
27     }
28 
29     private void slove() {
30         ca = new CustomAnim();
31         ca.setDuration(1000);
32         findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
33 
34             @Override
35             public void onClick(View v) {
36                 v.startAnimation(ca);
37             }
38         });
39     }
40 }
41 //v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.ta));

这样就自定义了一个“摇头”的动画效果

[2017-8-2]Android Learning Day8