Android入門筆記 - 介面開發 - Animation

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.