您的位置:首页 > 移动开发 > Android开发

Android 动画深入分析

2015-09-12 21:42 495 查看
一些娱乐动画安德鲁斯被广泛使用应用上述的。在不牺牲性能,它可以带来非常好的体验,下面会解释具体的实现安卓动画。知识的学校一个明确清晰的白色。

动画类型

Android的animation由四种类型组成

XML中
alpha
渐变透明度动画效果
scale
渐变尺寸伸缩动画效果
translate
画面转换位置移动动画效果
rotate
画面转移旋转动画效果
JavaCode中
AlphaAnimation
渐变透明度动画效果
ScaleAnimation
渐变尺寸伸缩动画效果
TranslateAnimation
画面转换位置移动动画效果
RotateAnimation
画面转移旋转动画效果
Android动画模式

Animation主要有两种动画模式:
一种是tweened animation(渐变动画)
XML中
JavaCode
alpha
AlphaAnimation
scale
ScaleAnimation
一种是frame by frame(画面转换动画)
XML中
JavaCode
translate
TranslateAnimation
rotate
RotateAnimation
怎样在XML文件里定义动画

① 打开Eclipse。新建Androidproject
② 在res文件夹中新建anim文件夹
③ 在anim文件夹中新建一个myanim.xml(注意文件名称小写)
④ 加入XML的动画代码

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

>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha/>
<scale/>
<translate/>
<rotate/>
</set>

Android动画解析--XML

<alpha>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="3000"
/>
<!-- 透明度控制动画效果 alpha
浮点型值:
fromAlpha 属性为动画起始时透明度
toAlpha   属性为动画结束时透明度
说明:
0.0表示全然透明
1.0表示全然不透明
以上值取0.0-1.0之间的float数据类型的数字

长整型值:
duration  属性为动画持续时间
说明:
时间以毫秒为单位
-->
</set>

<scale>

<?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" />
</set>
<!-- 尺寸伸缩动画效果 scale
属性:interpolator 指定一个动画的插入器
有三种动画插入器:
accelerate_decelerate_interpolator  加速-减速 动画插入器
accelerate_interpolator        加速-动画插入器
decelerate_interpolator        减速- 动画插入器
其它的属于特定的动画效果
浮点型值:

fromXScale 属性为动画起始时 X坐标上的伸缩尺寸
toXScale   属性为动画结束时 X坐标上的伸缩尺寸

fromYScale 属性为动画起始时Y坐标上的伸缩尺寸
toYScale   属性为动画结束时Y坐标上的伸缩尺寸

说明:
以上四种属性值

0.0表示收缩到没有
1.0表示正常无伸缩
值小于1.0表示收缩
值大于1.0表示放大

pivotX     属性为动画相对于物件的X坐标的開始位置
pivotY     属性为动画相对于物件的Y坐标的開始位置

说明:
以上两个属性值 从0%-100%中取值
50%为物件的X或Y方向坐标上的中点位置

长整型值:
duration  属性为动画持续时间
说明:   时间以毫秒为单位

布尔型值:
fillAfter 属性 当设置为true 。该动画转化在动画结束后被应用
-->

<translate>

<?

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 位置转移动画效果
整型值:
fromXDelta 属性为动画起始时 X坐标上的位置
toXDelta   属性为动画结束时 X坐标上的位置
fromYDelta 属性为动画起始时 Y坐标上的位置
toYDelta   属性为动画结束时 Y坐标上的位置
注意:
没有指定fromXType toXType fromYType toYType 时候,
默认是以自己为相对參照物
长整型值:
duration  属性为动画持续时间
说明:   时间以毫秒为单位
-->
</set>

<rotate>

<?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 旋转动画效果
属性:interpolator 指定一个动画的插入器
有三种动画插入器:
accelerate_decelerate_interpolator   加速-减速 动画插入器
accelerate_interpolator               加速-动画插入器
decelerate_interpolator               减速- 动画插入器
其它的属于特定的动画效果

