|
| 1 | +package com.clock.study.activity; |
| 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.RotateAnimation; |
| 10 | +import android.view.animation.ScaleAnimation; |
| 11 | +import android.view.animation.TranslateAnimation; |
| 12 | +import android.widget.Button; |
| 13 | + |
| 14 | +import com.clock.study.R; |
| 15 | + |
| 16 | +/** |
| 17 | + * Android动画效果实现复习 |
| 18 | + */ |
| 19 | +public class AndroidAnimActivity extends AppCompatActivity implements View.OnClickListener { |
| 20 | + |
| 21 | + private Button mBtnTranslate; |
| 22 | + private Button mBtnScale; |
| 23 | + private Button mBtnRotate; |
| 24 | + private Button mBtnAlpha; |
| 25 | + private Button mBtnSet; |
| 26 | + |
| 27 | + @Override |
| 28 | + protected void onCreate(Bundle savedInstanceState) { |
| 29 | + super.onCreate(savedInstanceState); |
| 30 | + setContentView(R.layout.activity_android_anim); |
| 31 | + |
| 32 | + mBtnTranslate = (Button) findViewById(R.id.btn_translate); |
| 33 | + mBtnTranslate.setOnClickListener(this); |
| 34 | + |
| 35 | + mBtnScale = (Button) findViewById(R.id.btn_scale); |
| 36 | + mBtnScale.setOnClickListener(this); |
| 37 | + |
| 38 | + mBtnRotate = (Button) findViewById(R.id.btn_rotate); |
| 39 | + mBtnRotate.setOnClickListener(this); |
| 40 | + |
| 41 | + mBtnAlpha = (Button) findViewById(R.id.btn_alpha); |
| 42 | + mBtnAlpha.setOnClickListener(this); |
| 43 | + |
| 44 | + mBtnSet = (Button) findViewById(R.id.btn_set); |
| 45 | + mBtnSet.setOnClickListener(this); |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void onClick(View v) { |
| 51 | + int viewId = v.getId(); |
| 52 | + if (viewId == R.id.btn_translate) {//偏移动画 |
| 53 | + //TranslateAnimation translateAnim = new TranslateAnimation(0, 500, 0, 500); |
| 54 | + /*TranslateAnimation translateAnim = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 500, Animation.ABSOLUTE, 0, |
| 55 | + Animation.ABSOLUTE, 500);*/ |
| 56 | + TranslateAnimation translateAnim = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0, |
| 57 | + Animation.RELATIVE_TO_PARENT, 1.0f); |
| 58 | + translateAnim.setDuration(2000); |
| 59 | + mBtnTranslate.startAnimation(translateAnim); |
| 60 | + //translateAnim.setFillAfter(true);//保持动画效果 |
| 61 | + |
| 62 | + } else if (viewId == R.id.btn_scale) {//缩放动画 |
| 63 | + |
| 64 | + //ScaleAnimation scaleAnim = new ScaleAnimation(0.5f, 1, 0.5f, 1); |
| 65 | + //ScaleAnimation scaleAnim = new ScaleAnimation(0.5f, 1, 0.5f, 1, 300, 300); |
| 66 | + ScaleAnimation scaleAnim = new ScaleAnimation(0.5f, 1, 0.5f, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); |
| 67 | + scaleAnim.setDuration(1000); |
| 68 | + mBtnScale.startAnimation(scaleAnim); |
| 69 | + |
| 70 | + } else if (viewId == R.id.btn_rotate) {//旋转动画 |
| 71 | + |
| 72 | + //RotateAnimation rotateAnim = new RotateAnimation(0, 360); |
| 73 | + //RotateAnimation rotateAnim = new RotateAnimation(0, 360, 100, 100); |
| 74 | + RotateAnimation rotateAnim = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); |
| 75 | + rotateAnim.setDuration(2000); |
| 76 | + mBtnRotate.startAnimation(rotateAnim); |
| 77 | + |
| 78 | + } else if (viewId == R.id.btn_alpha) {//透明度动画 |
| 79 | + |
| 80 | + AlphaAnimation alphaAnim = new AlphaAnimation(0, 1); |
| 81 | + alphaAnim.setDuration(2000); |
| 82 | + mBtnAlpha.startAnimation(alphaAnim); |
| 83 | + |
| 84 | + } else if (viewId == R.id.btn_set) {//动画合集 |
| 85 | + |
| 86 | + AnimationSet animSet = new AnimationSet(true); |
| 87 | + animSet.setDuration(2000); |
| 88 | + RotateAnimation rotateAnim = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); |
| 89 | + animSet.addAnimation(rotateAnim); |
| 90 | + AlphaAnimation alphaAnim = new AlphaAnimation(0, 1); |
| 91 | + animSet.addAnimation(alphaAnim); |
| 92 | + mBtnSet.startAnimation(animSet); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | +} |
0 commit comments