android 动画变换,Android 动画基础-变换动画(Twen Animation)

Android中有四大动画,它们分别是:变换动画(Tween Animation),帧动画(frame Animation),布局动画(layout Animation),属性动画(Property Animation )。今天我在这里简单的说下变换动画。

Animation的通用属性

xml属性

java方法

解释

android:detachWallpaper

setDetachWallpaper(boolean)

是否在在壁纸上运行

android:duration

setDuration(long)

动画持续时间,单位为毫秒

android:fillAfter

setFillAfter(boolean)

控件结束后是否保持动画最后的状态

android:fillBefore

setFillBefore(boolean)

控件结束后是否还原到开始动画的状态

android:fillEnabled

setFillEnabled(boolean)

与android:fillBefore效果相同

android:interpolator

setInterpolator(Interpolator)

设定插值器(指定的动画效果,譬如回弹等)

android:repeatCount

setRepeatCount(int)

动画重复次数

android:repeatMode

setRepeatMode(int)

重复类型有两个值,reverse表示倒序回放,restart表示从头播放

android:startOffset

setStartOffset(long)

调用start函数之后等待开始运行的时间,单位为毫秒

android:zAdjustment

setZAdjustment(int)

表示被设置动画的内容运行时在Z轴上的位置默认为normal

变换动画中分为4种:透明度动画,缩放动画,旋转动画,位移动画。今天我在这里会一一说明变换动画的四种动画的使用方法.

1.透明度动画

(1). 属性

xml属性

java方法

解释

android:fromAlpha

AplphaAnimation(float fromAlpah,...)

动画开始的透明度(0.0,到1.0,0.0是全透明,1.0是不透明)

android:toAlppha

AlphaAnimation(..., float toAlpha)

动画结束的透明度

(2). 用法

我们现在res文件下新建一个anim文件夹,用来存放动画的xml文件。

anim下的xml代码

android:duration="1000"

android:fromAlpha="0.1"

android:toAlpha="1.0"

/>

布局文件xml代码

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="com.example.Tween_Animation.Alpha_MainActivity" >

android:id="@+id/button_scale"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

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

android:gravity="center"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/imageview_scale"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/ic_launcher" />

activity中的JAVA代码

package com.example.Tween_Animation;

import com.example.androidanimation.R;

import android.app.Activity;

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

public class Alpha_MainActivity extends Activity implements OnClickListener{

private Button button = null;

private ImageView imageview = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imageview = (ImageView) findViewById(R.id.imageview_scale);

button = (Button) findViewById(R.id.button_scale);

button.setOnClickListener(this);

}

@Override

public void onClick(View v) {

//通过AnimationUtils里面的一个静态方法--loadAnimation用来绑定这个动画变换的xml文件

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

//imageview加载animation对象

imageview.startAnimation(animation);

}

}

2.缩放动画

(1). 属性

xml属性

JAVA方法

解释

android:fromXScale

ScaleAnimation(float fromX, …)

初始X轴缩放比例,1.0表示无变化

android:toXScale

ScaleAnimation(…, float toX, …)

结束X轴缩放比例

android:fromYScale

ScaleAnimation(…, float fromY, …)

初始Y轴缩放比例

android:toYScale

ScaleAnimation(…, float toY, …)

结束Y轴缩放比例

android:pivotX

ScaleAnimation(…, float pivotX, …)

缩放起点X轴坐标(数值、百分数、百分数p,譬如50表示以当前View左上角坐标加50px为初始点、50%表示以当前View的左上角加上当前View宽高的50%做为初始点、50%p表示以当前View的左上角加上父控件宽高的50%做为初始点)

ScaleAnimation(…, float pivotX, …)

ScaleAnimation(…, float pivotY)

缩放七点Y轴坐标,同上规律

(2). 用法

A.anim下的xml代码

android:duration="5000"

android:fromXScale="0.0"

android:fromYScale="0.0"

android:toYScale="1.0"

android:toXScale="1.0"