浮点数型值:
fromDegrees 属性为动画起始时物件的角度
toDegrees   属性为动画结束时物件旋转的角度 能够大于360度

说明:
当角度为负数——表示逆时针旋转
当角度为正数——表示顺时针旋转
(负数from——to正数:顺时针旋转)
(负数from——to负数:逆时针旋转)
(正数from——to正数:顺时针旋转)
(正数from——to负数:逆时针旋转)

pivotX     属性为动画相对于物件的X坐标的開始位置
pivotY     属性为动画相对于物件的Y坐标的開始位置

说明:        以上两个属性值 从0%-100%中取值
50%为物件的X或Y方向坐标上的中点位置

长整型值:
duration  属性为动画持续时间
说明:       时间以毫秒为单位
-->
</set>

怎样使用XML中的动画效果

public static Animation loadAnimation (Context context, int id)
//第一个參数Context为程序的上下文
//第二个參数id为动画XML文件的引用
//样例:
myAnimation= AnimationUtils.loadAnimation(this,R.anim.my_action);
//使用AnimationUtils类的静态方法loadAnimation()来载入XML中的动画XML文件

怎样在Java代码中定义动画

//在代码中定义 动画实例对象
private Animation myAnimation_Alpha;
private Animation myAnimation_Scale;
private Animation myAnimation_Translate;
private Animation myAnimation_Rotate;

//依据各自的构造方法来初始化一个实例对象
myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);

myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);

myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);

Android动画解析--JavaCode

AlphaAnimation

① AlphaAnimation类对象定义

private AlphaAnimation myAnimation_Alpha

② AlphaAnimation类对象构造

AlphaAnimation(float fromAlpha, float toAlpha)
//第一个參数fromAlpha为 动画開始时候透明度
//第二个參数toAlpha为 动画结束时候透明度
myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);
//说明:
//                0.0表示全然透明
//                1.0表示全然不透明

③ 设置动画持续时间

myAnimation_Alpha.setDuration(5000);
//设置时间持续时间为 5000毫秒

ScaleAnimation

① ScaleAnimation类对象定义

private AlphaAnimation myAnimation_Alpha;

② ScaleAnimation类对象构造

