1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/bt1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="平移" />
<Button
android:id="@+id/bt2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="缩放" />
<Button
android:id="@+id/bt3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="旋转" />
<Button
android:id="@+id/bt4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="透明" />
<Button
android:id="@+id/bt5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="综合" />
</LinearLayout>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
2.MainActivity.java
package com.cwj.anim3;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ImageView iv;
private Button bt1, bt2, bt3, bt4, bt5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 属性动画
iv = (ImageView) findViewById(R.id.iv);
bt1 = (Button) findViewById(R.id.bt1);
bt2 = (Button) findViewById(R.id.bt2);
bt3 = (Button) findViewById(R.id.bt3);
bt4 = (Button) findViewById(R.id.bt4);
bt5 = (Button) findViewById(R.id.bt5);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
bt5.setOnClickListener(this);
iv.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view == bt1) {
// 平移
ObjectAnimator tAnim = ObjectAnimator.ofFloat(iv, "translationX", 0f, 100f, 200f, 300f, 100f, 200f, 500f);
tAnim.setDuration(5 * 1000);
tAnim.start();
} else if (view == bt2) {
// 缩放
ObjectAnimator sAnim = ObjectAnimator.ofFloat(iv, "scaleX", 1f, 2f, 3f, 1f);
sAnim.setDuration(5 * 1000);
sAnim.start();
ObjectAnimator syAnim = ObjectAnimator.ofFloat(iv, "scaleY", 1f, 2f, 3f, 1f);
syAnim.setDuration(5 * 1000);
syAnim.start();
} else if (view == bt3) {
// 旋转
ObjectAnimator rAnim = ObjectAnimator.ofFloat(iv, "rotationX", 0f, 360f);
rAnim.setDuration(5 * 1000);
rAnim.start();
} else if (view == bt4) {
// 透明
ObjectAnimator aAnim = ObjectAnimator.ofFloat(iv, "alpha", 1f, 0.5f, 1f, 0.5f, 0.8f, 0.2f, 0f,1f);
aAnim.setDuration(5 * 1000);
aAnim.start();
} else if (view == bt5) {
// 综合
//平移
ObjectAnimator tAnim = ObjectAnimator.ofFloat(iv, "translationX", 0f, 100f, 200f, 300f, 100f, 200f, 500f);
tAnim.setDuration(5 * 1000);
//旋转
ObjectAnimator rAnim = ObjectAnimator.ofFloat(iv, "rotationX", 0f, 360f);
rAnim.setDuration(5 * 1000);
AnimatorSet animSet = new AnimatorSet();
//平移和旋转一起
animSet.playTogether(tAnim, rAnim);
//开始动画
animSet.start();
//取消动画
// animSet.cancel();
//结束动画
// animSet.end();
//暂停动画
// animSet.pause();
//继续动画
// animSet.resume();
}else if (view == iv){
Toast.makeText(this, "您点击了我!", Toast.LENGTH_SHORT).show();
}
}
}
本文详细介绍了一个使用Android属性动画实现视图平移、缩放、旋转和透明度变化的例子。通过MainActivity中的按钮触发不同类型的动画效果,并展示了如何组合多种动画。
3910

被折叠的 条评论
为什么被折叠?



