安卓属性动画PropertyValuesHolder、ObjectAnimator

第一个博客文章,看到安卓一个神奇动画效果,属性动画,摆脱了以往繁琐的动画实现 代码

translationX and translationY

rotation, rotationX, and rotationY

scaleX and scaleY

pivotX and pivotY

x and y

alpha

主要是上诉几种代码效果,直接贴代码吧:

package com.example.test;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private ImageView iv;
	private float mScreenHeight;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		iv = (ImageView) findViewById(R.id.iv);
		DisplayMetrics outMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
		mScreenHeight = outMetrics.heightPixels;
	}

	public void click(View view) {

		// 平移动画
		ObjectAnimator transX = ObjectAnimator.ofFloat(iv, "translationX", 0f,
				300f);
		transX.setDuration(2000);
		transX.setRepeatCount(5);
		transX.setRepeatMode(ObjectAnimator.RESTART);
		transX.start();

		// 平移动画
		ObjectAnimator transY = ObjectAnimator.ofFloat(iv, "translationY", 0f,
				300f);
		transY.setDuration(1000);
		transY.setRepeatCount(3);
		transY.setRepeatMode(ObjectAnimator.RESTART);
		transY.start();

		// 旋转动画
		ObjectAnimator rotation = ObjectAnimator.ofFloat(iv, "rotation", 0f,
				360f);
		rotation.setDuration(1000);
		rotation.setRepeatCount(7);
		rotation.setRepeatMode(ObjectAnimator.RESTART);
		rotation.start();

		// 缩放
		ObjectAnimator scaleX = ObjectAnimator.ofFloat(iv, "scaleX", 0f, 1.6f);
		scaleX.setDuration(1000);
		scaleX.setRepeatCount(5);
		scaleX.setRepeatMode(ObjectAnimator.RESTART);
		scaleX.start();

		// 缩放动画
		ObjectAnimator scaleY = ObjectAnimator.ofFloat(iv, "scaleY", 0f, 1.6f);
		scaleY.setDuration(1000);
		scaleY.setRepeatCount(5);
		scaleY.setRepeatMode(ObjectAnimator.RESTART);
		scaleY.start();

		// 缩放动画
		ObjectAnimator alpha = ObjectAnimator.ofFloat(iv, "alpha", 0f, 1f);
		alpha.setDuration(1000);
		alpha.setRepeatCount(5);
		alpha.setRepeatMode(ObjectAnimator.RESTART);
		alpha.start();

		transX.addListener(new AnimatorListener() {

			@Override
			public void onAnimationStart(Animator animation) {

			}

			@Override
			public void onAnimationRepeat(Animator animation) {

			}

			@Override
			public void onAnimationEnd(Animator animation) {
				Toast.makeText(MainActivity.this, "终于结束了...", Toast.LENGTH_LONG)
						.show();
			}

			@Override
			public void onAnimationCancel(Animator animation) {

			}
		});
	}

	// 属性动画
	public void click1(View view) {
		PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,
				0f, 1f);
		// 属性动画Y值
		PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 0,
				mScreenHeight / 2, 0);
		// 属性动画X值
		PropertyValuesHolder pvhXx = PropertyValuesHolder.ofFloat("x", 0,
				mScreenHeight / 2, 0);
		ObjectAnimator.ofPropertyValuesHolder(iv, pvhX, pvhY, pvhXx)
				.setDuration(1000).start();
	}

	@Override
	public 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;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}

界面代码:

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

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true" >

        <Button
            android:id="@+id/but"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="动画" />

        <Button
            android:id="@+id/but1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="click1"
            android:text="动画2" />
    </LinearLayout>

</RelativeLayout>


争取每天写一个博客,分享知识给广大的安卓爱好者一起学习


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值