ScaleAnimation(float fromX, float toX, float fromY, float toY,
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
//第一个參数fromX为动画起始时 X坐标上的伸缩尺寸
//第二个參数toX为动画结束时 X坐标上的伸缩尺寸
//第三个參数fromY为动画起始时Y坐标上的伸缩尺寸
//第四个參数toY为动画结束时Y坐标上的伸缩尺寸
/*说明:
以上四种属性值
0.0表示收缩到没有
1.0表示正常无伸缩
值小于1.0表示收缩
值大于1.0表示放大
*/
//第五个參数pivotXType为动画在X轴相对于物件位置类型
//第六个參数pivotXValue为动画相对于物件的X坐标的開始位置
//第七个參数pivotXType为动画在Y轴相对于物件位置类型
//第八个參数pivotYValue为动画相对于物件的Y坐标的開始位置
myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

③ 设置动画持续时间

myAnimation_Scale.setDuration(700);
//设置时间持续时间为 700毫秒

TranslateAnimation

① TranslateAnimation类对象定义

private AlphaAnimation myAnimation_Alpha;

② TranslateAnimation类对象构造

TranslateAnimation(float fromXDelta, float toXDelta,
float fromYDelta, float toYDelta)
//第一个參数fromXDelta为动画起始时 X坐标上的移动位置
//第二个參数toXDelta为动画结束时 X坐标上的移动位置
//第三个參数fromYDelta为动画起始时Y坐标上的移动位置
//第四个參数toYDelta为动画结束时Y坐标上的移动位置

③ 设置动画持续时间

myAnimation_Translate.setDuration(2000);
//设置时间持续时间为 2000毫秒

RotateAnimation

① RotateAnimation类对象定义

private AlphaAnimation myAnimation_Alpha;

② RotateAnimation类对象构造

RotateAnimation(float fromDegrees, float toDegrees,
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
//第一个參数fromDegrees为动画起始时的旋转角度
//第二个參数toDegrees为动画旋转到的角度
//第三个參数pivotXType为动画在X轴相对于物件位置类型
//第四个參数pivotXValue为动画相对于物件的X坐标的開始位置
//第五个參数pivotXType为动画在Y轴相对于物件位置类型
//第六个參数pivotYValue为动画相对于物件的Y坐标的開始位置
myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);

③ 设置动画持续时间

myAnimation_Rotate.setDuration(3000);
//设置时间持续时间为 3000毫秒

怎样使用Java代码中的动画效果

使用从View父类继承过来的方法startAnimation()来为View或是子类View等等加入一个动画效果

public void startAnimation (Animation animation)


以上介绍的是补间动画以下介绍一下帧动画

关于补间动画和帧动画的差别。也能够简单理解为:

补间动画相当于连贯的电影。而帧动画相当于走马灯,转的越快越连贯。

补间动画省空间但消耗资源。帧动画占用空间大,可是资源消耗少。

依据实际情况进行取舍。

实例(自己定义ProgressDialog帧动画):

首先在res文件夹下的anim文件夹下新建文件progress_round.xml内容例如以下:

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

>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/progress_1" android:duration="100"/>
<item android:drawable="@drawable/progress_2" android:duration="100"/>
<item android:drawable="@drawable/progress_3" android:duration="100"/>
<item android:drawable="@drawable/progress_4" android:duration="100"/>
<item android:drawable="@drawable/progress_5" android:duration="100"/>
<item android:drawable="@drawable/progress_6" android:duration="100"/>
<item android:drawable="@drawable/progress_7" android:duration="100"/>
<item android:drawable="@drawable/progress_8" android:duration="60"/>
</animation-list>

在style以下加入:

<style name="CustomDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>

<style name="CustomProgressDialog" parent="@style/CustomDialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>

然后在ProgressDialog布局文件里引用动画,设置为imageview的背景:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/loadingImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@anim/progress_round"/>
<TextView
android:id="@+id/id_tv_loadingmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="20dp"/>
</LinearLayout>

接下来就是复写dialog:

package com.pztuan.common.view;
import com.zhijing.pztuan.R;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomProgressDialog extends Dialog {
private Context context = null;
private static CustomProgressDialog customProgressDialog = null;

public CustomProgressDialog(Context context){
super(context);
this.context = context;
}

public CustomProgressDialog(Context context, int theme) {
super(context, theme);
}

public static CustomProgressDialog createDialog(Context context){//引用style,加入自己定义布局
customProgressDialog = new CustomProgressDialog(context,R.style.CustomProgressDialog);
customProgressDialog.setContentView(R.layout.customprogressdialog);
customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;

return customProgressDialog;
}

public void onWindowFocusChanged(boolean hasFocus){

if (customProgressDialog == null){
return;
}

ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loadingImageView);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
animationDrawable.start();
}

/**
*
* [Summary]
*       setTitile 标题
* @param strTitle
* @return
*
*/
public CustomProgressDialog setTitile(String strTitle){
return customProgressDialog;
}

/**
*
* [Summary]
*       setMessage 提示内容
* @param strMessage
* @return
*
*/
public CustomProgressDialog setMessage(String strMessage){
TextView tvMsg = (TextView)customProgressDialog.findViewById(R.id.id_tv_loadingmsg);

if (tvMsg != null){
tvMsg.setText(strMessage);
}

return customProgressDialog;
}
}

使用:

/**
* @author sunglasses
* @param tip 载入提示信息
*/
public void showLoadingDialog(String tip) {

if (progressDialog == null) {
progressDialog = CustomProgressDialog.createDialog(this);
if(tip!=null){
progressDialog.setMessage(tip);
}else{
progressDialog.setMessage("正在载入...");
}
}
progressDialog.show();
}

public void dismissLoadingDialog() {
if (progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
}

效果图:



參考:http://www.eoeandroid.com/thread-564-1-1.html

如有问题,请留言,转载注明出处。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: