android开发之Animations的使用(二)

本博文主要讲述的是android开发中的animation动画效果的使用,和上一篇博文不同的是,此次四种动画效果,主要使用的是xml文件实现的,提高了代码的可重用性和可维护性.

使用的基本过程例如以下:

1、首先在res目录下创建一个anim目录

 2、在此目录中新建四种动画效果的xml文件(scale.xml,rotate.xml,alpha.xml,translate.xml)

 3、在代码中直接使用AnimationUtils调用静态方法loadAnimation载入创建一个animation对象

 4、执行animation对象



实例代码例如以下:

package com.example.animationtest2;





import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.animation.Animation;

import android.view.animation.AnimationUtils;

import android.widget.Button;

import android.widget.ImageView;









public class MainActivity extends Activity {





private ImageView imageView = null;

private Button scaleButton = null;

private Button translateButton = null;

private Button rotateButton = null;

private Button alphaButton = null;







@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);



imageView = (ImageView)findViewById(R.id.myImage);

scaleButton = (Button)findViewById(R.id.scaleButton);

translateButton = (Button)findViewById(R.id.translateButton);

rotateButton = (Button)findViewById(R.id.rotateButton);

alphaButton = (Button)findViewById(R.id.alphaButton);



scaleButton.setOnClickListener(new setScaleListener());

translateButton.setOnClickListener(new setTranslateListener());

rotateButton.setOnClickListener(new setRotateListener());

alphaButton.setOnClickListener(new setAlphaListener());



}



//动画缩放效果监听器

class setScaleListener implements OnClickListener{





@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);



imageView.startAnimation(animation);

}



}



//动画移动效果监听器

class setTranslateListener implements OnClickListener{





@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);



imageView.startAnimation(animation);


}



}



//旋转动画效果监听器

class setRotateListener implements OnClickListener{





@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);



imageView.startAnimation(animation);

}



}





//渐入渐出动画效果监听器

class setAlphaListener implements OnClickListener{





@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);



imageView.startAnimation(animation);

}



}











@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}





}







主布局文件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"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >





    <TextView

        android:id="@+id/myText"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/hello_world" />

    

    <LinearLayout 

        android:id="@+id/imgLayout"

        android:layout_below="@id/myText"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:padding="55dp"

        >

        <ImageView

        android:id="@+id/myImage"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:src="@drawable/ic_launcher"

        />

    </LinearLayout>

    

    

    <Button 

        android:id="@+id/scaleButton"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_below="@id/imgLayout"

        android:text="@string/scale"

        />

    

    <Button 

        android:id="@+id/translateButton"

        android:layout_below="@id/scaleButton"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/translate"

        />

    

    <Button 

        android:id="@+id/rotateButton"

        android:layout_below="@id/translateButton"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/rotate"

        />

    

    <Button 

        android:id="@+id/alphaButton"

        android:layout_below="@id/rotateButton"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/alpha"

        />





</RelativeLayout>











四种动画效果的xml文件分别例如以下:

scale.xml:

<?

xml version="1.0" encoding="utf-8"?>

<set android:shareInterpolator="true" xmlns:android="http://schemas.android.com/apk/res/android">

    <scale 

        android:fromXScale="1"

        android:toXScale="0"

        android:fromYScale="1"

        android:toYScale="0"

        android:pivotX="50%"

        android:pivotY="50%"

        android:duration="2000"

        />

<!-- 50%p  表示 相对父控件的中间位置 -->

   <!-- 50%   表示相对自身的中间位置 -->

   <!-- 50   表示绝对位置 -->

</set>



rotate.xml:

<?

xml version="1.0" encoding="utf-8"?>

<set android:shareInterpolator="true" xmlns:android="http://schemas.android.com/apk/res/android">

<rotate 

   android:fromDegrees="0"

   android:toDegrees="-360"

   android:pivotX="50%"

   android:pivotY="50%"

   android:duration="2000"

   />   

   <!-- 50%p  表示 相对父控件的中间位置 -->

   <!-- 50%   表示相对自身的中间位置 -->

   <!-- 50   表示绝对位置 -->

   <!-- 360 表示顺时针  -360表示逆时针旋转 --> 

</set>



alpha.xml:



<?xml version="1.0" encoding="utf-8"?>

<set android:shareInterpolator="true" xmlns:android="http://schemas.android.com/apk/res/android">  <!-- set相当于AnimationSet对象 -->    

    <alpha

        android:fromAlpha="1.0"

        android:toAlpha="0.1"

        android:duration="2000"

        />

</set>



translate.xml:

<?xml version="1.0" encoding="utf-8"?>

