Android移动开发—属性动画

Android移动开发—属性动画

 

——木梓婕

1、题目要求:

使用属性动画,分别实现以下效果:

(1)使图片先顺时针旋转再逆时针旋转

(2)使图片围绕X轴旋转

(3)使图片可以横向放大2倍再还原

(4)使文字可以飞入

2、效果图:

Android移动开发—属性动画

3、XML代码:

<?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=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="167dp"
        android:layout_height="149dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2"
        app:srcCompat="@mipmap/ic_launcher" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="c1"
        android:text="(1)"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="c2"
        android:text="(2)"
        app:layout_constraintEnd_toStartOf="@+id/button3"
        app:layout_constraintStart_toEndOf="@+id/button1"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="c3"
        android:text="(3)"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="82dp"
        android:textSize="30dp"
        android:text=" 好好学习"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:onClick="c4"
        android:text="(4)"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>

4、Java代码:

package com.example.myapplication3_2;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    ImageView img;
    TextView tv;
    Button b1,b2,b3,b4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img=findViewById(R.id.imageView);
        tv=findViewById(R.id.textView);
        b1=findViewById(R.id.button1);
        b2=findViewById(R.id.button2);
        b3=findViewById(R.id.button3);
        b4=findViewById(R.id.button4);
    }
    public void c1(View v)
    {
        ObjectAnimator animator=ObjectAnimator.ofFloat(img,"rotation",0.0F,360.0F,0.0F,-360.0F);
        animator.setDuration(3000);
        animator.start();
    }
    public void c2(View v)
    {
        ObjectAnimator animator=ObjectAnimator.ofFloat(img,"rotationX",0.0F,180.0F,00.0F);
        animator.setDuration(3000);
        animator.start();
    }
    public void c3(View v)
    {
        ObjectAnimator animator=ObjectAnimator.ofFloat(img,"ScaleX",1.0F,2.0F,1.0F);
        animator.setDuration(3000);
        animator.start();
    }
    public void c4(View v)
    {
        ObjectAnimator animator1=ObjectAnimator.ofFloat(tv,"translationX",300,0);
        ObjectAnimator animator2=ObjectAnimator.ofFloat(tv,"translationY",200,0);
        ObjectAnimator animator3=ObjectAnimator.ofFloat(tv,"alpha",0.0F,1.0F);
        animator1.setDuration(3000);
        animator2.setDuration(3000);
        animator3.setDuration(4000);
        AnimatorSet animatorSet=new AnimatorSet();
        animatorSet.playTogether(animator1,animator2,animator3);
        animatorSet.start();
    }

}

 

Android移动开发—属性动画

上一篇:使用UI Automator Viewer查看APP元素的相对位置,及识别


下一篇:Guideline 2.1 - Performance - App Completeness