Android 基础动画效果

来源:互联网 发布:商场数据采集 编辑:程序博客网 时间:2024/05/21 18:18

Android 的基础动画效果有透明度动画,旋转动画,比例动画,位移动画,自定义动画

在下面会各给出一个示例:


新建一个Activity:TweenAnimationActivity.java

activity_tween_animation.xml文件内布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".TweenAnimationActivity"    android:orientation="vertical" >        <ImageView         android:id="@+id/animation_image"        android:layout_width="150dp"        android:layout_height="150dp"        android:layout_marginTop="50dp"        android:src="@drawable/appico"        android:layout_gravity="center_horizontal"/>    <Button         android:id="@+id/alpha_animation"        android:layout_width="150dp"        android:layout_height="wrap_content"        android:layout_marginTop="100dp"        android:layout_gravity="center_horizontal"        android:text="透明度动画"/>        <Button         android:id="@+id/rotate_animation"        android:layout_width="150dp"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:text="旋转动画"/>        <Button         android:id="@+id/scale_animation"        android:layout_width="150dp"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:text="比例动画"/>        <Button         android:id="@+id/translate_animation"        android:layout_width="150dp"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:text="位移动画"/>        <Button         android:id="@+id/xml_animation"        android:layout_width="150dp"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:text="XML自定义动画"/></LinearLayout>


实际效果如下:



Activity 内代码如下:

public class TweenAnimationActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_tween_animation);final ImageView iv=(ImageView) findViewById(R.id.animation_image);final Button btn=(Button) findViewById(R.id.alpha_animation);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//alpha透明度变化动画//第一个参数fromAlpha为动画开始时的透明度//第二个参数toAlpha为动画结束时的透明度//0.0表示完全透明;1.0表示完全不透明Animation alphaAnimation=new AlphaAnimation(1.0f, 0.1f);//设置动画时间alphaAnimation.setDuration(1000);//停留在最后一帧,避免恢复原状,仅为测试,如在工程中,应写监听事件alphaAnimation.setFillAfter(true);alphaAnimation.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {//iv.setVisibility(View.GONE);//iv.setVisibility(View.INVISIBLE);}});iv.startAnimation(alphaAnimation);}});Button rotate_btn=(Button) findViewById(R.id.rotate_animation);rotate_btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//旋转动画//第一个参数fromDegrees表示动画起始时的角度,第二个参数toDegrees表示动画结束时的角度//默认以左上角为原点//Animation rotateAnimation=new RotateAnimation(0f, 360f);Animation rotateAnimation=new RotateAnimation(-90f, 90f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);rotateAnimation.setRepeatCount(10);rotateAnimation.setRepeatMode(Animation.REVERSE);rotateAnimation.setDuration(500);iv.startAnimation(rotateAnimation);}});Button scale_btn=(Button) findViewById(R.id.scale_animation);scale_btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//比例动画//第一个参数fromXDelta,第二个参数toXDelta:分别是动画的起始、结束时的X坐标//第三个参数fromYDelta,第四个参数toYDelta:分别是动画的起始、结束时的Y坐标//Animation scaleAnimation=new ScaleAnimation(0.1f,1.0f,0.1f,1.0f);//第五个参数pivotXType为动画在X轴相对于物件位置类型//第六个参数pivotXValue为动画相对于物件的X坐标的开始位置//第七个参数pivotYType为动画在Y轴相对于物件位置类型//第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置Animation scaleAnimation=new ScaleAnimation(1.0f,2.0f,1.0f,2.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);scaleAnimation.setRepeatCount(10);scaleAnimation.setRepeatMode(Animation.RESTART);//设置动画时间scaleAnimation.setDuration(60);iv.startAnimation(scaleAnimation);}});Button translate_btn=(Button) findViewById(R.id.translate_animation);translate_btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//位移动画//第一个参数fromXDelta,第二个参数toXDelta:分别是动画起始结束时X坐标//第三个参数fromYDelta,第四个参数toYDelta:分别是动画起始结束时Y坐标Animation translateAnimation=new TranslateAnimation(0.1f, 0.1f,0.1f, 150.f);translateAnimation.setRepeatCount(105);translateAnimation.setRepeatMode(Animation.REVERSE);translateAnimation.setDuration(500);iv.startAnimation(translateAnimation);}});Button xml_btn=(Button) findViewById(R.id.xml_animation);xml_btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Animation myAnimation=AnimationUtils.loadAnimation(TweenAnimationActivity.this, R.anim.complex_animation);iv.startAnimation(myAnimation);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.tween_animation, menu);return true;}}

自定义动画需要在res文件夹内新建一个anim文件夹,之后在anim文件夹内新建xml文件complex_animation.xml

complex_animation.xml内容如下:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <alpha         android:fromAlpha="0.1"        android:toAlpha="1.0"        android:duration="1000"/>        <translate         android:fromXDelta="0"        android:toXDelta="0"        android:fromYDelta="0"        android:toYDelta="150"/></set>

我设置的这几个动画效果还是很有意思的,大家也可以玩一玩!



0 0
原创粉丝点击