【例6-3】 属性动画示例
编写一个旋转、缩放、淡入淡出的属性动画程序。先将图片an.jpg复制到drawable目录。

教学视频
布局文件objectanimator.xml的源代码
<?xml version="1.0" encoding="utf-8"?>
<!--【例6-3】编写一个可以旋转、缩放、淡入淡出的属性动画程序。-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="属性动画演示"
android:id="@+id/textView"
android:textSize="28sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView"
android:layout_gravity="bottom"
android:text="旋转"
android:textSize="18sp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button2"
android:layout_gravity="bottom"
android:text="缩放"
android:textSize="18sp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_gravity="bottom"
android:text="淡入淡出"
android:textSize="18sp" />
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/imageView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:layout_gravity="center_horizontal"
android:src="@drawable/an" />
</LinearLayout>
</RelativeLayout>
控制文件ObAnimatorActivity.java的源代码如下:
package com.example.chap06;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class ObAnimatorActivity extends AppCompatActivity {
Button rotateButton,alphaButton,scaleButton;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.objectanimator);
img = (ImageView)findViewById(R.id.imageView);
rotateButton = (Button)findViewById(R.id.button1);
alphaButton = (Button)findViewById(R.id.button2);
scaleButton = (Button)findViewById(R.id.button3);
rotateButton.setOnClickListener(new mClick());
alphaButton.setOnClickListener(new mClick());
scaleButton.setOnClickListener(new mClick());
}
public class mClick implements View.OnClickListener
{
@Override
public void onClick(View v) {
if(v == rotateButton) { //旋转
ObjectAnimator animator = ObjectAnimator.ofFloat(img, "rotation", 0.0F, 360.0F);
animator.setDuration(1000);
animator.start();
}
else if(v == alphaButton){ //淡入/淡出
ObjectAnimator animator = ObjectAnimator.ofFloat(img, "alpha",1.0F, 0.0F, 1.0F);
animator.setDuration(3000);
animator.start();
}
else if(v == scaleButton){ //缩放
ObjectAnimator animator = ObjectAnimator.ofFloat(img, "ScaleY", 1.0F, 0.5F, 1.0F);
animator.setDuration(5000);
animator.start();
}
}
}
}
打开项目配置文件AndroidManifest.xml,配置要启动的Activity类名
<activity android:name=".ObAnimatorActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

