2011.10.28注:如果需要控件停在动画后的位置,需要设置android:fillAfter属性为true,在set节点中。默认在动画结束后回到动画前位置。设置android:fillAfter后,我们看到了控件留在了动画后的位置,其实也只是看到在那个位置,真实位置还是在原来动画前那里,你会发现Button不能被点击,就是这个原因。所以我们可以在动画结束后,手动把控件移动到动画结束后的位置。这就需要根结点为AbsoluteLayout,因为LinearLayout不能通过x,y座标定位。具体方法:把布局换成AbsoluteLayout,使用Animation的setAnimationListener设置动画播放事件,在onAnimationEnd方法中,使用控件的setLayoutParams方法,设置动画后的位置。

5月15日注:overridePendingTransition只支持android 2.0以上版本

Android的动画效果分为两种,一种是tweened animation(补间动画),第二种是frame by frame animation。一般我们用的是第一种。补间动画又分为AlphaAnimation,透明度转换 RotateAnimation,旋转转换 ScaleAnimation,缩放转换 TranslateAnimation 位置转换(移动)。

动画效果在anim目录下的xml文件中定义,在程序中用AnimationUtils.loadAnimation(Context context,int ResourcesId)载入成Animation对象,在需要显示动画效果时,执行需要动画的View的startAnimation方法,传入Animation,即可。切换Activity也可以应用动画效果,在startActivity方法后,执行overridePendingTransition方法,两个参数分别是切换前的动画效果,切换后的动画效果,下面的例子中传入的是两个alpha动画,以实现切换Activity时淡出淡入,渐隐渐现效果。

下面贴出代码:

两个Activity的布局文件 main.xml:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:id="@+id/ll"

    android:background="@drawable/white"

    >

<TextView  android:id="@+id/tv"

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="translate动画效果"

    />

<TextView  android:id="@+id/tv2"

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="scale动画效果"

    />

<TextView  android:id="@+id/tv3"

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="alpha动画效果"

    />

<TextView  android:id="@+id/tv4"

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="rotate动画效果"

    />

<Button android:id="@+id/bt3"

android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="动画演示"

/>

<Button android:id="@+id/bt"

android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="切换"

/>

</LinearLayout>

activity2.xml:





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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:id="@+id/ll2"

    >

<TextView  

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="Activity2"

    />

<Button android:id="@+id/bt2"

android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="返回main"

/>

</LinearLayout>

动画效果XML文件,全部存放在anim目录下:

a1.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.0"

android:duration="500"

/>

</set>

<!-- 

fromAlpha:开始时透明度

toAlpha:结束时透明度

duration:动画持续时间

 -->

a2.xml 淡入效果:





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

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

<alpha

android:fromAlpha="0.0"

android:toAlpha="1.0"

android:duration="500"

/>

</set>

rotate.xml 旋转效果:





<?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="300"

android:toDegrees="-360"

android:pivotX="10%"

android:pivotY="100%"

android:duration="10000" />

</set>

<!-- 

fromDegrees开始时的角度

toDegrees动画结束时角度

pivotX,pivotY不太清楚,看效果应该是定义旋转的圆心的

 -->

scale.xml 缩放效果:





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

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

<scale  

     android:interpolator= "@android:anim/decelerate_interpolator"    

     android:fromXScale="0.0"    

     android:toXScale="1.5"    

     android:fromYScale="0.0"    

     android:toYScale="1.5"    

     android:pivotX="50%"    

     android:pivotY="50%"    

     android:startOffset="0"    

     android:duration="10000"

     android:repeatCount="1"  

     android:repeatMode="reverse"

     /> 

</set>

<!-- 

interpolator指定动画插入器,常见的有加速减速插入器accelerate_decelerate_interpolator,加速插入器accelerate_interpolator,减速插入器decelerate_interpolator。

fromXScale,fromYScale,动画开始前X,Y的缩放,0.0为不显示,1.0为正常大小

toXScale,toYScale,动画最终缩放的倍数,1.0为正常大小,大于1.0放大

pivotX,pivotY动画起始位置,相对于屏幕的百分比,两个都为50%表示动画从屏幕中间开始

startOffset,动画多次执行的间隔时间,如果只执行一次,执行前会暂停这段时间,单位毫秒

duration,一次动画效果消耗的时间,单位毫秒,值越小动画速度越快

repeatCount,动画重复的计数,动画将会执行该值+1次

repeatMode,动画重复的模式,reverse为反向,当第偶次执行时,动画方向会相反。restart为重新执行,方向不变

 -->

translate.xml 移动效果:





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

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

<translate

android:fromXDelta="320"

android:toXDelta="0"

android:fromYDelta="480"

android:toYDelta="0"

android:duration="10000" />

</set>

<!-- 

fromXDelta,fromYDelta起始时X,Y座标,屏幕右下角的座标是X:320,Y:480

toXDelta,toYDelta动画结束时X,Y的座标

 -->

下面是程序代码,main.java:





package com.pocketdigi.animation;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

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.TextView;

 

public class main extends Activity {

    /** Called when the activity is first created. */

TextView tv,tv2,tv3,tv4;

Button bt3;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

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

        tv=(TextView)findViewById(R.id.tv);

        tv2=(TextView)findViewById(R.id.tv2);

        tv3=(TextView)findViewById(R.id.tv3);

        tv4=(TextView)findViewById(R.id.tv4);

 

 

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

