今天给大家分享的是Android中的动画,在Android中系统为我们提供了四种动画效果,分别是:缩放、平移、旋转、渐变。下面我们通过一个简单的例子来了解Android中的动画机制。这里我们主要用到的类有ScaleAnimation、TranslateAnimation、RotateAnimation、AlphaAnimation,分别对应上面的四种动画效果。代码实现如下:
package com.Android.Animation;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
Button BtnScale,BtnMove,BtnRoate,BtnAlpha;
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BtnScale=(Button)findViewById(R.id.BtnScale);
BtnScale.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
Animation Ani_Scale=new ScaleAnimation(0f, 1f, 0f, 1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
Ani_Scale.setDuration(3000);
iv.startAnimation(Ani_Scale);
}
});
BtnMove=(Button)findViewById(R.id.BtnMove);
BtnMove.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
Animation Ani_Trans = new TranslateAnimation(10,100,10,100);
Ani_Trans.setDuration(3000);
iv.startAnimation(Ani_Trans);
}
});
BtnRoate=(Button)findViewById(R.id.BtnRoate);
BtnRoate.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
Animation Ani_Rotate = new RotateAnimation(0f, -360f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
Ani_Rotate.setDuration(3000);
iv.startAnimation(Ani_Rotate);
}
});
BtnAlpha=(Button)findViewById(R.id.BtnAlpha);
BtnAlpha.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
Animation Ani_Alpha = new AlphaAnimation(0.1f, 1.0f);
Ani_Alpha.setDuration(3000);
iv.startAnimation(Ani_Alpha);
}
});
iv=(ImageView)findViewById(R.id.ImageView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
谢谢你,在这世界的角落,找到我,一个即将进入而立之年的中年大叔,常年以 飞鸿踏雪 的花名混迹江湖。在现实生活中,我是一名 伪·全栈攻城狮,因为我觉得,什么都略懂一点,生活会更多彩一些。目前,主要关注.NET、.NET Core、Python、数据分析、微服务、Web 等技术方向。日常行为:读书、写作、电影、烹饪、洞箫等。喜欢看日剧/纪录片/科普、刷B站、刷LeetCode等。