package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class MainActivity3 extends AppCompatActivity {
private ImageView img=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
img=findViewById(R.id.fan1);
findViewById(R.id.moveBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ImageView img1 = findViewById(R.id.imgView1);
TranslateAnimation translateAnimation = new TranslateAnimation(0, 1000, 0,0);
translateAnimation.setDuration(2000);
translateAnimation.setFillAfter(false);
translateAnimation.setRepeatCount(1);
img1.startAnimation(translateAnimation);
}
});
findViewById(R.id.RoteBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RotateAnimation rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(1000);
rotate.setRepeatCount(10);
rotate.setFillAfter(false);
img.setAnimation(rotate);
}
});
findViewById(R.id.RcaleBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ImageView img1 = findViewById(R.id.fan5);
ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 0f, 1f, 0.9f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(2000);
img1.startAnimation(scaleAnimation);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
tools:context=".MainActivity3">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imgView1"
android:src="@mipmap/fan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
</ImageView>
<Button
android:id="@+id/moveBtn"
android:text="开启移动动画"
android:layout_marginTop="200dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"></Button>
<Button
android:layout_gravity="center"
android:text="开启缩放动画"
android:layout_marginBottom="200dp"
android:id="@+id/RcaleBtn"
android:layout_marginRight="100dp"
android:layout_marginTop="200dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
</Button>
<ImageView
android:layout_gravity="center"
android:id="@+id/fan5"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="112dp"
android:layout_marginTop="224dp"
android:src="@mipmap/fan"
android:layout_marginBottom="200dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="DuplicateIds,MissingConstraints"></ImageView>
<Button
android:layout_gravity="center"
android:text="开启旋转动画"
android:id="@+id/RoteBtn"
android:layout_marginRight="100dp"
android:layout_marginTop="200dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
</Button>
<ImageView
android:layout_gravity="center"
android:id="@+id/fan1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="112dp"
android:layout_marginTop="224dp"
android:src="@mipmap/fan"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints"></ImageView>
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>