动画是一种方法,其中以特定方式组合图像集合并进行处理,然后将它们显示为运动图像。建立动画使屏幕上的对象看起来还活着。 Android有很多工具可以帮助您相对轻松地创建动画。因此,在本文中,让我们学习使用Java创建android动画。
属性表
XML ATTRIBUTES | DESCRIPTION |
---|---|
android:id | Sets unique id of the view |
android:duration | Used to specify the duration of the animation |
android:fromDegrees | Starting angular position (in degrees) |
android:toDegrees | Ending angular position (in degrees) |
android:fromXScale | Starting X size offset |
android:toXScale | Ending of X size offset |
android:fromYScale | Starting Y size offset |
android:toYScale | Ending of Y size offset |
android:fromAlpha | Starting alpha value for the animation (1.0 means fully opaque and 0.0 means fully transparent) |
android:toAlpha | Ending alpha value |
android:fromYDelta | Change in Y coordinate to be applied at the beginning of the animation |
android:toYDelta | Change in Y coordinate to be applied at the end of the animation |
android:pivotX | Represents the X-axis coordinates to zoom from the starting point |
android:pivotY | Represents the Y-axis coordinates to zoom from the starting point |
android:interpolator | It defines the rate of change of an animation |
android:startOffset | Delay occurs when an animation runs (in ms), once start time is reached |
如何使用Java在Android中添加动画
步骤1:创建一个新项目
- 启动Android Studio(版本> 2.2)
- 转到文件->新建->新建项目。
- 选择清空活动,然后单击下一步
- 选择最低的SDK作为21
- 选择Java语言,然后单击完成按钮。
- 修改以下XML和Java文件。
步骤2:修改activity_main.xml文件
在XML文件中,我们在RelativeLayout内添加了ImageView,TextView和Button。
XML
blinks.xml
rotate.xml
slides.xml
zoom.xml
Java
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.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView logo;
Button blink, slide, rotate, zoom;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// GFG logo
logo = findViewById(R.id.imageView1);
// blink button
blink = findViewById(R.id.button1);
// slide button
slide = findViewById(R.id.button2);
// rotate button
rotate = findViewById(R.id.button3);
// zoom button
zoom = findViewById(R.id.button4);
// blink button listener
blink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
// call a static function loadAnimation()
// of the class AnimationUtils
Animation object
= AnimationUtils
.loadAnimation(
getApplicationContext(),
// blink file is in anim folder
R.anim.blinks);
// call the startAnimation method
logo.startAnimation(object);
}
});
// slide button listener
slide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
// call a static function loadAnimation()
// of the class AnimationUtils
Animation object
= AnimationUtils
.loadAnimation(
getApplicationContext(),
// slide file is in anim folder
R.anim.slide);
// call the startAnimation method
logo.startAnimation(object);
}
});
// rotate button listener
rotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
// call a static function loadAnimation()
// of the class AnimationUtils
Animation object
= AnimationUtils
.loadAnimation(
getApplicationContext(),
// rotate file is in anim folder
R.anim.rotate);
// call the startAnimation method
logo.startAnimation(object);
}
});
// zoom button listener
zoom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
// call a static function loadAnimation()
// of the class AnimationUtils
Animation object
= AnimationUtils
.loadAnimation(
getApplicationContext(),
// zoom file is in anim folder
R.anim.zoom);
// call the startAnimation method
logo.startAnimation(object);
}
});
}
}
步骤3:将这些XML文件添加到anim目录
修改布局后,我们将为动画创建XML文件。因此,我们将首先创建一个文件夹名称anim 。在此文件夹中,我们将添加将用于生成动画的XML文件。为此,请右键单击app / res ,然后选择“ Android资源目录”并将其命名为动画。
Android中一些常见的动画类型是
- 闪烁–隐藏对象0.6到1秒钟。
- 滑动–将对象垂直或水平移动到其轴。
- 旋转–顺时针或逆时针旋转对象。
- 缩放–在X和Y轴上放大或缩小对象。
blinks.xml
rotation.xml
slides.xml
zoom.xml
步骤4:修改MainActivity。Java
要在android中执行动画,我们必须调用AnimationUtils类的静态函数loadAnimation() 。我们在动画对象的实例中获得结果。创建动画对象的语法:
Animation object = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.ANIMATIONFILE);
要将以上动画应用于对象(在图像中说),我们必须调用对象的startAnimation()方法。调用方法的语法:
ImageView image = findViewById(R.id.imageID);
image.startAnimation(object);
动画课的方法:
Method |
Description |
startAnimation(object) | Starts the animation |
setDuration(long duration) | Sets the duration of animation |
getDuration() | Gets the duration of animation |
end() | Ends the animation |
cancel() | Cancels the animation |
Java
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.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView logo;
Button blink, slide, rotate, zoom;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// GFG logo
logo = findViewById(R.id.imageView1);
// blink button
blink = findViewById(R.id.button1);
// slide button
slide = findViewById(R.id.button2);
// rotate button
rotate = findViewById(R.id.button3);
// zoom button
zoom = findViewById(R.id.button4);
// blink button listener
blink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
// call a static function loadAnimation()
// of the class AnimationUtils
Animation object
= AnimationUtils
.loadAnimation(
getApplicationContext(),
// blink file is in anim folder
R.anim.blinks);
// call the startAnimation method
logo.startAnimation(object);
}
});
// slide button listener
slide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
// call a static function loadAnimation()
// of the class AnimationUtils
Animation object
= AnimationUtils
.loadAnimation(
getApplicationContext(),
// slide file is in anim folder
R.anim.slide);
// call the startAnimation method
logo.startAnimation(object);
}
});
// rotate button listener
rotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
// call a static function loadAnimation()
// of the class AnimationUtils
Animation object
= AnimationUtils
.loadAnimation(
getApplicationContext(),
// rotate file is in anim folder
R.anim.rotate);
// call the startAnimation method
logo.startAnimation(object);
}
});
// zoom button listener
zoom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
// call a static function loadAnimation()
// of the class AnimationUtils
Animation object
= AnimationUtils
.loadAnimation(
getApplicationContext(),
// zoom file is in anim folder
R.anim.zoom);
// call the startAnimation method
logo.startAnimation(object);
}
});
}
}