1. 程式人生 > >Android之動畫效果

Android之動畫效果

各種絢麗的動畫效果,為你的App錦上添花吧

首先貼出Activity程式碼:

MainAcivity程式碼:

package com.example.thirdty_animation_android;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {

	private ImageView image;
	private Button scale;
	private Button rotate;
	private Button translate;
	private Button mix;
	private Button alpha;
	private Button continue_btn;
	private Button continue_btn2;
	private Button flash;
	private Button move;
	private Button change;
	private Button layout;
	private Button frame;

	private AnimationDrawable animationDrawable1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		image = (ImageView) findViewById(R.id.image);
		scale = (Button) findViewById(R.id.scale);
		rotate = (Button) findViewById(R.id.rotate);
		translate = (Button) findViewById(R.id.translate);
		alpha = (Button) findViewById(R.id.alpha);
		continue_btn = (Button) findViewById(R.id.continue_btn);
		continue_btn2 = (Button) findViewById(R.id.continue_btn2);
		flash = (Button) findViewById(R.id.flash);
		move = (Button) findViewById(R.id.move);
		change=(Button) findViewById(R.id.change);
		layout=(Button) findViewById(R.id.layout);
		frame=(Button) findViewById(R.id.frame);
		scale.setOnClickListener(this);
		rotate.setOnClickListener(this);
		translate.setOnClickListener(this);
		alpha.setOnClickListener(this);
		continue_btn.setOnClickListener(this);
		continue_btn2.setOnClickListener(this);
		flash.setOnClickListener(this);
		move.setOnClickListener(this);
		change.setOnClickListener(this);
		layout.setOnClickListener(this);
		frame.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		Animation loadAnimation;
		switch (view.getId()) {
		case R.id.scale: {
			loadAnimation = AnimationUtils.loadAnimation(this, R.anim.scale);
			image.startAnimation(loadAnimation);
			break;
		}

		case R.id.rotate: {
			loadAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate);
			image.startAnimation(loadAnimation);
			break;
		}

		case R.id.translate: {

			loadAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);
			image.startAnimation(loadAnimation);
			break;
		}

		case R.id.continue_btn: {
			loadAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);
			image.startAnimation(loadAnimation);
			final Animation loadAnimation2 = AnimationUtils.loadAnimation(this,R.anim.rotate);
			loadAnimation.setAnimationListener(new AnimationListener() {

				//開始
				@Override
				public void onAnimationStart(Animation arg0) {
					// TODO Auto-generated method stub

				}
				//重複
				@Override
				public void onAnimationRepeat(Animation arg0) {
					// TODO Auto-generated method stub

				}
				//結束
				@Override
				public void onAnimationEnd(Animation arg0) {
					// TODO Auto-generated method stub
					image.startAnimation(loadAnimation2);
				}
			});
			break;
		}

		case R.id.continue_btn2: {
			loadAnimation = AnimationUtils.loadAnimation(this,R.anim.continue_anim);
			image.startAnimation(loadAnimation);
			break;
		}

		case R.id.alpha: {		
			loadAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
			image.startAnimation(loadAnimation);
			break;
		}

		case R.id.move: {
			TranslateAnimation translate = new TranslateAnimation(-50, 50,
					0, 0);
			translate.setDuration(1000);
			translate.setRepeatCount(Animation.INFINITE);
			translate.setRepeatMode(Animation.REVERSE);
			image.startAnimation(translate);

			break;
		}

		case R.id.flash: {

			AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
			alphaAnimation.setDuration(100);
			alphaAnimation.setRepeatCount(10);
			//倒序重複REVERSE  正序重複RESTART
			alphaAnimation.setRepeatMode(Animation.REVERSE);
			image.startAnimation(alphaAnimation);

			break;
		}
		
		case R.id.change:
		{
			Intent intent=new Intent(MainActivity.this,MainActivity2.class);
			startActivity(intent);
			overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out);
			break;
		}
		
		case R.id.layout:
		{
			Intent intent=new Intent(MainActivity.this,ListActivity.class);
			startActivity(intent);
			break;
		}
		
		case R.id.frame:
		{
			image.setImageResource(R.drawable.anim_list);
			animationDrawable1 = (AnimationDrawable) image.getDrawable();
			animationDrawable1.start();
			break;
		}
		}
	}

}
跳轉動畫,Activity頁面切換,第二個MainActivity程式碼:
package com.example.thirdty_animation_android;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity2 extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main2);
	}
	
}

