Heim  >  Artikel  >  Web-Frontend  >  Android动画Animation的两种加载执行方式_html/css_WEB-ITnose

Android动画Animation的两种加载执行方式_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:39:131473Durchsuche

本文以简单的AlphaAnimation(“淡入淡出(透明度改变)”动画)为例,简单的说明Android动画Animation的两种加载执行方法:
(1) 直接写Java代码,制作Android动画。
(2) 写XML配置文件,加载XML资源文件执行。
其实这两者是一致的。要知道,在Android中,凡是可以在XML文件完成的View,代码亦可完全写出来。
现在先给出一个Java代码完成的动画AlphaAnimation,AlphaAnimation功能简单,简言之,可以让一个View在一个给定时间内从一个透明度变成另外一个透明度。0.0f是完全透明,1.0f是完全不透明。

A。第一种做法,写Java代码实现。

测试用的主Activity很简单,就一个居中显示的TextView,显示一串文字:Android动画,该TextView启动后在3秒内从完全透明渐变到完全不透明。MainActivity.java :

package zhangphil.animation;import android.app.Activity;import android.os.Bundle;import android.view.animation.AlphaAnimation;import android.view.animation.AnimationSet;import android.widget.TextView;public class MainActivity extends Activity {	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		TextView text = (TextView) findViewById(R.id.text);		// “淡入淡出”动画。		// 0.0f,完全透明,		// 1.0f,完全不透明。		AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);		alphaAnimation.setDuration(3000);		// 动画集。		AnimationSet animationSet = new AnimationSet(false);		animationSet.addAnimation(alphaAnimation);		// 开始播放动画。		text.startAnimation(animationSet);	}}

MainActivity.java需要的activity_main.xml布局文件:

<RelativeLayout 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" >    <TextView        android:id="@+id/text"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:gravity="center"        android:textSize="30sp"        android:text="Android动画" /></RelativeLayout>

这是第一种Java代码写法。


B。第二种做法,写XML文件实现。


介绍第二种XML写法。MainAvtivity.java:

package zhangphil.animation;import android.app.Activity;import android.os.Bundle;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.TextView;public class MainActivity extends Activity {	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		TextView text = (TextView) findViewById(R.id.text);		// 初始化需要加载的动画资源		Animation animation = AnimationUtils				.loadAnimation(this, R.anim.my_anim);		// 将TextView执行Animation动画		text.startAnimation(animation);	}}


MainActivity.java需要的activity_main.xml文件不做任何修改和上述的A相同。

复杂代码工作转嫁到定义anim的XML。

一,需要在res/目录下建立一个anim文件夹,在里面创建一个Android资源文件,假设这个名字自定义为my_anim.xml。目录结构层次如图所示:


二,完成动画所需要的控制要素集代码,my_anim.xml具体代码:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/accelerate_interpolator" >    <alpha        android:duration="3000"        android:fromAlpha="0"        android:toAlpha="1.0" />    <!--        透明度控制动画效果 alpha        浮点型值:            fromAlpha 动画起始时透明度            toAlpha   动画结束时透明度            说明:                 0.0表示完全透明                1.0表示完全不透明            以上值取0.0-1.0之间的float数据类型的数字                整数:            duration  动画持续时间            说明:                     时间以毫秒为单位    --></set>


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn