1. 程式人生 > >安卓屬性動畫ValueAnimator與ObjectAnimator詳解

安卓屬性動畫ValueAnimator與ObjectAnimator詳解

直接上demo,   用法都在程式的註釋裡了,首先上五渣效果圖,

佈局程式碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.animation.MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/alpha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="漸漸消失" />

        <Button
            android:id="@+id/rotate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旋轉360" />

        <Button
            android:id="@+id/translationX"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="左右移動" />

        <Button
            android:id="@+id/translationY"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="上下移動" />
    </LinearLayout>

    <Button
        android:id="@+id/scaleX"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左右拉伸" />

    <Button
        android:id="@+id/scaleY"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上下拉伸" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="動畫樣例"
        android:textSize="30sp"
        android:textColor="#00EE00" />

</LinearLayout>

主Activity

package com.example.animation;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

	private TextView textview ;
	private Button scaleX ;
	private Button scaleY ;
	private Button alpha ;
	private Button rotate ;
	private Button translationX ;
	private Button translationY ;
	private ValueAnimator va ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.textview = (TextView) findViewById(R.id.textview) ;
		this.alpha = (Button) findViewById(R.id.alpha) ;
		this.rotate = (Button) findViewById(R.id.rotate) ;
		this.translationX = (Button)findViewById(R.id.translationX) ;
		this.translationY = (Button)findViewById(R.id.translationY) ;
		this.scaleX = (Button)findViewById(R.id.scaleX) ;
		this.scaleY  = (Button)findViewById(R.id.scaleY) ;
		this.scaleX.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				scaleX() ;
			}

		});
		
		this.scaleY.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				scaleY();
			}
		});
		this.translationY.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				translation(2) ;
			}
		});
		this.translationX.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				translation(1);
			}
		});
		this.alpha.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				alpha() ;
			}
		});

		this.rotate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				rotate() ;
			}
		});
	}
	/*
	 * 旋轉動畫
	 */
	private void rotate() {
		this.va = ObjectAnimator.ofFloat(textview, "rotation", 0f,360f);   //從0度旋轉到360度
		this.va.setDuration(5000) ;   //設定從0度到360度的旋轉時間
		//this.va.setRepeatCount(5);   重複5次
		//this.va.setRepeatMode(ValueAnimator.REVERSE);  播放完畢直接翻轉播放
		//this.va.setRepeatMode(ValueAnimator.RESTART);  播放完畢直接從頭播放
		this.va.start();
	}

	/*
	 *左右移動 so  up and down  is translationY
	 *flag==1  zuoyou
	 *flag==2 shangxia
	 */
	private void translation(int flag) {
		if(flag == 1)
			this.va = ObjectAnimator.ofFloat(textview, "translationX", 0f,50f,5f);   //left and right
		else this.va = ObjectAnimator.ofFloat(textview, "translationY", 0f,50f,5f);   //left and right
		this.va.setDuration(1500) ;   //設定從0度到360度的旋轉時間
		this.va.setRepeatCount(5);  // 重複5次
		//this.va.setRepeatMode(ValueAnimator.REVERSE);  播放完畢直接翻轉播放
		//this.va.setRepeatMode(ValueAnimator.RESTART);  播放完畢直接從頭播放
		this.va.start();
	}


	/*
	 *上下移動移動 
	 */
	private void translationY() {
		this.va = ObjectAnimator.ofFloat(textview, "translationY", 0f,50f,5f);   //left and right
		this.va.setDuration(1500) ;   //設定從0度到360度的旋轉時間
		this.va.setRepeatCount(5);  // 重複5次
		//this.va.setRepeatMode(ValueAnimator.REVERSE);  播放完畢直接翻轉播放
		//this.va.setRepeatMode(ValueAnimator.RESTART);  播放完畢直接從頭播放
		this.va.start();
	}

	/*
	 * 改變透明度(漸漸消失)
	 */
	private void alpha() {
		this.va = ObjectAnimator.ofFloat(this.textview,"alpha",1f,0f,1f) ; //透明度從1變成0然後變成1,以此類推。。
		this.va.setDuration(5000) ;   //設定變化間隔
		//this.va.setRepeatCount(5);   重複5次
		//this.va.setRepeatMode(ValueAnimator.REVERSE);  播放完畢直接翻轉播放
		//this.va.setRepeatMode(ValueAnimator.RESTART);  播放完畢直接從頭播放
		this.va.start();
	}

	/*
	 * 左右拉伸
	 */
	private void scaleX() {
		this.va = ObjectAnimator.ofFloat(this.textview,"scaleX",1f,5f,1f) ;
		this.va.setDuration(5000) ;
		//this.va.setRepeatCount(5);   重複5次
		//this.va.setRepeatMode(ValueAnimator.REVERSE);  播放完畢直接翻轉播放
		//this.va.setRepeatMode(ValueAnimator.RESTART);  播放完畢直接從頭播放
		this.va.start();

	}
	
	/*
	 * 上下拉伸
	 */
	private void scaleY() {
		this.va = ObjectAnimator.ofFloat(this.textview,"scaleY",1f,5f,1f) ;
		this.va.setDuration(5000) ;
		//this.va.setRepeatCount(5);   重複5次
		//this.va.setRepeatMode(ValueAnimator.REVERSE);  播放完畢直接翻轉播放
		//this.va.setRepeatMode(ValueAnimator.RESTART);  播放完畢直接從頭播放
		this.va.start();

	}
}


相關推薦

屬性動畫ValueAnimatorObjectAnimator

直接上demo,   用法都在程式的註釋裡了,首先上五渣效果圖, 佈局程式碼: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="h

屬性動畫ValueAnimatorObjectAnimator