ListView動畫演示,需要一個ListActivity佈局,程式碼如下:
package com.example.thirdty_animation_android;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListActivity extends Activity{
	
	private ListView listView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_layout);
		listView=(ListView) findViewById(R.id.listView);
		List<String>list=new ArrayList<String>();
		for(int i=0;i<20;i++)
		{
			list.add("慕課網"+i);
		}
		ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
	    listView.setAdapter(adapter);
	    LayoutAnimationController lac=new LayoutAnimationController(AnimationUtils.loadAnimation(this, R.anim.zoom_in));
	    lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
	    listView.setLayoutAnimation(lac);
	    listView.startLayoutAnimation();
	}

}

接下來是xml檔案,不再以一一敘述,請自己理解:

main.xml對應MainActivity佈局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="vertical" >

        <Button
            android:id="@+id/scale"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:paddingBottom="5dp"
            android:paddingTop="5sp"
            android:text="ScaleAnimation(縮放動畫)" />

        <Button
            android:id="@+id/alpha"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:paddingBottom="5dp"
            android:paddingTop="5sp"
            android:text="AlphaAnimation(透明度動畫)" />

        <Button
            android:id="@+id/rotate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:paddingBottom="5dp"
            android:paddingTop="5sp"
            android:text="RotateAnimation(旋轉動畫)" />

        <Button
            android:id="@+id/translate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:paddingBottom="5dp"
            android:paddingTop="5sp"
            android:text="TranslateAnimation(位移動畫)" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/continue_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="5dp"
                android:paddingBottom="5dp"
                android:paddingTop="5sp"
                android:text="續播1" />

            <Button
                android:id="@+id/continue_btn2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="5dp"
                android:paddingBottom="5dp"
                android:paddingTop="5sp"
                android:text="續播2" />

            <Button
                android:id="@+id/flash"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="5dp"
                android:paddingBottom="5dp"
                android:paddingTop="5sp"
                android:text="閃爍" />

            <Button
                android:id="@+id/move"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="5dp"
                android:paddingBottom="5dp"
                android:paddingTop="5sp"
                android:text="抖動" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/change"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="5dp"
                android:paddingBottom="5dp"
                android:paddingTop="5sp"
                android:text="切換動畫" />

            <Button
                android:id="@+id/layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="5dp"
                android:paddingBottom="5dp"
                android:paddingTop="5sp"
                android:text="佈局動畫" />

            <Button
                android:id="@+id/frame"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="5dp"
                android:paddingBottom="5dp"
                android:paddingTop="5sp"
                android:text="逐幀動畫" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:src="@drawable/anim_list" >
        </ImageView>
    </LinearLayout>

</LinearLayout>

main2.xml對應MainActivity佈局,很簡單,就添加了一張圖片:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/back"
    android:orientation="vertical" >

</LinearLayout>
最後一個是ListView的xml檔案程式碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>



相關推薦

Android動畫效果

各種絢麗的動畫效果,為你的App錦上添花吧 首先貼出Activity程式碼: MainAcivity程式碼: package com.example.thirdty_animation_android; import android.app.Activity; impor

Android開發動畫效果淺析(一)

程式執行效果圖:Android動畫主要包含補間動畫(Tween)View Animation、幀動畫(Frame)Drawable Animation、以及屬性動畫Property Animation。下面依次介紹一下各個動畫。1. 補間動畫(Tween)Tween動畫,通過

Android實現動畫效果淡入淡出

QQ群:372135639 View漸隱動畫效果 /** * View漸隱動畫效果 */ public void setHideAnimation( View view, int duration) {

打地鼠遊戲(3)動畫效果

rand pre 繼續 ood 擴展 child timeout cti java 在第一篇中,我們已經實現了單擊開始後遊戲的倒計時,那麽下面,我們需要繼續在Game這個簡直對對象中進行擴展屬性和方法: 首先我們需要在Game中添加幾組初始鍵值對: //所有的地鼠dom元

android 動畫 (二) 插值器

插值器 首先要了解為什麼需要插值器,因為在補間動畫中,我們一般只定義關鍵幀(首幀或尾幀),然後由系統自動生成中間幀,生成中間幀的這個過程可以成為“插值”。插值器定義了動畫變化的速率,提供不同的函式定義變化值相對於時間的變化規則,可以定義各種各樣的非線性變化函式,比如加速、減速等。下面是幾種常

04.Android動畫問題

目錄介紹 4.0.0.1 Android中有哪幾種類型的動畫,屬性動畫和補間動畫有何區別?補間動畫和屬性動畫常用的有哪些? 4.0.0.2 View動畫為何不能真正改變View的位置?而屬性動畫為何可以?屬性動畫是如何改變View的屬性? 4.0.0.3 補間動畫是如何作用於view的,從原始

Android ObjectAnimator動畫效果實現

一、 ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f).setDuration(1000).start(); 第一個括號引數:圖片控制元件

