安卓属性动画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();

	}
}



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值