Example use of four interpolation animations in Android (with code download)

scene

Four interpolation animations in Android.

Transparency gradient drawing

 

 

Rotate Animation

 

 

Zoom Animation

 

 

translating animation

 

 

Note:

Blog:
https://blog.csdn.net/badao_liumang_qizhi
Focus on Public Number
Domineering program ape
Get programming-related e-books, tutorial pushes, and free downloads.

Realization

Transparency gradient drawing

First add an ImageView to the layout file and set the picture source and ID

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".BuJianAnimActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:id="@+id/image"
        android:src="@drawable/bg02"
        android:layout_height="wrap_content"/>

 

</RelativeLayout>

 

Then create a new anim directory under res, and create a new animation resource file alpha.xml under anim directory

 

 

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0"
        android:toAlpha="1"
        android:duration = "2000"/>
</set>

 

Set the start transparency to 0, the end transparency to 1 and the duration to 2 seconds in the animation resource file.

Then come to BuJianAnimActivity, get the ImageView, and set its click event listener.

Click through events

 Animation anim = AnimationUtils.loadAnimation(BuJianAnimActivity.this,R.anim.alpha);

 

Load the animation resource file to create the animation object.

Then the startView method of ImageView is called to start the animation.

package com.badao.animationtest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class BuJianAnimActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bu_jian_anim);
        final ImageView imageView = (ImageView) findViewById(R.id.image);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Creating Animated Objects
                Animation anim = AnimationUtils.loadAnimation(BuJianAnimActivity.this,R.anim.translate);
                //Start Animation
                imageView.startAnimation(anim);
            }
        });
    }
}

 

Rotate Animation

Similar to the above, create a new ronate.xml under anim

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration = "2000"/>
</set>

 

Set the start and end angles, then set the position of the rotation center X and Y, and then set the animation duration.

Then switch the animation file loaded in the activity to this animation resource file.

Zoom Animation

Similar to the above, create a new scale.xml under anim

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:fromXScale="1"
        android:fromYScale="1"
        android:toXScale="2"
        android:toYScale="2"
        android:pivotY="50%"
        android:pivotX="50%"
        android:duration = "2000"/>
</set>

 

Set the start and end scaling ratio, center position and duration of scaling.

Then switch the animation file loaded in the activity to this animation resource file.

translating animation

Similar to the above, create a new translate.xml under anim

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="300"
        android:toYDelta="300"
        android:duration = "2000"/>
</set>

 

Set the X and Y positions of the start and end points, and set the duration for 2 seconds.

Then switch the animation file loaded in the activity to this animation resource file.

Code Download

https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12097375

Tags: Android xml encoding Programming

Posted on Sat, 11 Jan 2020 09:18:10 -0800 by aahh