Android動畫全講-劉志遠-專題視訊課程

Android之動畫全講—7291人已學習 課程介紹        本課程講解了Android中的幀動畫,補間動畫,屬性動畫的動畫實現過程課程收益    讓學員掌握Android中基本的動畫形式講師介紹    劉志遠更多講師課程    劉志遠,北京郵電大學碩士研究生,北京積雲

Android動畫-逐幀動畫

介紹:“逐幀動畫”,就是將多張圖按照播放動畫片那樣,從第一張圖片播放到最後一張。 animation.xml <?xml version="1.0" encoding="utf-8

Android動畫效果Frame Animation(逐幀動畫

想要 顯示 star 載體 rop 復雜 ide sources post 前言: 上一篇介紹了Android的Tween Animation(補間動畫) Android動畫效果之Tween Animation(補間動畫),今天來總結下Android的另外一種動

【我的Android進階旅】自定義控制元件使用ViewPager實現可以預覽的畫廊效果,並且自定義畫面切換的動畫效果的切換時間

我們來看下效果 在這裡,我們實現的是,一個ViewPager來顯示圖片列表。這裡一個頁面,ViewPage展示了前後的預覽,我們讓預覽頁進行Y軸的壓縮,並設定透明度為0.5f,所有我們看到gif最後,左右兩邊的圖片有點朦朧感。讓預覽頁和主頁面有主從感。我們用分

Android 動畫分析翻轉效果

大家一定看到過三維的翻轉效果,所以今天在這裡簡單的給大家分析一下,我們怎麼樣才能實現Android中的翻轉動畫效果的實現,首先看一下執行效果如下圖所示。 效果圖: Android中並沒有提供直接做3D翻轉的動畫,所以關於3D翻轉的動畫效果需要我們自己實現,那麼我們首先

Android酷炫動畫效果進度載入動畫

package com.eftimoff.androipathview; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android

Android動畫效果自定義ViewGroup添加布局動畫

前言:      前面幾篇文章介紹了補間動畫、逐幀動畫、屬性動畫,大部分都是針對View來實現的動畫,那麼該如何為了一個ViewGroup新增動畫呢?今天結合自定義ViewGroup來學習一下佈局動畫。本文將通過對自定義圖片選擇控制元件設定動畫為例來學習佈局動畫。      其他幾種動畫效果:  自定義

Androidapp引導頁(背景圖片切換加各個頁面動畫效果)

轉載:http://blog.csdn.net/lowprofile_coding/article/details/48037095 先看效果圖: 1.顯示三個頁面的Activity  用view pager去載入三個fragment實現,控制點點點的切換,監聽view

Android酷炫動畫效果3D星體旋轉效果

  在Android中,如果想要實現3D動畫效果一般有兩種選擇:一是使用Open GL ES,二是使用Camera。Open GL ES使用起來太過複雜,一般是用於比較高階的3D特效或遊戲,並且這個也不是開源的,像比較簡單的一些3D效果,使用Camera就足夠了。   一些

Android 動畫進階動畫切換

ram 屏幕 pre 退出 tac 旋轉動畫 utf 轉動 XML 一、Activity切換動畫   在Android開發中,經常會遇到Activity之間切換效果,下面介紹一下,Activity左右滑動切換效果。在Android2.0以後版本,在Activity中添加了

Android 常用動畫RotateAnimation

找到 draw 狀態 set 運行 代碼 pla instance from 前兩天接到任務做一個UI,有用到動畫,於是抽空看了下Android動畫相關知識。 Android Animation共有四大類型,分別是 Alpha 透明度動畫 Scale 大

200多種Android動畫效果的強悍框架

lsh ron dmi spl adb 下拉選擇 源代碼 社區 eee admin 發布於2015-10-23 14:33 363/68015 【精品推薦】200多種Android動畫效果的強悍框架,太全了,不看這個,再有動畫的問題,不理你了^@^ 功能模塊

android 仿ppt進入動畫效果合集

ppt 效果 動畫 android 進入 EnterAnimationandroid 仿ppt進入動畫效果合集, 百葉窗效果,擦除效果,盒狀效果,階梯效果,菱形效果,輪子效果,劈裂效果,棋盤效果, 切入效果,扇形展開效果,十字擴展效果,隨機線條效果,向內溶解效果,圓形擴展效果, 適用於各種