1. 程式人生 > >Android 動畫之屬性動畫

Android 動畫之屬性動畫

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import
android.widget.TextView; public class MainActivity extends Activity { TextView tvTitle; Button btnStart,btnStop; ValueAnimator animation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲得元件
tvTitle=(TextView) findViewById(R.id.tv_title_main); btnStart=(Button) findViewById(R.id.start_anim_main); btnStop=(Button) findViewById(R.id.stop_anim_main); //監聽事件 btnStart.setOnClickListener(new OnClickListener() { @Override public void
onClick(View v) { //一。構造並返回0-255之間的一個變化的ValueAnimator animation=ValueAnimator.ofInt(0,255); //二。設定動畫 的時常 animation.setDuration(3000); //三。Repeat重複,設定重複的次數 animation.setRepeatCount(ValueAnimator.INFINITE); //四。設定重複的模板(當這個動畫到結尾時,應該如何做) animation.setRepeatMode(ValueAnimator.RESTART); //設定動畫更新的監聽事件 animation.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { //五。獲取當前時刻animation的值 Log.i("Tag", ""+animation.getAnimatedValue()); int red=Color.rgb((Integer) animation.getAnimatedValue(), 0, 0); //六 。把獲得的值設定為TextView的顏色值 tvTitle.setTextColor(red); } }); //開始動畫 animation.start(); } }); btnStop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(animation.isRunning()){//如果animation動畫正在執行 animation.end();//結束 } } }); } }
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv_title_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="35sp" />

    <Button
        android:id="@+id/start_anim_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_title_main"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:text="開始屬性動畫" />

    <Button
        android:id="@+id/stop_anim_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/start_anim_main"
        android:layout_centerVertical="true"
        android:text="停止屬性動畫" />
</RelativeLayout>

這裡寫圖片描述
這裡寫圖片描述