Android學習筆記--動畫特效

來源:互聯網
上載者:User

標籤:cte   集中   cli   rri   art   break   app   ext   list   

直接上代碼,有事先不多寫,以後在補

MainActivity

  1 package com.wuxianedu.animation;  2   3 import android.support.annotation.AnimRes;  4 import android.support.v7.app.AppCompatActivity;  5 import android.os.Bundle;  6 import android.view.View;  7 import android.view.animation.AlphaAnimation;  8 import android.view.animation.Animation;  9 import android.view.animation.AnimationSet; 10 import android.view.animation.AnimationUtils; 11 import android.view.animation.RotateAnimation; 12 import android.view.animation.ScaleAnimation; 13 import android.view.animation.TranslateAnimation; 14 import android.widget.CompoundButton; 15 import android.widget.ImageView; 16 import android.widget.Switch; 17  18 public class MainActivity extends AppCompatActivity implements View.OnClickListener { 19  20     private ImageView imgView; 21     private boolean isCheched = true; 22  23     @Override 24     protected void onCreate(Bundle savedInstanceState) { 25         super.onCreate(savedInstanceState); 26         setContentView(R.layout.activity_main); 27  28  29         Switch switch1 = (Switch) findViewById(R.id.switch1); 30         switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 31             @Override 32             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 33                 MainActivity.this.isCheched = isChecked; 34             } 35         }); 36  37  38         findViewById(R.id.button).setOnClickListener(this); 39         findViewById(R.id.button2).setOnClickListener(this); 40         findViewById(R.id.button3).setOnClickListener(this); 41         findViewById(R.id.button4).setOnClickListener(this); 42         findViewById(R.id.button5).setOnClickListener(this); 43         imgView = (ImageView) findViewById(R.id.imageView); 44     } 45  46  47     @Override 48     public void onClick(View v) { 49         switch (v.getId()){ 50             case R.id.button://漸層透明動畫 51                 if(isCheched){ 52                     alphaJava(); 53                 }else{ 54                     loadByXml(R.anim.alpha_animation); 55                 } 56                 break; 57             case R.id.button2://縮放動畫 58 //                ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,0,1); 59 //                scaleJava(); 60                 loadByXml(R.anim.scal_animation); 61                 break; 62             case R.id.button3://位移動畫 63 //                translateJava(); 64                 loadByXml(R.anim.translate_animation); 65                 break; 66             case R.id.button4://旋轉動畫 67 //                rotateJava(); 68                 loadByXml(R.anim.rotate_animation); 69                 break; 70             case R.id.button5://動畫集 71                 AnimationSet animationSet = new AnimationSet(true); 72  73                 //建立三個動畫 74                 RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f, 75                         Animation.RELATIVE_TO_SELF,0.5f); 76                 ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f, 77                         Animation.RELATIVE_TO_SELF,0.5f); 78                 TranslateAnimation translateAnimation = new TranslateAnimation(0,800,0,800); 79  80                 //將動畫添加到動畫集中 81                 animationSet.addAnimation(rotateAnimation); 82                 animationSet.addAnimation(scaleAnimation); 83                 animationSet.addAnimation(translateAnimation); 84  85                 //設定時間長度 86                 animationSet.setDuration(3000); 87  88                 //啟動動畫 89                 imgView.startAnimation(animationSet); 90                 break; 91         } 92     } 93  94     /** 95      * 位移動畫 96      */ 97     private void translateJava() { 98         TranslateAnimation translateAnimation = new TranslateAnimation(0,800,0,800); 99         translateAnimation.setDuration(3000);100         imgView.startAnimation(translateAnimation);101     }102 103     /**104      * 旋轉動畫105      */106     private void rotateJava() {107         RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,108                 Animation.RELATIVE_TO_SELF,0.5f);109         rotateAnimation.setDuration(3000);110         imgView.startAnimation(rotateAnimation);111     }112 113     /**114      * 縮放動畫115      */116     private void scaleJava() {117         ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,118                 Animation.RELATIVE_TO_SELF,0.5f);119         scaleAnimation.setDuration(3000);120         imgView.startAnimation(scaleAnimation);121     }122 123     /**124      * 通過XMl載入動畫125      * @param resId 動畫資源,布局檔案126      */127     private void loadByXml(@AnimRes int resId) {128         Animation animation = AnimationUtils.loadAnimation(this,resId);129         imgView.startAnimation(animation);130     }131 132     /**133      * 漸層透明動畫134      */135     private void alphaJava(){136         AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);137         alphaAnimation.setDuration(3000);138         imgView.startAnimation(alphaAnimation);139     }140 }

 

Android學習筆記--動畫特效

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.