码迷,mamicode.com
首页 > 移动开发 > 详细

Android补间动画笔记

时间:2016-12-05 23:07:44      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:ogg   eve   repeat   inf   静态   over   start   使用   ica   

布局文件:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_my_anim"
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="com.jogger.propertiesanim.MyAnim">

<ImageView
android:id="@+id/iv_clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/t_clock"/>

<ImageView
android:id="@+id/iv_bettery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/t_bettery"/>

<ImageView
android:id="@+id/iv_low_bettery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/iv_clock"
android:layout_centerHorizontal="true"
android:src="@drawable/t_low_bettery"/>
<ImageView
android:id="@+id/iv_next"
android:layout_centerHorizontal="true"
android:src="@drawable/t_next"
android:layout_below="@id/iv_clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/iv_msg"
android:layout_centerHorizontal="true"
android:src="@drawable/t_first_msg"
android:layout_below="@id/iv_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</RelativeLayout>
方法一:动态注册
public class MyAnim extends AppCompatActivity {
private ImageView iv_low_bettery, iv_next, iv_bettery;

/**
* 补间动画案例(缩放,平移,旋转)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_anim);
iv_low_bettery = (ImageView) findViewById(R.id.iv_low_bettery);
iv_next = (ImageView) findViewById(R.id.iv_next);
iv_bettery = (ImageView) findViewById(R.id.iv_bettery);
//缩放动画
/**
* fromX:开始缩放的X轴倍数。如1.0f:本身大小;如2.0f:从自己两倍开始
toX:结束缩放的X轴倍数,体现在结束时的宽度上
fromY:始缩放的Y轴倍数。
toY:结束缩放的Y轴倍数。
pivotXType:X轴缩放中心点类型;可选值有:
Animation.RELATIVE_TO_SELF相对自己--常用
Animation.RELATIVE_TO_PARENT相对父窗体
Animation.ABSOLUTE 绝对的---不常用

pivotXValue:在pivotXType的基础上,X轴缩放中心的位置。如:0.5f:缩放中心就在控件的一半的位置。如果是0.0f,则会在控件本身的左边位置
pivotYType:X轴缩放中心点类型;同上 ...
pivotYValue:在pivotYType的基础上,Y轴缩放中心的位置。
*/
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.5f, 1.0f, 1.5f, Animation
.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//平移动画
/**
* fromXType:开始平移的X轴参照位置,一般使用Animation.RELATIVE_TO_SELF
fromXValue:X轴平移的开始倍数。在fromXType的基础上。
toXType:结束平移的X轴参照位置
toXValue:X轴平移的结束倍数。
fromYType:开始平移的Y轴参照位置
fromYValue:Y轴平移的开始倍数。
toYType:结束平移的Y轴参照位置
toYValue:Y轴平移的结束倍数。
*/
TranslateAnimation translateAnimation = new TranslateAnimation(Animation
.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
0, Animation.RELATIVE_TO_SELF, 1f);
scaleAnimation.setRepeatCount(Animation.INFINITE);//设置重复次数,INFINITE为设置无限重复
translateAnimation.setRepeatCount(Animation.INFINITE);
AnimationSet set = new AnimationSet(false);
set.addAnimation(scaleAnimation);
set.addAnimation(translateAnimation);
set.setDuration(2000);//播放一轮动画的时间
set.setRepeatMode(Animation.REVERSE);//用于设置动画的重复方式,可选择为reverse(反向)和restart(重新开始)
//set.setRepeatCount(Animation.INFINITE);//set不能直接设置无限重复
iv_low_bettery.startAnimation(set);
TranslateAnimation translateAnimationNext = new TranslateAnimation(Animation
.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
0, Animation.RELATIVE_TO_SELF, 1.1f);
translateAnimationNext.setDuration(2000);
translateAnimationNext.setRepeatCount(Animation.INFINITE);
translateAnimationNext.setRepeatMode(Animation.REVERSE);
iv_next.startAnimation(translateAnimationNext);

/**
* fromDegrees:开始旋转的角度;三点方向为0度,六点方向为90度。
toDegrees:结束旋转的角度
pivotXType:参照缩放动画
pivotXValue:参照缩放动画
pivotYType:参照缩放动画
pivotYValue:参照缩放动画
*/
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setRepeatMode(Animation.REVERSE);
rotateAnimation.setRepeatCount(Animation.INFINITE);
rotateAnimation.setDuration(2000);
iv_bettery.startAnimation(rotateAnimation);
}
}


方法二:静态注册
在res目录下创建anim文件夹,创建如下3个资源文件:
low_bettery_anim.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:repeatMode="reverse"
>
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:repeatCount="infinite"
android:toXDelta="0"
android:toYDelta="100%"></translate>
<scale
android:fromXScale="100%"
android:fromYScale="100%"
android:pivotX="50%"
android:pivotY="50"
android:repeatCount="infinite"
android:toXScale="150%"
android:toYScale="150%"></scale>
</set>

next_anim.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:repeatMode="reverse">

<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:repeatCount="infinite"
android:toXDelta="0"
android:toYDelta="100%"></translate>
</set>
bettery_anim.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:repeatMode="reverse">
<!--pivotX 50%表示相对自身-->
<rotate
android:fromDegrees="0.5"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="360"></rotate>

</set>
代码界面:
public class MyAnim extends AppCompatActivity {
private ImageView iv_low_bettery, iv_next, iv_bettery;

/**
* 补间动画案例(缩放,平移,旋转)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_anim);
iv_low_bettery = (ImageView) findViewById(R.id.iv_low_bettery);
iv_next = (ImageView) findViewById(R.id.iv_next);
iv_bettery = (ImageView) findViewById(R.id.iv_bettery);
Animation low_bettery_anim = AnimationUtils.loadAnimation(this, R.anim.low_bettery_anim);
Animation next_anim = AnimationUtils.loadAnimation(this, R.anim.next_anim);
Animation bettery_anim = AnimationUtils.loadAnimation(this, R.anim.bettery_anim);
iv_next.startAnimation(next_anim);
iv_low_bettery.startAnimation(low_bettery_anim);
iv_bettery.startAnimation(bettery_anim);
}
}

效果图:
技术分享

Android补间动画笔记

标签:ogg   eve   repeat   inf   静态   over   start   使用   ica   

原文地址:http://www.cnblogs.com/epilogue/p/6135593.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!