安卓常用的动画
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>