上一篇学习用硬编码直接进行对动画效果进行设置(Android中的动画效果学习之---Tween动画(硬编码方法创建)),今天学习一下使用XML文件进行配置动画效果:

其中里面主要参数说明已经在上一篇讲过了,接下来进行设置动画的具体步骤:

第一步:在项目的res文件下面新建一个文件夹名字是anim 【注意】这个名字必要要是anim

第二步: 在anim文件夹下面,新建新的XMl文件 在xml文件中具体设置动画效果

第三步: 在Activity中 使用AnimationUtils.loadAnimation(Demo_Animation_second.this, R.anim.xxxxx);进行获取

具体看下面的配置代码:

base_alpha配置:

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" >      <alpha         android:duration="3000"         android:fromAlpha="0.0"         android:toAlpha="1.0" />  </set>
base_rotate配置:

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" >      <rotate         android:duration="3000"         android:fromDegrees="0.0"         android:pivotX="80%"         android:pivotY="80%"         android:toDegrees="180" />  </set>

base_scale配置:

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" >      <scale         android:duration="3000"         android:fromXScale="0.0"         android:fromYScale="0.0"         android:pivotX="80%"         android:pivotY="80%"         android:toXScale="10.0"         android:toYScale="10.0" />  </set>
base_translate配置:

   <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" >       <translate         android:fromXDelta="0.0"         android:fromYDelta="0.0"         android:toXDelta="10.0"         android:toYDelta="10.0"
        android:duration="3000"/>   </set>            

Demo Activity源代码如下:

package com.android.animation;  import com.android.R;  import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationUtils; 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 Demo_Animation_second extends Activity {  // 定义四个按钮,分别对用四种动画效果操作 private Button alpha, roate, scale, translate;  // 声明ImageView组件 private ImageView animation_test;  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.animation);  animation_test = (ImageView) this.findViewById(R.id.test_animation); alpha = (Button) this.findViewById(R.id.alpha); roate = (Button) this.findViewById(R.id.roate); scale = (Button) this.findViewById(R.id.scale); translate = (Button) this.findViewById(R.id.translate);  // 为按钮组件添加监听事件 alpha.setOnClickListener(new OnClickListener() {  @Override public void onClick(View v) {  // 使用AnimationUitls工具类进行常见动画,使用到XML配置文件 Animation alphaAnimation = AnimationUtils.loadAnimation( Demo_Animation_second.this, R.anim.base_alpha); // 为图片添加效果,开始动画 animation_test.setAnimation(alphaAnimation); } });  roate.setOnClickListener(new OnClickListener() {  @Override public void onClick(View v) { // 使用AnimationUitls工具类进行常见动画,使用到XML配置文件 Animation roateAnimation = AnimationUtils.loadAnimation( Demo_Animation_second.this, R.anim.base_rotate); roateAnimation.setDuration(3000); // 为图片添加效果,开始动画 animation_test.setAnimation(roateAnimation); } });  scale.setOnClickListener(new OnClickListener() {  @Override public void onClick(View v) { // 使用AnimationUitls工具类进行常见动画,使用到XML配置文件 Animation scaleAnimation = AnimationUtils.loadAnimation( Demo_Animation_second.this, R.anim.base_scale); // 为图片添加效果,开始动画 animation_test.setAnimation(scaleAnimation); } }); translate.setOnClickListener(new OnClickListener() {  /**  * 位置变化  */ @Override public void onClick(View v) { // 使用AnimationUitls工具类进行常见动画,使用到XML配置文件 Animation translateAnimation = AnimationUtils.loadAnimation( Demo_Animation_second.this, R.anim.base_translate); // 为图片添加效果,开始动画 animation_test.setAnimation(translateAnimation); } }); } } 

截图效果:




上面两个截图分别是 alpha和rotate的效果

下面两个截图分别是 scale 和translate的效果

更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  3. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. 基于 Distcc 的android分布式编译环境的搭建
  6. AndroidUnity项目嵌入有米广告注意
  7. Android(安卓)通过伪3D变换可以基本实现coverflow的效果
  8. Android下增加service和对应的AIDL
  9. ANDROID 如何适配屏幕

随机推荐

  1. Android第三方登录-----微信登录接入方法
  2. 【Android】各个透明度的取值
  3. 5.4呈献:HP-Socket v5.3.1 支持 Android(
  4. Android编译环境的配置
  5. Kotlin Android Studio入门学习研究
  6. 修改Android开机图片、动画、声音
  7. Android Studio-Gradle项目中添加libs目
  8. Android SlidingMenu例子运行报错的解决
  9. Android Notification通知栏、点击事件、
  10. android的五大布局详解