补间动画
透明度,旋转,位移,缩放,集合(前面的整合)
布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="50dp"
android:src="@drawable/houzi" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn1"
android:text="透明度" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn2"
android:text="缩放" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn3"
android:text="旋转" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn4"
android:text="平移" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn5"
android:text="集合" />
</LinearLayout>
Java代码
public class MainActivity extends Activity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
// 找到需要播放的图片资源
}
// 透明度
public void btn1(View view) {
// 0.0f-1.0f
AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 0.88f);
// 持续的时间
alphaAnimation.setDuration(2000);
// 动画进行的次数
alphaAnimation.setRepeatCount(1);
// 重复的模式
// 来回 重启
alphaAnimation.setRepeatMode(AlphaAnimation.RESTART);
// 动画结束后保持在结束的状态
alphaAnimation.setFillAfter(true);
// 开启动画
imageView.startAnimation(alphaAnimation);
}
// 缩放
public void btn2(View view) {
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f,
0.5f);
// 持续的时间
scaleAnimation.setDuration(4000);
// 动画进行的次数
scaleAnimation.setRepeatCount(2);
// 重复的模式
// 来回 重启
scaleAnimation.setRepeatMode(AlphaAnimation.RESTART);
// 动画结束后保持在结束的状态
scaleAnimation.setFillAfter(true);
// 开启动画
imageView.startAnimation(scaleAnimation);
}
// 旋转
public void btn3(View view) {
RotateAnimation rotateAnimation = new RotateAnimation(0, 135);
// 持续的时间
rotateAnimation.setDuration(4000);
// 动画进行的次数
rotateAnimation.setRepeatCount(2);
// 重复的模式
// 来回 重启
rotateAnimation.setRepeatMode(AlphaAnimation.RESTART);
// 动画结束后保持在结束的状态
rotateAnimation.setFillAfter(true);
// 开启动画
imageView.startAnimation(rotateAnimation);
}
// 平移
public void btn4(View view) {
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
2.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -2.0f);
// 持续的时间
translateAnimation.setDuration(4000);
// 动画进行的次数
translateAnimation.setRepeatCount(2);
// 重复的模式
// 来回 重启
translateAnimation.setRepeatMode(AlphaAnimation.RESTART);
// 动画结束后保持在结束的状态
translateAnimation.setFillAfter(true);
// 开启动画
imageView.startAnimation(translateAnimation);
}
// 集合
public void btn5(View view) {
AnimationSet animationSet = new AnimationSet(true);
// 透明度
AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 0.88f);
// 平移
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
2.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -2.0f);
// 缩放
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f,
0.5f);
// 旋转
RotateAnimation rotateAnimation = new RotateAnimation(0, 135);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(translateAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(rotateAnimation);
// 持续的时间
translateAnimation.setDuration(3000);
// 动画进行的次数
translateAnimation.setRepeatCount(2);
// 重复的模式
// 来回 重启
translateAnimation.setRepeatMode(AlphaAnimation.RESTART);
// 动画结束后保持在结束的状态
translateAnimation.setFillAfter(false);
// 开启动画
imageView.startAnimation(animationSet);
}
}
使用xml方法操作补间动画之透明度示例
其余可参照该示例操作
在res下新建一个文件夹anim,然后新建xml文件,选择alpha,命名xml为:alpha_anim .xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fillAfter="true"
android:fromAlpha="0.1"
android:repeatCount="2"
android:repeatMode="reverse"
android:toAlpha="0.88" >
</alpha>
布局文件下再添加一个按钮后,在下面再添加一个按钮响应方法:
Java代码:
// XML实现动画的透明度
public void btn6(View view) {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this,
R.anim.alpha_anim);
// 开启
imageView.startAnimation(animation);
}