效果图:
补间动画的实现步骤:
1、在res文件夹下创建anim文件夹,在此文件夹下创建对于的补间动画;
2、在java代码中创建
animation = AnimationUtils.loadAnimation(TweenActivity.this, R.anim.tween_alpha);
3、启动imageView.startAnimation(animation);
activity的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="OnAlpha" android:textColor="#ebf705" android:layout_weight="1" android:text="透明度渐变AlphaAnimation" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="OnScale" android:layout_weight="1" android:textColor="#f50202" android:text="缩放动画ScaleAnimation" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="OnTranslate" android:layout_weight="1" android:textColor="#cf7878" android:text="位移动画TranslateAnimation" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="#2b62d1" android:onClick="OnRotate" android:text="旋转动画RotateAnimation" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="OnSet" android:textColor="#c212ed" android:layout_weight="1" android:text="组合动画SetAnimation" /> </LinearLayout> <ImageView android:id="@+id/tween_img" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/a" /> </LinearLayout>
activity的实现:
public class TweenActivity extends AppCompatActivity { private ImageView img; private Animation animation; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tween); img=findViewById(R.id.tween_img); } /** * 透明度渐变 * @param view */ public void OnAlpha(View view){ animation= AnimationUtils.loadAnimation(TweenActivity.this,R.anim.tween_alpha); img.startAnimation(animation); } /** * 缩放渐变 * @param view */ public void OnScale(View view){ animation= AnimationUtils.loadAnimation(TweenActivity.this,R.anim.tween_scale); img.startAnimation(animation); } /** * 位移动画 * @param view */ public void OnTranslate(View view){ animation= AnimationUtils.loadAnimation(TweenActivity.this,R.anim.tween_translate); img.startAnimation(animation); } /** * 旋转动画 * @param view */ public void OnRotate(View view){ animation= AnimationUtils.loadAnimation(TweenActivity.this,R.anim.tween_rotate); img.startAnimation(animation); } /** * 组合动画 * @param view */ public void OnSet(View view){ animation= AnimationUtils.loadAnimation(TweenActivity.this,R.anim.tween_set); img.startAnimation(animation); } }
就这样了,。用到的图片与anim,我会打包提供下载的,。
源码地址下载:https://download.csdn.net/download/weixin_42267745/10464843