android:pivotX="50%"

android:pivotY="50%"

/>

B.布局文件xml代码

xml代码:同上的布局代码

C.activity的代码

package com.example.Tween_Animation;

import com.example.androidanimation.R;

import android.app.Activity;

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

public class Scale_MainActivity extends Activity implements OnClickListener{

private Button button = null;

private ImageView imageview = null;

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

button = (Button) findViewById(R.id.button_scale);

imageview = (ImageView) findViewById(R.id.imageview_scale);

button.setOnClickListener(this);

}

public void onClick(View v) {

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

imageview.startAnimation(animation);

}

}

3.旋转动画

(1). 属性

xml属性

JAVA方法

解释

android:fromDegrees

RotateAnimation(float fromDegrees, …)

旋转开始角度,正代表顺时针度数,负代表逆时针度数

android:toDegrees

RotateAnimation(…, float toDegrees, …)

旋转结束角度,正代表顺时针度数,负代表逆时针度数

android:pivotX

RotateAnimation(…, float pivotX, …)

缩放起点X坐标(数值、百分数、百分数p,譬如50表示以当前View左上角坐标加50px为初始点、50%表示以当前View的左上角加上当前View宽高的50%做为初始点、50%p表示以当前View的左上角加上父控件宽高的50%做为初始点)

android:pivotY

RotateAnimation(…, float pivotY)

缩放起点Y坐标,同上规律

(2). 用法

A. anim下的xml代码

android:repeatCount="5"

android:duration="1000"

android:fromDegrees="0"

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

android:pivotX="50%"

android:pivotY="50%"

android:toDegrees="+360" />

B.布局文件xml代码

xml代码:同上的布局代码

C. activity的代码

package com.example.Tween_Animation;

import com.example.androidanimation.R;

import android.app.Activity;

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

public class Rotate_MainActivity extends Activity implements OnClickListener{

private Button button = null;

private ImageView imageview = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imageview = (ImageView) findViewById(R.id.imageview_scale);

button = (Button) findViewById(R.id.button_scale);

button.setOnClickListener(this);

}

@Override

public void onClick(View v) {

//通过AnimationUtils里面的一个静态方法--loadAnimation用来绑定这个动画变换的xml文件

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

//imageview加载animation对象

imageview.startAnimation(animation);

}

}

4.位移动画

(1). 属性

xml属性

JAVA方法

解释

android:fromXDelta

TranslateAnimation(float fromXDelta, …)

起始点X轴坐标(数值、百分数、百分数p,譬如50表示以当前View左上角坐标加50px为初始点、50%表示以当前View的左上角加上当前View宽高的50%做为初始点、50%p表示以当前View的左上角加上父控件宽高的50%做为初始点)

android:fromYDelta

TranslateAnimation(…, float fromYDelta, …)

起始点Y轴从标,同上规律

android:toXDelta

TranslateAnimation(…, float toXDelta, …)

结束点X轴坐标,同上规律

android:toYDelta

TranslateAnimation(…, float toYDelta)

结束点Y轴坐标,同上规律

(2). 用法

A.anim下的xml代码

android:duration="1000"

android:fromXDelta="10"

android:fromYDelta="10"

android:toXDelta="1000"

android:toYDelta="1000" />

B.布局文件xml代码

xml代码:同上的布局代码

C. activity的代码

package com.example.Tween_Animation;

import com.example.androidanimation.R;

import android.app.Activity;

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

public class Translate_MainActivity extends Activity implements OnClickListener{

private Button button = null;

private ImageView imageview = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imageview = (ImageView) findViewById(R.id.imageview_scale);

button = (Button) findViewById(R.id.button_scale);

button.setOnClickListener(this);

}

@Override

public void onClick(View v) {

//通过AnimationUtils里面的一个静态方法--loadAnimation用来绑定这个动画变换的xml文件

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

//imageview加载animation对象

imageview.startAnimation(animation);

}

}

以上就是变换动画中的四种情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值