ValueAnimator的簡單應用/ //初始化,設定初始值和結束 // ValueAnimator valueAnimator=ValueAnimator.ofFloat(0.4f,8.7f,3,5); // valueAnima

屬性動畫詳細解析及自定義

前言 在上一篇中,我們對Android三大動畫的使用做了基本的介紹。那麼本篇就來詳細的說說屬性動畫。 首先我們得了解一下屬性動畫和傳統動畫之間的優缺點。 幀動畫和補間動畫的缺陷: 1.作用物件的侷限性: 補間動畫 只能夠作用在檢視View上,即只可以對

檢視動畫+收縮展開

最近需求 類似支付寶 餘額寶中的一個 檢視 根據動畫 動態收縮和展開    其實很簡單  廢話不多說   上程式碼 頭部紅色佈局  可以理解為三個不同的view   中間佈局 顯示或隱藏動畫   mExpandAnimation = AnimationUtils.loa

屬性動畫 ValueAnimatorObjectAnimator 之間的區別

ValueAnimator  是 對 值的平滑過渡動畫。什麼意思呢。就是對數值在一定時間內進行平滑過渡。 public class MainActivity extends AppCompatActivity { @Override protected v

四大控制元件之BroadcastReceiver

BroadcastReceiver詳解 廣播的概念 Android:系統在產生某個事件時傳送廣播,應用程式使用廣播接收者接收這個廣播,就知道系統產生了什麼事件。 Android系統在執行的過程中,會產生很多事件,比如開機、電量改變、收發簡訊、撥打電話、螢

中的五大布局

原文轉自:http://blog.csdn.net/llping2011/article/details/9992941?utm_source=tuicool&utm_medium=referral 我們知道Android系統應用程式一般是由多個Acti

eclipse+sdk+adk開發環境部署(全程,適合小白)

寫給第一次想在自己Windows上建立eclipse的Android開發環境的朋友們,為了確保大家能順利完成開發環境的搭建,希望對準備進入Android開發的朋友有幫助。 我們知道eclipse+sdk+adk有很多版本的選擇,為了方便剛接觸的朋友 首先我介紹本人的套裝:

搖一搖 包含 objectAnimator valueAnimator動畫效果

star sea cte tty pda eset ttr manager repeat public class MyAnimationDemo extends BaseActivity implements SensorEventListener{ priv

themestyle

   安卓app作為一個有介面的程式,谷歌給我們提供了很多UI控制元件。而控制元件是功能性的,具體的控制元件樣式卻是需要我們自己去控制的。這也就引出了今天話題,安卓的theme與style。說到這個,大家可能都知道theme是主題,也就是整個app的樣式。而style是樣式

網狐榮耀版端的簽名打包

nbsp 生成 輸入 sso keystore png conf oid 分享 第一、打開ADT,選擇file-import,加載榮耀版安卓項目,如下圖 第二,右鍵項目,選擇export,進入下圖界面,選擇android,然後再選擇Export Android App

[js高手之路]原型對象(prototype)原型鏈相關屬性方法

隱式 之前 username tar uname create pro getproto .get 一,instanceof: instanceof檢測左側的__proto__原型鏈上,是否存在右側的prototype原型. 我在之前的兩篇文章 [js高手之路]構造函數的基

第一課:模擬器的介紹應用

pos 模擬器 直接 blog 系統 安卓教程 安卓模擬器 class 腳本 1: 安卓教程 第一:安卓模擬器 。 第二:真機安卓系統。 2: 安卓腳本 第一: 就是外部EXE程序來控制模擬器。 第二: 就是直接用A

一文了APP逆向分析保護機制

dex 也不會 時也 也有 包含 啟動 RM 操作 混亂 “知物由學”是網易雲易盾打造的一個品牌欄目,詞語出自漢·王充《論衡·實知》。人,能力有高下之分,學習才知道事物的道理,而後才有智慧,不去求問就不會知道。“知物由學”希望通過一篇篇技術幹貨、趨勢解讀、人物思考和沈澱給你

Android 開發:(一)開發環境搭建配置 (Windows和Mac )以及目錄結構介紹

(一)、windows版 一. 開發工具: 1.Android Studio:(http://www.androiddevtools.cn/) 2.Genymotion(虛擬機器):(http://www.genymotion.net/) (二)、Mac版 一

佈局 相對佈局網格佈局

筆記: 案例一:相對佈局 相對佈局(重點) 1.1 相對佈局視窗內子元件的位置總是相對兄弟元件、父容器來決定的,因此叫相對佈局 1.2 如果A元件位置是由B元件的位置決定的,Android要求先定B元件,再定義A元件 如果A元件位置是由B元件的位置決定的,Android

入門之ActivityDialog

Activity介紹 安卓中Activity代表頁的意思,也就是☞我們手機上當前的整個介面顯示,點選按鈕等操作可以跳轉到另外一個Activity中。 Activity主要的幾個函式如下:           onCreate( ) :

探索java中json物件json字串之間的關係

  前段時間一直在跟json資料打交道,從一開始的什麼都不懂,到現在整合了許多知識的懵懂,還是收穫了不少對於json格式資料新的看法和安卓資料傳輸的理解。   首先,json資料的格式是什麼樣的呢?我們來定義一個字串型別的json資料。  String json

過渡動畫 ViewAnimationUtils簡單介紹使用

安卓5.0的過渡動畫學習記錄一下。效果如圖 () 1.方法原始碼 public static Animator createCircularReveal(View view, int centerX, int centerY, float startRadius

position屬性absoluterelative

最近一直在研究javascript指令碼,熟悉DOM中CSS樣式的各種定位屬性,以前對這個屬性不太瞭解,從網上找到兩篇文章感覺講得很透徹,收藏下來,唯恐忘記。 一.解讀absolute與relative http://www.blueidea.com/tech/web/2006/4249