<set android:shareInterpolator="true" xmlns:android="http://schemas.android.com/apk/res/android">

    <translate 

        android:fromXDelta="0%"

        android:toXDelta="50%"

        android:fromYDelta="0%"

        android:toYDelta="100%"

        android:duration="2000"

        />

    

      <!-- 50%p  表示 相对父控件的中间位置 -->

   <!-- 50%   表示相对自身的中间位置 -->

   <!-- 50   表示绝对位置 -->



</set>



实现效果例如以下:





按下相关button查看相关效果



android开发之Animations的使用(二)的更多相关文章

  1. Android开发之Java集合类性能分析

    对于Android开发者来说深入了解Java的集合类很有必要主要是从Collection和Map接口衍生出来的,目前主要提供了List.Set和 Map这三大类的集合,今天Android吧(ard8. ...

  2. 【Android UI】Android开发之View的几种布局方式及实践

    引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...

  3. Android开发之旅: Intents和Intent Filters(理论部分)

    引言 大部分移动设备平台上的应用程序都运行在他们自己的沙盒中.他们彼此之间互相隔离,并且严格限制应用程序与硬件和原始组件之间的交互. 我们知道交流是多么的重要,作为一个孤岛没有交流的东西,一定毫无意义 ...

  4. Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab

     今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...

  5. Android 开发之旅:深入分析布局文件&又是“Hello World!”

    http://www.cnblogs.com/skynet/archive/2010/05/20/1740277.html 引言 上篇可以说是一个分水岭,它标志着我们从Android应用程序理论进入实 ...

  6. Android开发之旅3:android架构

    引言 通过前面两篇: Android 开发之旅:环境搭建及HelloWorld Android 开发之旅:HelloWorld项目的目录结构 我们对android有了个大致的了解,知道如何搭建andr ...

  7. Android开发之MdiaPlayer详解

    Android开发之MdiaPlayer详解 MediaPlayer类可用于控制音频/视频文件或流的播放,我曾在<Android开发之基于Service的音乐播放器>一文中介绍过它的使用. ...

  8. Android开发之InstanceState详解

    Android开发之InstanceState详解   本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...

  9. Android开发之Git配置

    Android开发之Git配置 1.首先git配置: 输入命令: git config --global user.name "xxx.xx" git config --globa ...

随机推荐

  1. C#多显示器转换的两种方法——SetWindowPos,Screen

    原文 http://blog.csdn.net/hejialin666/article/details/6057551 实现多屏显示目的:一般情况下是一个电脑显示屏,外接一个电视显示屏.在电脑上显示的 ...

  2. UML_行为图

    活动图是UML用于对系统的动态行为建模的另一种常用工具,它描述活动的顺序,展现从一个活动到另一个活动的控制流.活动图在本质上是一种流程图.活动图着重表现从一个活动到另一个活动的控制流,是内部处理驱动的 ...

  3. linux动态库加载RPATH, RUNPATH

    摘自http://gotowqj.iteye.com/blog/1926771 linux动态库加载RPATH, RUNPATH 链接动态库 如何程序在连接时使用了共享库,就必须在运行的时候能够找到共 ...

  4. hdu 2059 龟兔赛跑(dp)

    龟兔赛跑 Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击——赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下沙某农业园卧薪尝胆潜心修炼,终于练成 ...

  5. linux共享内存简析

    共享内存是IPC的一种机制,允许两个不相关的进程共享同一块内存 //共享内存可以双向通信,但其本身没有相应机制,需要程序编写者设计,本例为单向通信(分为读端和写端). 共享内存读端: #include ...

  6. OpenGLES 怎样在十天内掌握线性代数 - 希望这是真的!

    OpenGLES 怎样在十天内掌握线性代数 - 希望这是真的! 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致&q ...

  7. 【稳定婚姻问题】【HDU1435】【Stable Match】

    2015/7/1 19:48 题意:给一个带权二分图  求稳定匹配 稳定的意义是对于某2个匹配,比如,( a ---- 1) ,(b----2) , 如果 (a,2)<(a,1) 且(2,a)& ...

  8. inline函数

    C语言中的inline函数并不是单纯的用函数块内容来替换,也可能存在局部变量啥的.另外,使用inline定义的函数只是建议编译器作为内联函数处理,但并不一定真会这样处理.inline一般直接在头文件中 ...

  9. (转)ASP.NET缓存概念及其应用浅析

    ASP.NET缓存概念及其应用浅析 ASP.NET缓存是什么呢?ASP.NET缓存有什么样子的特点呢?本文就向你详细介绍ASP.NET缓存的相关情况. ASP.NET缓存概念是什么呢?通常,应用程序可 ...

  10. 实现winfrom进度条及进度信息提示,winfrom程序假死处理

    1.方法一:使用线程 功能描述:在用c#做WinFrom开发的过程中.我们经常需要用到进度条(ProgressBar)用于显示进度信息.这时候我们可能就需要用到多线程,如果不采用多线程控制进度条,窗口 ...