通过xml文件来设置动画

alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0"
        android:startOffset="200"
        android:duration="1000"/>

</set>

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%p"
        android:pivotY="50%"
        android:duration="1000"/>

</set>

scale.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:fromXScale="1.0"
        android:toXScale="0"
        android:fromYScale="1.0"
        android:toYScale="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000"/>

</set>

translate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="50%"
        android:toXDelta="100%"
        android:fromYDelta="50%"
        android:toYDelta="200%"
        android:duration="2000"/>

</set>

MainActivity.java

package com.kale.anim;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity {

    ImageView iV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iV = (ImageView)findViewById(R.id.imageView);
    }

    public void buttonListener(View v) {
        Animation anim = null;
        switch (v.getId()) {
        case R.id.alpha_button:
            anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);
            break;
        case R.id.rotate_button:
            anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
            break;
        case R.id.scale_button:
            anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale);
            break;
        case R.id.translate_button:
            anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate);
            break;
        default:
            break;
        }

        //设置开始动画
        iV.startAnimation(anim);
    }
}

通过工具类就能加载xml文件,这样就能使用xml文件了。最后贴上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"
    tools:context="${relativePackage}.${activityClass}" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:orientation="vertical" >

        <Button
            android:id="@+id/alpha_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="buttonListener"
            android:text="透明度改变" />

        <Button
            android:id="@+id/rotate_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="buttonListener"
            android:text="旋转动画" />

        <Button
            android:id="@+id/scale_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="buttonListener"
            android:text="缩放动画" />

        <Button
            android:id="@+id/translate_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="buttonListener"
            android:text="移动动画" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

关于动画速率设置。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true">

……
</set>

example:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:shareInterpolator="true">
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0"
        android:startOffset="200"
        android:duration="1000"/>

</set>

代码中可以这样设置:

        AnimationSet set = new AnimationSet(true);
        set.setInterpolator(new DecelerateInterpolator());
时间: 2024-08-02 19:19:13

通过xml文件来设置动画的相关文章

android通过xml文件实现Animation动画

Rotate的xml文件编写方法 <rotate android:fromDegrees="0" android:toDegrees="+350" android:pivotX="50%" android:pivotY="50%" android:duration="1000"/> *android:toDegrees="+350"正号代表的是旋转方向,正号为顺时针,负号为逆

Android中string.xml文件中设置部分字体颜色大小

1.在string.xml文件中: <string name="tips_all"><Data><![CDATA[清理进程:<font color="#7700ff00"><b>%1$d<br/></b></font>清理内存:<font color="#7700ff00"><b>%2$sMB</b></font&g

Android开发在string.xml文件中设置部分字体颜色大小

1.在string.xml文件中: <string name="exchange_txt_hint"><Data><![CDATA[请使用<font color="#ff0000"><b>%1$s</b></font>牛兑换<font color="#00ff00"><b>%2$s</b></font>%3$s 上网时长]

pom.xml文件设置

一个相对完整的maven配置文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.

Android 为PopupWindow设置动画效果

首先定义显示效果的动画文件: <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="0" android:fromYDel

圆角背景实现,如实现圆角按钮;用xml文件画圆

项目中为了更好的视觉效果,然后又懒的去弄图片做背景时,往往就会通过xml布局文件来实现圆角的效果. 在drawable目录下新建一个shape类型的xml文件,设置<corners android:radius="5dp" />,然后在需要设为圆角的控件中,如按钮或LinearLayout,设置他们的background为drawable="@drawable/...".即可达到圆角的效果 // 用xml文件画圆 用竖线连接ListView的各个item

转载:用Ant操作XML文件

1.14 用XMLTask操作XML(1) 本节作者:Brian Agnew 对于简单的文本搜索和替换操作,Ant的<replace>任务就够用了,但在现代Java框架中,用户更可能需要强大的XML操作能力来修改servlet描述符.Spring配置等. XMLTask是Ant外部任务,它提供了强大的XML编辑工具,主要用于在构建/部署过程中创建和修改XML文件. 使用XMLTask的好处如下? 与Ant的<replace>任务不同,XMLTask使用XPath提供识别XML文档各

【Android】利用表格布局,Android中xml文件与java的交互制作登录界面

登录界面是图形编程.网页编程的一个经典而又基础的程序. 在安卓中,如图所示一个基本登录界面: 点击取消按钮就关闭这个程序,点击登录按钮则显示用户输入的用户名与密码. 一.基本布局 这个程序利用到安卓中的表格布局. 先打开res/values/strings.xml中定义几个字符串.之所以不直接把字符串直接写在activity_main.xml的组件中,是因为免得Eclipse出现警告.这个文件的代码如下: <?xml version="1.0" encoding="ut

XML文件(1)--使用DOM示例

其他依赖字段/方法 // 书籍列表 private List<Book> bookList = new LinkedList<Book>(); /** * 根据xml文件,得到书籍列表 * * @param file * @return */ public List<Book> getBookList(String file) { parseXML(file); return bookList; } 获取DocumentBuilder(文件解析器) /** * 获取Do