        bt.setOnClickListener(new OnClickListener(){

 

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Intent intent=new Intent(main.this,activity2.class);

startActivity(intent);

overridePendingTransition(R.anim.a2,R.anim.a1);

//淡出淡入动画效果

}

 

        });

        bt3.setOnClickListener(new OnClickListener(){

 

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

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

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

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

       Animation alpha=AnimationUtils.loadAnimation(main.this, R.anim.a1);

       //载入XML文件成Animation对象

       tv.startAnimation(translate);

       tv2.startAnimation(scale);

       tv3.startAnimation(alpha);

       tv4.startAnimation(rotate);

       //应用动画

 

}});

    }

}

activity2.java:





package com.pocketdigi.animation;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

 

public class activity2 extends Activity {

Button bt2;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity2);

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

        bt2.setOnClickListener(new OnClickListener(){

 

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Intent intent=new Intent(activity2.this,main.class);

startActivity(intent);

overridePendingTransition(R.anim.a2,R.anim.a1);

}

 

        });

    }

}


注:动画切换Activity只有在新启动Activity才有效,如果Activity已经启动,并且intent加了FLAG_ACTIVITY_REORDER_TO_FRONT,这样不会新启动Activity,也就没有动画效果。

因为代码比较多,最后附上打包的源文件:

animation
(2350)

Android动画效果 translate、scale、alpha、rotate 切换Activity动画 控件位置调整的更多相关文章

  1. Android动画效果translate、scale、alpha、rotate详解

    动画类型 Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画效果 rotate 画面 ...

  2. Android动画效果之初识Property Animation(属性动画)

    前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...

  3. Android动画效果之Tween Animation(补间动画)

    前言: 最近公司项目下个版本迭代里面设计了很多动画效果,在以往的项目中开发中也会经常用到动画,所以在公司下个版本迭代开始之前,抽空总结一下Android动画.今天主要总结Tween Animation ...

  4. Android动画效果之Property Animation进阶(属性动画)

    前言: 前面初步认识了Android的Property Animation(属性动画)Android动画效果之初识Property Animation(属性动画)(三),并且利用属性动画简单了补间动画 ...

  5. Android动画效果之Frame Animation(逐帧动画)

    前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame ...

  6. Android 高级UI设计笔记11:Gallery(画廊控件)之Gallery基本使用

    1. 这里要向大家介绍Android控件Gallery(画廊控件) Gallery控件主要用于横向显示图像列表,不过按常规做法.Gallery组件只能有限地显示指定的图像.也就是说,如果为Galler ...

  7. Android开发系列(十八):自己定义控件样式在drawable目录下的XML实现

    在Android开发的过程中,我们常常须要对控件的样式做一下改变,能够通过用添加背景图片的方式进行改变,可是背景图片放多了肯定会使得APK文件变的非常大. 我们能够用自己定义属性shape来实现. s ...

  8. Android动画效果translate、scale、alpha、rotate

    overridePendingTransition只支持android 2.0以上版本,动画效果在anim目录下的xml文件中定义,在程序中用AnimationUtils.loadAnimation( ...

  9. Android开发之ViewPager实现多页面切换及动画效果(仿Android的Launcher效果)

    Android开发中经常会有引导页或者切换页面等效果,本文采用ViewPager结合动画效果来实现仿Launcher以及页面切换的效果.源码地址在文章最后给出下载. 效果图如下:       1.Vi ...

随机推荐

  1. Java Web学习总结(6)Cookie/Session

    一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 二.会话过程中要解决的一些问题 每个用户在使用浏览器与服务器进行 ...

  2. php strcasecmp()函数 语法

    php strcasecmp()函数 语法 作用:比较两个字符串(不区分大小写)直线电机驱动器 语法:strcasecmp(string1,string2) 参数: 参数 描述 string1 必须, ...

  3. PHP curl_getinfo函数

    curl_getinfo — 获取一个cURL连接资源句柄的信息 说明 mixed curl_getinfo ( resource $ch [, int $opt = 0 ] ) 获取最后一次传输的相 ...

  4. html 和 body标签的 css 设置

    个人猜测浏览器的机制:H5页面底板上有一张画布,画布高度可以被撑高.html.body等元素是固定在画布上的.浏览器中页面的滚动是跟着画布滚动的.(fixed定位是脱离这种机制的,相对浏览器窗口定位的 ...

  5. 2-prometheus各组件安装

    相关下载: https://prometheus.io/download/https://github.com/prometheus/ 相关文档 https://songjiayang.gitbook ...

  6. 源码分析笔记Vector

    概述 继承抽象类AbStractList,实现接口List.RandomAccess.Cloneable以及序列化接口默认容量大小为10,扩容增量为0,扩容为原容量的2倍如设置的增量大于0,则扩容为( ...

  7. 2018-2019-2 实验四 Android程序设计

    实验要求 参考Android开发简易教程 完成云班课中的检查点,也可以先完成实验报告,直接提交.注意不能只有截图,要有知识点,原理,遇到的问题和解决过程等说明.实验报告中一个检查点要有多张截图. 发表 ...

  8. C#链接mysql出现 One of the identified items was in an invalid format

    这个问题在tolist查询结果的时候就会出现但是count就不会出现,后来才发现是数据生成工具生成出来的ID有问题导致的,只要保证iD不重复并且按照指定的类型建立ID就可以了

  9. jenkins插件set build description使用规则

    配置前要注意的点: 先安装插件:set build description 安装该插件后,在[Post-build Actions]栏目中会多出description setter功能,可以实现构建完 ...

  10. Python 进阶_OOP 面向对象编程_类属性和方法

    目录 目录 类属性 调用类属性 查看类属性 特殊的类属性 类方法 真构造器 __new__ 类属性 在理解类属性之前要先搞清楚 实例属性 和 函数属性 之间的区别: 1. 实例属性:指的是实例化类对象 ...