安卓-动画

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);


                //fromxDelta 开启时离当前x坐标的距离
                //toxDelta 结束时离当前x坐标的距离
                //fromyDelta 开启时离当前y坐标的距离
                //toyDelta 结束时离当前y坐标的距离
                TranslateAnimation translateAnimation = new TranslateAnimation(0, 1000, 0,0);
                translateAnimation.setDuration(2000); //动画执行时间
                translateAnimation.setFillAfter(false); //动画结束时是否停留在当前位置
                translateAnimation.setRepeatCount(1);//重复次数 -1 一直重复
                img1.startAnimation(translateAnimation); //开启动画
            }
        });


        findViewById(R.id.RoteBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //Animation.RELATIVE_TO_SELF(相对于自身)、Animation.RELATIVE_TO_PARENT(相对于父控件(容器 
                // 开始 ,结束,  
                RotateAnimation rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                rotate.setDuration(1000);//动画执行时间
                rotate.setRepeatCount(10);//重复次数 -1 一直重复
                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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值