android Animation动画的xml使用
- android Animation动画的xml使用
-
在Android应用程序,使用动画效果,能带给用户更好的感觉,做动画可以通过XML或Android代码来实现。Animation动画效果的实现可以通过两种方式进行实现,一种是tweened animation (渐变动画),另一种是frame by frame animation (画面转换动画)。
tweened animation渐变动画有以下两种类型:
1.alpha 渐变透明度动画效果
2.scale 渐变尺寸伸缩动画效果
frame by frame animation 画面转换动画有以下两种类型:
1.translate 画面转换位置移动动画效果
2.rotate 画面转移旋转动画效果
在这里,我使用XML来做动画。实现基本的动画,如淡入,旋转等。
步骤:1、首先在res目录中新建anim的文件夹,在anim中新建需要的动画xml资源文件。
anim/alpha.xml(渐变动画)
?1 2 3 4 5 <!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<set xmlns:android=
"http://schemas.android.com/apk/res/android"
>
</alpha></set>
anim/scale.xml(伸缩动画)
?1 2 3 4 5 <!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<set xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<scale android:interpolator=
"@android:anim/accelerate_decelerate_interpolator"
android:fromxscale=
"0.0"
android:toxscale=
"1.4"
android:fromyscale=
"0.0"
android:toyscale=
"1.4"
android:pivotx=
"50%"
android:pivoty=
"50%"
android:fillafter=
"false"
android:duration=
"700"
>
</scale></set>
1 2 3 4 5 <!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<set xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<translate android:fromxdelta=
"30"
android:toxdelta=
"-80"
android:fromydelta=
"30"
android:toydelta=
"300"
android:duration=
"2000"
>
</translate></set>
anim/rotate.xml(旋转动画)
?1 2 3 4 5 <!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<set xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<rotate android:interpolator=
"@android:anim/accelerate_decelerate_interpolator"
android:fromdegrees=
"0"
android:todegrees=
"+350"
android:pivotx=
"50%"
android:pivoty=
"50%"
android:duration=
"3000"
>
</rotate></set>
2、xml资源文件创建完成之后,接下来就是调用这些资源文件。以alpha为例,先创建AlphaActivity。
在activity_alpha.xml中随便放一张背景图片
?1 2 3 <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"
android:background=
"@drawable/test_bg"
tools:context=
".AlphaActivity"
>
</relativelayout>
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package
com.example.animation_demo;
import
android.os.Bundle;
import
android.app.Activity;
import
android.view.View;
import
android.view.animation.Animation;
import
android.view.animation.Animation.AnimationListener;
import
android.view.animation.AnimationUtils;
import
android.widget.Toast;
public
class
AlphaActivity
extends
Activity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
View view=View.inflate(
this
, R.layout.activity_alpha,
null
);
setContentView(view);
//使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件
Animation animation=AnimationUtils.loadAnimation(
this
, R.anim.alpha);
view.startAnimation(animation);
animation.setAnimationListener(
new
AnimationListener() {
@Override
public
void
onAnimationStart(Animation arg0) {}
//在动画开始时使用
@Override
public
void
onAnimationRepeat(Animation arg0) {}
//在动画重复时使用
@Override
public
void
onAnimationEnd(Animation arg0) {
Toast.makeText(AlphaActivity.
this
,
"在动画结束时使用"
, Toast.LENGTH_SHORT).show();
}
});
}
}
这个是整个layout文件的动画,也可以让任何UI元素调用starAnimation方法。
例如:textMsg.startAnimation(animation); 这时候就不需要下面代码 动画填充指定xml了
?1 2 View view=View.inflate(
this
, R.layout.activity_alpha,
null
);
setContentView(view);
今天刚用到,迟点贴出图片。。
附上找到的一些基本的xml效果:
Fade In:淡入
alpha是渐变透明度效果,值由0到1
?1 2 3 4 5 6 fade_in.xml
<!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<set xmlns:android=
"http://schemas.android.com/apk/res/android"
android:fillafter=
"true"
>
</alpha></set>
以Fade In刚好相反,值由1到0
?1 2 3 4 5 6 fade_out.xml
<!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<set xmlns:android=
"http://schemas.android.com/apk/res/android"
android:fillafter=
"true"
>
</alpha></set>
同时使用Fade in和Fade out可以达到交叉的效果
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 public
class
CrossfadeActivity
extends
Activity
implements
AnimationListener {
TextView txtMessage1, txtMessage2;
Button btnStart;
Animation animFadeIn, animFadeOut;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_crossfade);
txtMessage1 = (TextView) findViewById(R.id.txtMessage1);
txtMessage2 = (TextView) findViewById(R.id.txtMessage2);
btnStart = (Button) findViewById(R.id.btnStart);
// load animations
animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_out);
// set animation listeners
animFadeIn.setAnimationListener(
this
);
animFadeOut.setAnimationListener(
this
);
// button click event
btnStart.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
txtMessage2.setVisibility(View.VISIBLE);
txtMessage2.startAnimation(animFadeIn);
txtMessage1.startAnimation(animFadeOut);
}
});
}
@Override
public
void
onAnimationEnd(Animation animation) {
if
(animation == animFadeOut) {
txtMessage1.setVisibility(View.GONE);
}
if
(animation == animFadeIn){
txtMessage2.setVisibility(View.VISIBLE);
}
}
@Override
public
void
onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public
void
onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
BLink:若隐若现
?1 2 3 4 5 blink.xml
<!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<set xmlns:android=
"http://schemas.android.com/apk/res/android"
>
</alpha></set>
1 2 3 4 5 6 zoom_in.xml
<!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<set xmlns:android=
"http://schemas.android.com/apk/res/android"
android:fillafter=
"true"
>
<scale xmlns:android=
"http://schemas.android.com/apk/res/android"
android:duration=
"1000"
android:fromxscale=
"1"
android:fromyscale=
"1"
android:pivotx=
"50%"
android:pivoty=
"50%"
android:toxscale=
"3"
android:toyscale=
"3"
>
</scale>
</set>
1 2 3 4 5 <!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<set xmlns:android=
"http://schemas.android.com/apk/res/android"
android:fillafter=
"true"
>
<scale xmlns:android=
"http://schemas.android.com/apk/res/android"
android:duration=
"1000"
android:fromxscale=
"1.0"
android:fromyscale=
"1.0"
android:pivotx=
"50%"
android:pivoty=
"50%"
android:toxscale=
"0.5"
android:toyscale=
"0.5"
>
</scale>
</set>
1 2 3 4 5 rotate.xml
<!--?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=
"600"
android:repeatmode=
"restart"
android:repeatcount=
"infinite"
android:interpolator=
"@android:anim/cycle_interpolator"
>
</rotate></set>
根节点的属性:
名称
属性
备注
android:shareInterpolator
是否共享插入器
共享时,四个子节点都用一个插入器
android:interpolator
指定一个动画的插入器
使用系统资源
android:fillEnabled
当设置为true时,fillAfter和fill, Befroe将会都为true,此时会忽略fillBefore 和fillAfter两种属性
android:fillAfter
该动画转化是否在动画结束后被应用
boolean
android:fillBefore
该动画转化是否在动画开始前被应用
boolean
android:repeatMode
重复模式
"restart" 或者 "reverse"
android:repeatCount
重复次数
integer
android:duration
动画持续时间
integer
android:startOffset
动画时间间隔
long
android:zAdjustment
定义动画z order的变换
[normal] or [top] or [bottom]
android:detachWallpaper
boolean
更多相关文章
- [图文]为移植到Android平台上的Cocos2d-x项目添加xml布局文件
- Android三种动画的实现
- 今天开始写android的照片浏览器(一)至返回所有图片文件
- Android工程内嵌资源文件的两种方法
- 理解---Android 向右滑动销毁(finish)Activity, 随着手势的滑动而
- Android 动画之Tween动画详细讲解及java源码实现
- 删除androidAndroid递归方式删除某文件夹下的所有文件
- IntelliJ IDEA 如何导出安卓(Android)apk文件 详细教程
- Android属性动画—实现第三方登录的上拉展开,下拉隐藏