安卓开机动画卡刷包动画包怎么准备?能详细点!?

Android 过渡动画原来可以这样写 - 简书
Android 过渡动画原来可以这样写
android.transition.TransitionManager
android.transition.Transition 抽象类
TransitionSet
AutoTransition
ChangeBounds
Visibility 抽象类
Explode ( design 包中无适配)
Slide ( design 包中无适配)
ChangeClipBounds ( design 包中无适配)
ChangeImageTransform ( design 包中无适配)
ChangeScroll ( design 包中无适配)
ChangeTransform ( design 包中无适配)
Visibility 抽象类的子类:Fade, Explode, Slide 动画作用于 View 的 Visibility 属性改变的时候。
对应的适配包在designcom.android.support:design:x.x.x包中
compile 'com.android.support:design:25.0.0'
更好的适配方案: (使用的时候注意导包 com.transitionseverywhere.xxx)
compile "com.andkulikov:transitionseverywhere:1.6.9"
&!--more--&
下面的例子 使用适配包 compile "com.andkulikov:transitionseverywhere:1.6.9" 测试系统 Android4.1
布局关键代码:
&LinearLayout
android:id="@+id/ll_container_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"&
android:id="@+id/btn_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fad"/&
android:id="@+id/tv_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Transitions are awesome!"
android:visibility="gone"/&
&/LinearLayout&
Activity中代码:
final LinearLayout transitionsContainer = (LinearLayout) findViewById(R.id.ll_container_one);
final TextView text = (TextView) findViewById(R.id.tv_one);
final Button button = (Button) findViewById(R.id.btn_one);
button.setOnClickListener(v -& {
TransitionManager.beginDelayedTransition(transitionsContainer);
if (text.getVisibility() == View.VISIBLE) {
text.setVisibility(View.GONE);
text.setVisibility(View.VISIBLE);
最终效果:
TransitionManager 中的 beginDelayedTransition 方法:
public static void beginDelayedTransition(final ViewGroup sceneRoot){...}
public static void beginDelayedTransition(final ViewGroup sceneRoot, Transition transition) {...}
方法一:效果如上面的默认效果
方法二:参数而用于定制动画效果, 参数为 Transition 的子类,常用的有:Fade, ChangeBounds, Slide,
对用画具体效果做定制: 设置动画时间,设置动画加速度,设置动画延时
transition.setDuration(300);
transition.setInterpolator(new FastOutSlowInInterpolator());
transition.setStartDelay(200);
Fade 淡出淡入
布局如上 更改Activity中代码:
final LinearLayout transitionsContainer = (LinearLayout) findViewById(R.id.ll_container_one);
final TextView text = (TextView) findViewById(R.id.tv_one);
final Button button = (Button) findViewById(R.id.btn_one);
button.setOnClickListener(v -& {
Fade fade = new Fade();
TransitionManager.beginDelayedTransition(transitionsContainer, fade);
if (text.getVisibility() == View.VISIBLE) {
text.setVisibility(View.GONE);
text.setVisibility(View.VISIBLE);
Slide 移动
布局如上 更改Activity中代码:
final LinearLayout transitionsContainer = (LinearLayout) findViewById(R.id.ll_container_one);
final TextView text = (TextView) findViewById(R.id.tv_one);
final Button button = (Button) findViewById(R.id.btn_one);
button.setOnClickListener(v -& {
// Slide slide = new Slide();
Slide slide = new Slide(Gravity.RIGHT);
TransitionManager.beginDelayedTransition(transitionsContainer, slide);
if (text.getVisibility() == View.VISIBLE) {
text.setVisibility(View.GONE);
text.setVisibility(View.VISIBLE);
Slide 构造方法
* Constructor using the default {@link Gravity#BOTTOM}
* slide edge direction.
public Slide() {
setSlideEdge(Gravity.BOTTOM);
设置滑出方向:
* Constructor using the provided slide edge direction.
public Slide(@GravityFlag int slideEdge) {
setSlideEdge(slideEdge);
Scale 缩放
布局如上 更改Activity中代码:
final LinearLayout transitionsContainer = (LinearLayout) findViewById(R.id.ll_container_one);
final TextView text = (TextView) findViewById(R.id.tv_one);
final Button button = (Button) findViewById(R.id.btn_one);
button.setOnClickListener(v -& {
// Scale scale = new Scale();
Scale scale = new Scale(0.7f);
TransitionManager.beginDelayedTransition(transitionsContainer, scale);
if (text.getVisibility() == View.VISIBLE) {
text.setVisibility(View.GONE);
text.setVisibility(View.VISIBLE);
Scale 构造方法
public Scale() {
* @param disappearedScale Value of scale on start of appearing or in finish of disappearing.
Default value is 0. Can be useful for mixing some Visibility
transitions, for example Scale and Fade
public Scale(float disappearedScale) {
setDisappearedScale(disappearedScale);
参数设置了缩放起始值或者最终值。
TransitionSet 动画集合
final LinearLayout transitionsContainer = (LinearLayout) findViewById(R.id.ll_container_one);
final TextView text = (TextView) findViewById(R.id.tv_one);
final Button button = (Button) findViewById(R.id.btn_one);
button.setOnClickListener(v -& {
TransitionSet set = new TransitionSet()
.addTransition(new Scale(0.7f))
.addTransition(new Fade())
.setInterpolator(visible ? new LinearOutSlowInInterpolator() :
new FastOutLinearInInterpolator());
TransitionManager.beginDelayedTransition(transitionsContainer, set);
if (text.getVisibility() == View.VISIBLE) {
text.setVisibility(View.GONE);
text.setVisibility(View.VISIBLE);
TransitionSet
Recolor 颜色渐变
空间的背景色或者文字颜色改变的动画
&LinearLayout
android:id="@+id/ll_container_recolor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:visibility="visible"&
android:id="@+id/btn_recolor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recolor"/&
android:id="@+id/btn_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Normal"
android:visibility="visible"/&
&/LinearLayout&
Activity 代码
final ViewGroup transitionsContainerRecolor = (ViewGroup) findViewById(R.id.ll_container_recolor);
final Button btnRecolor = (Button) findViewById(R.id.btn_recolor);
final Button btnNormal = (Button) findViewById(R.id.btn_normal);
int green = getResources().getColor(R.color.green);
int white = getResources().getColor(R.color.white);
int grey = getResources().getColor(R.color.grey);
btnRecolor.setOnClickListener(v -& {
TransitionManager.beginDelayedTransition(transitionsContainerRecolor, new Recolor());
btnRecolor.setBackgroundColor(visible ? green : white); // 无动画效果
btnRecolor.setTextColor(visible ? white : grey);
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.JELLY_BEAN) {
btnRecolor.setBackground(new ColorDrawable(visible ? green : white));
btnRecolor.setBackgroundDrawable(new ColorDrawable(visible ? green : white));
visible = !
btnNormal.setOnClickListener(v -& {
btnNormal.setBackgroundColor(visible ? green : white);
btnNormal.setTextColor(visible ? white : green);
visible = !
注意: btnRecolor.setBackgroundColor(visible ? green : white); // 无动画效果 通过 setBackgroundColor 背景色时没有动画效果,可以使用 setBackground, setBackgroundDrawable
Rotate 旋转
&LinearLayout
android:id="@+id/ll_container_rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:visibility="visible"&
android:id="@+id/btn_rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate"/&
&ImageView
android:id="@+id/iv_rotate"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:src="@drawable/ic_clear_black_24dp"
android:visibility="visible"/&
&/LinearLayout&
Activity 代码
final ViewGroup transitionsContainerRotate = (ViewGroup) findViewById(R.id.ll_container_rotate);
final Button btnRotate = (Button) findViewById(R.id.btn_rotate);
ImageView ivRotate = (ImageView) findViewById(R.id.iv_rotate);
btnRotate.setOnClickListener(v -& {
TransitionManager.beginDelayedTransition(transitionsContainerRotate, new Rotate());
ivRotate.setRotation(isRotated ? 0 : 135);
isRotated = !isR
ChangeText 文字改变时动画
&LinearLayout
android:id="@+id/ll_container_change_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:visibility="visible"&
android:id="@+id/btn_change_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ChangeText"/&
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Transitions are awesome!"
android:visibility="visible"/&
&/LinearLayout&
Activity 代码
final LinearLayout transitionsContainerChangeText = (LinearLayout) findViewById(R.id.ll_container_change_text);
final TextView tvText = (TextView) findViewById(R.id.tv_text);
final Button btnChangeText = (Button) findViewById(R.id.btn_change_text);
String secText = " Second Text";
String firstText = "First Text";
btnChangeText.setOnClickListener(v -& {
TransitionManager.beginDelayedTransition(transitionsContainerChangeText,
new ChangeText().setChangeBehavior(ChangeText.CHANGE_BEHAVIOR_OUT_IN));
tvText.setText(isFirstText ? secText : firstText);
isFirstText = !isFirstT
ChangeText
TransitionName 制作 打乱动画
使用场景:
需要动态生成 ViewGroup 的子 View , 并且子 View 内容需要更新时
需要制作随机时
注意:这里把 Button 移到了 LinearLayout 的外面,原因是一会创建子 View 的时候会先删除所有子 View(Button 也会被删除)。
android:id="@+id/btn_transition_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打乱"/&
&LinearLayout
android:id="@+id/ll_container_transition_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:visibility="visible"&
Activity 代码
// TransitionName 做打乱动画
final LinearLayout transitionsContainerTransitionName = (LinearLayout) findViewById(R.id.ll_container_transition_name);
final Button btnTransitionName = (Button) findViewById(R.id.btn_transition_name);
LayoutInflater inflater = LayoutInflater.from(this);
ArrayList&String& titles = new ArrayList&&();
for (int i = 0; i & 4; i++) {
titles.add(String.format(Locale.CHINA, "Item %d", i));
createViews(inflater, transitionsContainerTransitionName, titles);
btnTransitionName.setOnClickListener(v -& {
TransitionManager.beginDelayedTransition(transitionsContainerTransitionName, new ChangeBounds());
Collections.shuffle(titles);
createViews(inflater, transitionsContainerTransitionName, titles);
// 独立的方法
// In createViews we should provide transition name for every view.
private static void createViews(LayoutInflater inflater, ViewGroup layout, List&String& titles) {
layout.removeAllViews();
for (String title : titles) {
TextView textView = (TextView) inflater.inflate(R.layout.text_view, layout, false);
textView.setText(title);
TransitionManager.setTransitionName(textView, title);
layout.addView(textView);
TransitionName
Explode and Propagation 爆炸和传播
爆炸效果,和移动过渡动画比较相似,不过子 View 的移动方向是由其所在的位置决定的。子 View 的移动方向需要通过计算得到(通过 setEpicenterCallback 方法)
这个例子使用 RecyclerView 和 GridLayoutManager 做基本布局,点击里面的 item 让其消失。
public void onClick(View clickedView) {
// save rect of view in screen coordinates
final Rect viewRect = new Rect();
clickedView.getGlobalVisibleRect(viewRect);
// create Explode transition with epicenter
Transition explode = new Explode()
.setEpicenterCallback(new Transition.EpicenterCallback() {
public Rect onGetEpicenter(Transition transition) {
return viewR
explode.setDuration(1000);
TransitionManager.beginDelayedTransition(recyclerView, explode);
// remove all views from Recycler View
recyclerView.setAdapter(null);
ChangeImageTransform
Path (Curved) motion 路径过渡动画
所有的过渡动画都需要两个值:起始值和结束值
比如:通过 ChangeBounds 来改变 view 的位置,通过 setPathMotion 来提供路径
&FrameLayout
android:id="@+id/fl_container_path_motion"
android:layout_width="match_parent"
android:layout_height="match_parent"&
android:id="@+id/btn_path_motion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Path Motion"/&
&/FrameLayout&
Activity 代码
// path motion 路径过渡动画
final FrameLayout transitionsContainerPathMotion = (FrameLayout) findViewById(R.id.fl_container_path_motion);
Button btnPathMotion = (Button) findViewById(R.id.btn_path_motion);
btnPathMotion.setOnClickListener(v -& {
TransitionManager.beginDelayedTransition(transitionsContainerPathMotion,
new ChangeBounds().setPathMotion(new ArcMotion()).setDuration(500));
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) btnPathMotion.getLayoutParams();
params.gravity = isReturnAnimation ? (Gravity.LEFT | Gravity.TOP) :
(Gravity.BOTTOM | Gravity.RIGHT);
btnPathMotion.setLayoutParams(params);
isReturnAnimation = !isReturnA
这里通过 LayoutParams 来控制 Button 在其父控件中的位置。
Path Motion
Targets 设置动画的目标对象
定义动画:
TransitionManager.beginDelayedTransition( viewGroup, transition);
默认情况下,这里的 transition 动画会作用于 viewGroup 中的所有子 View
当我们需要在一个 ViewGroup 中定义多个动画,作用于不同的子 View 该如何做?
比如 让一个 TextView 移动, 另一个 TextView 淡出
&LinearLayout
android:id="@+id/ll_container_target"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:visibility="visible"&
android:id="@+id/btn_target"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Target"/&
android:id="@+id/tv_target_fade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Transitions are awesome fade!"
android:visibility="visible"/&
android:id="@+id/tv_target_slide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Transitions are awesome slide!"
android:visibility="visible"/&
&/LinearLayout&
Activity 代码
// Targets 设置动画的目标对象
final LinearLayout transitionsContainerTarget = (LinearLayout) findViewById(R.id.ll_container_target);
final Button btnTarget = (Button) findViewById(R.id.btn_target);
final TextView tvFade = (TextView) findViewById(R.id.tv_target_fade);
final TextView tvSlide = (TextView) findViewById(R.id.tv_target_slide);
btnTarget.setOnClickListener(v -& {
Slide slide = new Slide(Gravity.RIGHT);
slide.excludeTarget(tvFade, true);
Fade fade = new Fade();
fade.excludeTarget(tvSlide, true);
TransitionSet transitionSet = new TransitionSet()
.addTransition(slide)
.addTransition(fade);
TransitionManager.beginDelayedTransition(transitionsContainerTarget, transitionSet);
if (tvFade.getVisibility() == View.VISIBLE) {
tvFade.setVisibility(View.GONE);
tvSlide.setVisibility(View.GONE);
tvFade.setVisibility(View.VISIBLE);
tvSlide.setVisibility(View.VISIBLE);
Target处理
transition 其他有关 target 方法
Methods to add target:
addTarget(View target) — view itself
addTarget(int targetViewId) — id of view
addTarget(String targetName) — do you remember about method TransitionManager.setTransitionName?
addTarget(Class targetType) — for example android.widget.TextView.class
To remove target:
removeTarget(View target)
removeTarget(int targetId)
removeTarget(String targetName)
removeTarget(Class target)
To exclude some views:
excludeTarget(View target, boolean exclude)
excludeTarget(int targetId, boolean exclude)
excludeTarget(Class type, boolean exclude)
excludeTarget(Class type, boolean exclude)
And for excluding all children of some ViewGroup:
excludeChildren(View target, boolean exclude)
excludeChildren(int targetId, boolean exclude)
excludeChildren(Class type, boolean exclude)
使用 xml 创建 Transition
&?xml version="1.0" encoding="utf-8"?&
&transitionSet xmlns:app="/apk/res-auto"
app:transitionOrdering="together"
app:duration="400"&
&changeBounds/&
&changeImageTransform/&
app:fadingMode="fade_in"
app:startDelay="200"&
&target app:targetId="@id/transition_title"/&
&/targets&
&/transitionSet&
TransitionInflater.from(getContext()).inflateTransition(R.anim.my_the_best_transition);
Activity and Fragment transitions
Custom Transitions
上面所有代码在:Android Animation动画(超详细) - CSDN博客
Android Animation动画(超详细)
转自/content/13/35.shtml
Animations
一、Animations介绍
Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转、缩放、淡入淡出等,这些效果可以应用在绝大多数的控件中。&
二、Animations的分类
Animations从总体上可以分为两大类:
1.Tweened Animations:该类Animations提供了旋转、移动、伸展和淡出等效果。Alpha——淡入淡出,Scale——缩放效果,Rotate——旋转,Translate——移动效果。
2.Frame-by-frame Animations:这一类Animations可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示。
三、Animations的使用方法(代码中使用)
Animations extends Object implements Cloneable&
&使用TweenedAnimations的步骤:
1.创建一个AnimationSet对象(Animation子类);
2.增加需要创建相应的Animation对象;
3.更加项目的需求,为Animation对象设置相应的数据;
4.将Animatin对象添加到AnimationSet对象当中;
5.使用控件对象开始执行AnimationSet。
  Tweened Animations的分类
  1、Alpha:淡入淡出效果
  2、Scale:缩放效果
  3、Rotate:旋转效果
  4、Translate:移动效果
Animation的四个子类:
  AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation
四、具体实现
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:orientation=&vertical& &
&LinearLayout
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:orientation=&horizontal& &
android:id=&@+id/rotateButton&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&旋转& /&
android:id=&@+id/scaleButton&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&缩放& /&
android:id=&@+id/alphaButton&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&淡入淡出& /&
android:id=&@+id/translateButton&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&移动& /&
&/LinearLayout&
&LinearLayout
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:orientation=&vertical& &
&ImageView
android:id=&@+id/image&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_centerInParent=&true&
android:src=&@drawable/an& /&
&/LinearLayout&
&/LinearLayout&
2、.java文件
importandroid.app.A
importandroid.os.B
importandroid.view.V
importandroid.view.View.OnClickL
import android.view.animation.AlphaA
import android.view.animation.A
importandroid.view.animation.AnimationS
importandroid.view.animation.RotateA
importandroid.view.animation.ScaleA
import android.view.animation.TranslateA
importandroid.widget.B
importandroid.widget.ImageV
public class Animation1Activity extends Activity {
private Button rotateButton =
private Button scaleButton =
private Button alphaButton =
private Button translateButton =
private ImageView image =
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rotateButton = (Button)findViewById(R.id.rotateButton);
scaleButton = (Button)findViewById(R.id.scaleButton);
alphaButton = (Button)findViewById(R.id.alphaButton);
translateButton = (Button)findViewById(R.id.translateButton);
image = (ImageView)findViewById(R.id.image);
rotateButton.setOnClickListener(newRotateButtonListener());
scaleButton.setOnClickListener(newScaleButtonListener());
alphaButton.setOnClickListener(newAlphaButtonListener());
translateButton.setOnClickListener(
new TranslateButtonListener());
class AlphaButtonListener implementsOnClickListener{
public void onClick(View v) {
//创建一个AnimationSet对象,参数为Boolean型,
//true表示使用Animation的interpolator,false则是使用自己的
AnimationSet animationSet = new AnimationSet(true);
//创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//设置动画执行的时间
alphaAnimation.setDuration(500);
//将alphaAnimation对象添加到AnimationSet当中
animationSet.addAnimation(alphaAnimation);
//使用ImageView的startAnimation方法执行动画
image.startAnimation(animationSet);
class RotateButtonListener implementsOnClickListener{
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
//参数1:从哪个旋转角度开始
//参数2:转到什么角度
//后4个参数用于设置围绕着旋转的圆的圆心在哪里
//参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标
//参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
//参数5:确定y轴坐标的类型
//参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
image.startAnimation(animationSet);
class ScaleButtonListener implementsOnClickListener{
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
//参数1:x轴的初始值
//参数2:x轴收缩后的值
//参数3:y轴的初始值
//参数4:y轴收缩后的值
//参数5:确定x轴坐标的类型
//参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
//参数7:确定y轴坐标的类型
//参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
ScaleAnimation scaleAnimation = new ScaleAnimation(
0, 0.1f,0,0.1f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
scaleAnimation.setDuration(1000);
animationSet.addAnimation(scaleAnimation);
image.startAnimation(animationSet);
class TranslateButtonListener implementsOnClickListener{
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
//参数1~2:x轴的开始位置
//参数3~4:y轴的开始位置
//参数5~6:x轴的结束位置
//参数7~8:x轴的结束位置
TranslateAnimation translateAnimation =
new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,0.5f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
image.startAnimation(animationSet);
Tween Animations的通用方法
  1、setDuration(long durationMills)
  设置动画持续时间(单位:毫秒)
  2、setFillAfter(Boolean fillAfter)
  如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态
  3、setFillBefore(Boolean fillBefore)
  如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态
  4、setStartOffSet(long startOffSet)
  设置动画执行之前的等待时间
  5、setRepeatCount(int repeatCount)
  设置动画重复执行的次数
在代码中使用Animations可以很方便的调试、运行,但是代码的可重用性差,重复代码多。同样可以在xml文件中配置Animations,这样做可维护性变高了,只不过不容易进行调试。
一、在xml中使用Animations步骤
&&&&&&&1.在res文件夹下建立一个anim文件夹;
&&&&&&&2.创建xml文件,并首先加入set标签,更改标签如下:
&?xml version=&1.0& encoding=&utf-8&?&
&set xmlns:android=&/apk/res/android&
android:interpolator=&@android:anim/accelerate_interpolator&&
3.在该标签当中加入rotate,alpha,scale或者translate标签;
android:fromAlpha=&1.0&
android:toAlpha=&0.0&
android:startOffset=&500&
android:duration=&500&/&
4.在代码当中使用AnimationUtils当中装载xml文件,并生成Animation对象。因为Animation是AnimationSet的子类,所以向上转型,用Animation对象接收。
Animation animation = AnimationUtils.loadAnimation(
Animation1Activity.this, R.anim.alpha);
// 启动动画
image.startAnimation(animation);
二、具体实现
&1、&&alpha.xml
&?xml version=&1.0& encoding=&utf-8&?&
&set xmlns:android=&/apk/res/android&
android:interpolator=&@android:anim/accelerate_interpolator&&
&!-- fromAlpha和toAlpha是起始透明度和结束时透明度 --&
android:fromAlpha=&1.0&
android:toAlpha=&0.0&
android:startOffset=&500&
android:duration=&500&/&
2、&&rotate.xml
&?xml version=&1.0& encoding=&utf-8&?&
&set xmlns:android=&/apk/res/android&
android:interpolator=&@android:anim/accelerate_interpolator&&
fromDegrees:开始的角度
toDegrees:结束的角度,+表示是正的
pivotX:用于设置旋转时的x轴坐标
1)当值为&50&,表示使用绝对位置定位
2)当值为&50%&,表示使用相对于控件本身定位
3)当值为&50%p&,表示使用相对于控件的父控件定位
pivotY:用于设置旋转时的y轴坐标
android:fromDegrees=&0&
android:toDegrees=&+360&
android:pivotX=&50%&
android:pivotY=&50%&
android:duration=&1000&/&
3、&&scale.xml
&?xml version=&1.0& encoding=&utf-8&?&
&set xmlns:android=&/apk/res/android&
android:interpolator=&@android:anim/accelerate_interpolator&&
起始x轴坐标
android:fromXScale=&1.0&
android:toXScale=&0.0&
android:fromYScale=&1.0&
android:toYScale=&0.0&
android:pivotX=&50%&
android:pivotY=&50%&
android:duration=&1000&/&
4、&&translate.xml
&?xml version=&1.0& encoding=&utf-8&?&
&set xmlns:android=&/apk/res/android&
android:interpolator=&@android:anim/accelerate_interpolator&&
&translate
android:fromXDelta=&0%&
android:toXDelta=&100%&
android:fromYDelta=&0%&
android:toYDelta=&100%&
android:duration=&2000&/&
5、&&.java文件
importandroid.app.A
importandroid.os.B
importandroid.view.V
importandroid.view.View.OnClickL
import android.view.animation.A
importandroid.view.animation.AnimationU
import android.widget.B
import android.widget.ImageV
public class Animation1Activity extends Activity {
private Button rotateButton =
private Button scaleButton =
private Button alphaButton =
private Button translateButton =
private ImageView image =
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rotateButton = (Button) findViewById(R.id.rotateButton);
scaleButton = (Button) findViewById(R.id.scaleButton);
alphaButton = (Button) findViewById(R.id.alphaButton);
translateButton = (Button) findViewById(R.id.translateButton);
image = (ImageView) findViewById(R.id.image);
rotateButton.setOnClickListener(newRotateButtonListener());
scaleButton.setOnClickListener(newScaleButtonListener());
alphaButton.setOnClickListener(newAlphaButtonListener());
translateButton.setOnClickListener(newTranslateButtonListener());
class AlphaButtonListener implementsOnClickListener {
public void onClick(View v) {
// 使用AnimationUtils装载动画配置文件
Animation animation = AnimationUtils.loadAnimation(
Animation1Activity.this, R.anim.alpha);
// 启动动画
image.startAnimation(animation);
class RotateButtonListener implementsOnClickListener {
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(
Animation1Activity.this, R.anim.rotate);
image.startAnimation(animation);
class ScaleButtonListener implementsOnClickListener {
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(
Animation1Activity.this, R.anim.scale);
image.startAnimation(animation);
class TranslateButtonListener implementsOnClickListener {
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(Animation1Activity.this, R.anim.translate);
image.startAnimation(animation);
AnimationSet的具体使用方法
&&&&&&&1.AnimationSet是Animation的子类;
&&&&&&&2.一个AnimationSet包含了一系列的Animation;
&&&&&&&3.针对AnimationSet设置一些Animation的常见属性(如startOffset,duration等),可以被包含在AnimationSet当中的Animation集成;
例:一个AnimationSet中有两个Animation,效果叠加
第一种方法:
doubleani.xml
&?xml version=&1.0& encoding=&utf-8&?&
&set xmlns:android=&/apk/res/android&
android:interpolator=&@android:anim/accelerate_interpolator&
android:shareInterpolator=&true&&
&!-- fromAlpha和toAlpha是起始透明度和结束时透明度 --&
android:fromAlpha=&1.0&
android:toAlpha=&0.0&
android:startOffset=&500&
android:duration=&500&/&
&translate
android:fromXDelta=&0%&
android:toXDelta=&100%&
android:fromYDelta=&0%&
android:toYDelta=&100%&
android:duration=&2000&/&
.java文件中
classDoubleButtonListener implements OnClickListener {
public void onClick(View v) {
// 使用AnimationUtils装载动画配置文件
Animation animation = AnimationUtils.loadAnimation(
Animation2Activity.this, R.anim. doubleani);
// 启动动画
image.startAnimation(animation);
第二种方法:
.java文件中
classDoubleButtonListener implements OnClickListener {
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(alphaAnimation);
image.startAnimation(animationSet);
Interpolator的具体使用方法
&&&&&&&Interpolator定义了动画变化的速率,在Animations框架当中定义了一下几种Interpolator
?&&&&&&&&&AccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候速率快。
?&&&&&&&&&AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速
?&&&&&&&&&CycleInterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线
?&&&&&&&&&DecelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始减速
?&&&&&&&&&LinearInterpolator:动画以均匀的速率改变
分为以下几种情况:
1、在set标签中
&set xmlns:android=&/apk/res/android&
android:interpolator=&@android:anim/accelerate_interpolator&/&
2、如果在一个set标签中包含多个动画效果,如果想让这些动画效果共享一个Interpolator。
android:shareInterpolator=&true&
3、如果不想共享一个interpolator,则设置android:shareInterpolator=&true&,并且需要在每一个动画效果处添加interpolator。
android:interpolator=&@android:anim/accelerate_decelerate_interpolator&
android:fromAlpha=&1.0&
android:toAlpha=&0.0&
android:startOffset=&500&
android:duration=&500&/&
4、如果是在代码上设置共享一个interpolator,则可以在AnimationSet设置interpolator。
AnimationSet animationSet = newAnimationSet(true);
animationSet.setInterpolator(new AccelerateInterpolator());
5、如果不设置共享一个interpolator则可以在每一个Animation对象上面设置interpolator。AnimationSet animationSet = newAnimationSet(false);
alphaAnimation.setInterpolator(new AccelerateInterpolator());
rotateAnimation.setInterpolator(new DecelerateInterpolator());
Frame-By-Frame Animations的使用方法
&&&&&&&Frame-By-Frame Animations是一帧一帧的格式显示动画效果。类似于电影胶片拍摄的手法。
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:orientation=&vertical&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&&
&LinearLayout
android:orientation=&horizontal&
android:layout_height=&wrap_content&
android:layout_width=&wrap_content&&
android:id=&@+id/button&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&运动&/&
&/LinearLayout&
&LinearLayout
android:orientation=&vertical&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&&
&ImageView
android:id=&@+id/image&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_centerInParent=&true&/&
&/LinearLayout&
&/LinearLayout&
3、anim.xml
&?xml version=&1.0& encoding=&utf-8&?&
&animation-list xmlns:android=&/apk/res/android&
android:oneshot=&false&&
&item android:drawable=&@drawable/a_01& android:duration=&50&/&
&item android:drawable=&@drawable/a_02& android:duration=&50&/&
&item android:drawable=&@drawable/a_03& android:duration=&50&/&
&item android:drawable=&@drawable/a_04& android:duration=&50&/&
&item android:drawable=&@drawable/a_05& android:duration=&50&/&
&item android:drawable=&@drawable/a_06& android:duration=&50&/&
&/animation-list&
4、.java文件
importandroid.app.A
importandroid.graphics.drawable.AnimationD
importandroid.os.B
importandroid.view.V
importandroid.view.View.OnClickL
importandroid.widget.B
importandroid.widget.ImageV
public class AnimationsActivity extends Activity {
private Button button =
private ImageView imageView =
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button);
imageView = (ImageView)findViewById(R.id.image);
button.setOnClickListener(newButtonListener());
class ButtonListener implementsOnClickListener{
public void onClick(View v) {
imageView.setBackgroundResource(R.anim.anim);
AnimationDrawable animationDrawable = (AnimationDrawable)
imageView.getBackground();
animationDrawable.start();
LayoutAnimationsController
1、什么是LayoutAnimationsController
LayoutAnimationsController可以用于实现使多个控件按顺序一个一个的显示。
1)LayoutAnimationsController用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置统一的动画效果。
2)每一个控件都有相同的动画效果。
3)控件的动画效果可以在不同的时间显示出来。
4)LayoutAnimationsController可以在xml文件当中设置,以可以在代码当中进行设置。
2、在xml当中使用LayoutAnimationController
1)在res/anim文件夹下创建一个名为list_anim_layout.xml文件:
android:delay -&动画间隔时间;子类动画时间间隔 (延迟)&& 70% 也可以是一个浮点数 如“1.2”等
android:animationOrder -&动画执行的循序(normal:顺序,random:随机,reverse:反向显示)
android:animation –&引用动画效果文件
&layoutAnimation
xmlns:android=&/apk/res/android&
android:delay=&0.5&
android:animationOrder=&normal&
android:animation=&@anim/list_anim&/&
2)创建list_anim.xml文件,设置动画效果
&?xml version=&1.0& encoding=&utf-8&?&
&set xmlns:android=&/apk/res/android&
android:interpolator=&@android:anim/accelerate_interpolator&
android:shareInterpolator=&true&&
android:fromAlpha=&0.0&
android:toAlpha=&1.0&
android:duration=&1000&/&
3)在布局文件main.xml当中为ListVIew添加如下配置
android:id=&@id/android:list&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:scrollbars=&vertical&
android:layoutAnimation=&@anim/list_anim_layout&/&
4)程序结构
5)list_anim_layout.xml
&layoutAnimation
xmlns:android=&/apk/res/android&
android:delay=&0.5&
android:animationOrder=&normal&
android:animation=&@anim/list_anim&/&
6)list_anim.xml
&?xml version=&1.0& encoding=&utf-8&?&
&set xmlns:android=&/apk/res/android&
android:interpolator=&@android:anim/accelerate_interpolator&
android:shareInterpolator=&true&&
android:fromAlpha=&0.0&
android:toAlpha=&1.0&
android:duration=&1000&/&
7)main.xml
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:orientation=&vertical&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:id=&@id/android:list&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:scrollbars=&vertical&
android:layoutAnimation=&@anim/list_anim_layout&/&
android:id=&@+id/button&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:text=&测试&/&
&/LinearLayout&
8)item.xml
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:orientation=&horizontal&
android:paddingLeft=&10dip&
android:paddingRight=&10dip&
android:paddingTop=&1dip&
android:paddingBottom=&1dip&&
&TextView android:id=&@+id/name&
android:layout_width=&180dip&
android:layout_height=&30dip&
android:textSize=&5pt&
android:singleLine=&true& /&
&TextView android:id=&@+id/sex&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:textSize=&5pt&
android:singleLine=&true&/&
&/LinearLayout&
9)java文件
public class Animation2Activity extendsListActivity {
private Button button =
private ListView listView =
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = getListView();
button = (Button)findViewById(R.id.button);
button.setOnClickListener(newButtonListener());
private ListAdapter createListAdapter() {
List&HashMap&String,String&& list =
new ArrayList&HashMap&String,String&&();
HashMap&String,String& m1 = new HashMap&String,String&();
m1.put(&name&, &bauble&);
m1.put(&sex&, &male&);
HashMap&String,String& m2 = new HashMap&String,String&();
m2.put(&name&, &Allorry&);
m2.put(&sex&, &male&);
HashMap&String,String& m3 = new HashMap&String,String&();
m3.put(&name&, &Allotory&);
m3.put(&sex&, &male&);
HashMap&String,String& m4 = new HashMap&String,String&();
m4.put(&name&, &boolbe&);
m4.put(&sex&, &male&);
list.add(m1);
list.add(m2);
list.add(m3);
list.add(m4);
SimpleAdapter simpleAdapter = new SimpleAdapter(
this,list,R.layout.item,new String[]{&name&,&sex&},
new int[]{R.id.name,R.id.sex});
return simpleA
private class ButtonListener implementsOnClickListener{
public void onClick(View v) {
listView.setAdapter(createListAdapter());
备注:要将整个动画效果设置到LinerLayout中,可以这样设置:
&LinearLayoutxmlns:android=&/apk/res/android&
android:orientation=&vertical&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:layoutAnimation=&@anim/list_anim_layout&
3、在代码当中使用LayoutAnimationController
1)去掉main.xml中的android:layoutAnimation=&@anim/list_anim_layout&/&
2)创建一个Animation对象:可以通过装载xml文件,或者是直接使用Animation的构造方法创建Animation对象;
Animation animation = (Animation) AnimationUtils.loadAnimation(
Animation2Activity.this, R.anim.list_anim);
3)创建LayoutAnimationController对象:&
LayoutAnimationController controller = new LayoutAnimationController(animation);
4)设置控件的显示顺序以及延迟时间
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
controller.setDelay(0.5f);
5)为ListView设置LayoutAnimationController属性:
listView.setLayoutAnimation(controller);
完整代码:
private class ButtonListener implementsOnClickListener {
public void onClick(View v) {
listView.setAdapter(createListAdapter());
Animation animation = (Animation) AnimationUtils.loadAnimation(
Animation2Activity.this, R.anim.list_anim);
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
controller.setDelay(0.5f);
listView.setLayoutAnimation(controller);
AnimationListener
1、什么是AnimationListener
1).AnimationListener是一个监听器,该监听器在动画执行的各个阶段会得到通知,从而调用相应的方法;
2).AnimationListener主要包括如下三个方法:
n&&&&&&&&&·onAnimationEnd(Animation animation) -&当动画结束时调用
n&&&&&&&&&·onAnimationRepeat(Animation animation) -&当动画重复时调用
n&&&&&&&&&·onAniamtionStart(Animation animation) -&当动画启动时调用
2、具体实现
1)main.xml
&?xml version=&1.0& encoding=&utf-8&?&
&RelativeLayout xmlns:android=&/apk/res/android&
android:id=&@+id/layout&
android:orientation=&vertical&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&&
&Button android:id=&@+id/addButton&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:layout_alignParentBottom=&true&
android:text=&添加图片& /&
&Button android:id=&@+id/deleteButton&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:layout_above=&@id/addButton&
android:text=&删除图片& /&
&ImageView android:id=&@+id/image&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_centerInParent=&true&
android:layout_marginTop=&100dip&
android:src=&@drawable/an& /&
&/RelativeLayout&
2).java文件
public class Animation2Activity extends Activity {
private Button addButton =
private Button deleteButton =
private ImageView imageView =
private ViewGroup viewGroup =
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addButton = (Button)findViewById(R.id.addButton);
deleteButton = (Button)findViewById(R.id.deleteButton);
imageView = (ImageView)findViewById(R.id.image);
//LinearLayout下的一组控件
viewGroup = (ViewGroup)findViewById(R.id.layout);
addButton.setOnClickListener(newAddButtonListener());
deleteButton.setOnClickListener(newDeleteButtonListener());
private class AddButtonListener implements OnClickListener{
public void onClick(View v) {
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(1000);
animation.setStartOffset(500);
//创建一个新的ImageView
ImageView newImageView = new ImageView(
Animation2Activity.this);
newImageView.setImageResource(R.drawable.an);
viewGroup.addView(newImageView,
new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
newImageView.startAnimation(animation);
private class DeleteButtonListener implements OnClickListener{
public void onClick(View v) {
AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(1000);
animation.setStartOffset(500);
//为Aniamtion对象设置监听器
animation.setAnimationListener(
new RemoveAnimationListener());
imageView.startAnimation(animation);
private class RemoveAnimationListener implements AnimationListener{
//动画效果执行完时remove
public void onAnimationEnd(Animation animation) {
System.out.println(&onAnimationEnd&);
viewGroup.removeView(imageView);
public void onAnimationRepeat(Animation animation) {
System.out.println(&onAnimationRepeat&);
public void onAnimationStart(Animation animation) {
System.out.println(&onAnimationStart&);
3、总结一下
可以在Activity中动态添加和删除控件,方法是:
1)取到那个Layout
viewGroup = (ViewGroup)findViewById(R.id.layout);
2)添加时,先创建对象,然后添加
ImageView newImageView = new ImageView(
Animation2Activity.this);
newImageView.setImageResource(R.drawable.an);
viewGroup.addView(newImageView,
new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
3)删除时,直接删除。
viewGroup.removeView(imageView);
本文已收录于以下专栏:
相关文章推荐
前言:这几天做客户回访,感触很大,用户只要是留反馈信息,总是一种恨铁不成钢的心态,想用你的app,却是因为你的技术问题,让他们不得不放弃,而你一个回访电话却让他们尽释前嫌,当最后把手机号留给他们以便随...
在Android应用程序,使用动画效果,能带给用户更好的感觉,做动画可以通过XML或Android代码来实现。
Animation动画效果的实现可以通过两种方式进行实现,一种是tweened anim...
在视图范围内展示波纹效果
android:background=&?android:attr/selectableItemBackground&波纹在接触点开始,之后填充整个视图背景
在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系统在一开始的时候就给我们提供了两种实现动画效果的方式,逐帧动画(frame-by-frame animation)和补间动画(twe...
春节假期刚到,就赶紧抽出点时间写点技术文章,这篇文章已经酝酿了很长时间了。FastDFS是用 C 语言编写的一款开源的高性能分布式文件系统,主要功能包括文件存储、文件同步和文件访问,以及高容量和负载平...
一、简介:vectordrawable允许你基于xml创建一个矢量图形。在API21时第一次发布。
二、优点:1、体积小;2、一个图片适配所有屏幕;3、可以实现非常优美的动画效果;4、svg格式的图片...
大家好,在上一篇文章当中,我们学习了Android属性动画的基本用法,当然也是最常用的一些用法,这些用法足以覆盖我们平时大多情况下的动画需求了。但是,正如上篇文章当中所说到的,属性动画对补间动画进行了...
官方地址:/guide/topics/graphics/prop-animation.html
  注:高级动画要在3.0以上版本才能使用
简介:贝塞尔曲线是计算机图形学中相当重要的参数曲线。可以用数学公式来描述一段曲线。
用途:1、贝塞尔曲线可以帮助我们在二维平面内用平滑的曲线画出各种图形。
2、同时也可以给动画提供一个平滑的曲线运动路...
Android提供了2中动画
1.Tween动画,通过对View的内容进行一系列的图形变换(包括平移,缩放,旋转,改变透明度)来实现动画的效果,动画效果的定义可以采用XML方式也可以采用编码来做Tw...
他的最新文章
讲师:吴岸城
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)

我要回帖

更多关于 安卓开机动画卡刷包 的文章

 

随机推荐