activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context="com.example.myapplication.MainActivity"> <Button android:onClick="weiyi" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="位移"/> <Button android:onClick="xuanzhuan" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="旋转"/> <Button android:onClick="jianbian" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="渐变"/> <Button android:onClick="suofang" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="缩放"/> <Button android:onClick="jihe" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="集合"/> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_margin="20dp" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@mipmap/n"/> </LinearLayout>
MainActivity
package com.example.myapplication; import android.animation.ObjectAnimator; import android.animation.PropertyValuesHolder; import android.animation.ValueAnimator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.animation.RotateAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private ImageView iv; private ObjectAnimator animator; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { iv = findViewById(R.id.iv); } /** * 位移 * @param view */ public void weiyi(View view) { animator = ObjectAnimator.ofFloat(iv,view.TRANSLATION_X,0,200f,0,0); animator.setDuration(3000).start(); } /** * 旋转 * @param view */ public void xuanzhuan(View view) { animator = ObjectAnimator.ofFloat(iv,view.ROTATION_Y,0,360f); animator.setDuration(3000).start(); } /** * 渐变 * @param view */ public void jianbian(View view) { animator = ObjectAnimator.ofFloat(iv,view.ALPHA,0,1f,0,1f); animator.setDuration(3000).start(); } /** * 缩放 * @param view */ public void suofang(View view) { animator = ObjectAnimator.ofFloat(iv,view.SCALE_Y,0,2f,0,1f); animator.setDuration(3000).start(); } /** * 集合动画 * @param view */ public void jihe(View view) { //组合动画 PropertyValuesHolder trans=PropertyValuesHolder.ofFloat(view.TRANSLATION_X,0,200f); PropertyValuesHolder rotate=PropertyValuesHolder.ofFloat(view.ROTATION_Y,0,360f); PropertyValuesHolder alpha=PropertyValuesHolder.ofFloat(view.ALPHA,0,1f,0,1f); PropertyValuesHolder scale=PropertyValuesHolder.ofFloat(view.SCALE_Y,0,2f,0,1f); //给图片添加多动画 ObjectAnimator animator= ObjectAnimator.ofPropertyValuesHolder(iv,trans,rotate,alpha,scale); animator.setDuration(3000).start(); } }