標籤:android 介面 animation 動畫
今天我們來看看Android中的動畫效果,執行個體比較簡單:
- AlphaAnimation:透明度動畫
- ScaleAnimation:縮放動畫
- TranslateAnimation:移動位置動畫
- RotateAnimation:旋轉角度動畫
先貼代碼:
這個執行個體完全使用代碼實現的,當然也可以使用xml檔案實現,我們先來看這個執行個體:
package com.example.demo5_03_animation;import android.os.Bundle;import android.annotation.SuppressLint;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.RotateAnimation;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity {private ImageView mImg;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mImg = (ImageView) findViewById(R.id.img);Button btn_alpha = (Button) findViewById(R.id.btn_alpha);btn_alpha.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Animation animAlpha = new AlphaAnimation(1.0f, 0.0f);animAlpha.setDuration(1000*3);mImg.startAnimation(animAlpha);}});Button btn_scale = (Button) findViewById(R.id.btn_scale);btn_scale.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Animation animScale = new ScaleAnimation(1.0f, 1.3f, 1.0f, 1.3f);animScale.setDuration(1000*3);mImg.startAnimation(animScale);}});Button btn_translate = (Button) findViewById(R.id.btn_translate);btn_translate.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Animation animAlpha = new TranslateAnimation(-mImg.getWidth(), 0, 0, 0);animAlpha.setDuration(1000*1);mImg.startAnimation(animAlpha);}});Button btn_rotate = (Button) findViewById(R.id.btn_rotate);btn_rotate.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Animation animAlpha = new RotateAnimation(0.0f, 90.0f);animAlpha.setDuration(1000*3);mImg.startAnimation(animAlpha);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}也沒有什麼好講的,這裡主要是給大家提供執行個體。
然後我們來看看用xml檔案怎麼實現:1. 使用xml來配置 animation, 首先需要在 res/ 下建立 anim/ 檔案夾, 建立想要的動畫設定檔,比如: res/anim/anim_alpha.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="2000" android:fromAlpha="1.0" android:toAlpha="0.5" /></set>
2. 然後就是在Activity中載入這個動畫就完成了:
Button btn_alpha = (Button) findViewById(R.id.btn_alpha);btn_alpha.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//Animation animAlpha = new AlphaAnimation(1.0f, 0.0f);//animAlpha.setDuration(1000*3);//mImg.startAnimation(animAlpha);Animation animationAlpha = AnimationUtils.loadAnimation(mContext, R.anim.anim_alpha);mImg.startAnimation(animationAlpha);}});這裡是把上一個例子中的透明度動畫注釋了,然後用xml載入方式寫的。
好了,動畫其實使用起來很簡單,其他的請大家自己去研究!!
Android入門筆記 - 介面開發 - Animation