Android中动画的介绍(二)——补间动画(2)

本文介绍如何使用Java代码实现Android中的补间动画效果,包括透明度、缩放、位移和旋转等,并展示了如何组合多种动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇博客当中,为大家提到了补间动画的特点和可以实现的效果,我们通过xml文件当中编写动画行为,可以对于view控件进行设置,然后我们也提到了,补间动画除了可以在xml布局当中编写,同样可以在java代码当中设计。那么在这篇博客当中,一起看看如何在java代码当中描述补间动画的各个行为。

直接写代码:

1.编写布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btn_alpha"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:text="透明度"/>
        <Button
            android:id="@+id/btn_scale"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:text="缩放"/>
        <Button
            android:id="@+id/btn_translate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:text="位移"/>
        <Button
            android:id="@+id/btn_rotate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:text="旋转"/>
        <Button
            android:id="@+id/btn_set"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:text="集合"/>
    </LinearLayout>
    <ImageView
        android:id="@+id/tween_iv"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@mipmap/image_info11"
        android:layout_centerInParent="true"/>
</RelativeLayout>


2.在activity的代码当中,针对于需求,进行代码的编写,主要完成补间动画在代码当中的设置。

public class TweenActivity extends AppCompatActivity {
    private ImageView tweenIv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tween);
        tweenIv = (ImageView) findViewById(R.id.tween_iv);
    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_alpha:
//              实例化透明度动画的对象
                AlphaAnimation alphaAnimation = new AlphaAnimation(0.2f, 1.0f);
                alphaAnimation.setDuration(3000);
                alphaAnimation.setFillAfter(true);  //设置保存改变后的状态
                alphaAnimation.setRepeatCount(1);  //设置重复的次数
                alphaAnimation.setRepeatMode(Animation.RESTART);  //设置重复的模式
                tweenIv.startAnimation(alphaAnimation);
                break;
            case R.id.btn_scale:
//              实例化放缩动画的对象
// float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue
                ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 2f, 1f, 2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                scaleAnimation.setDuration(3000);
                scaleAnimation.setFillAfter(true);
                scaleAnimation.setRepeatCount(1);
                scaleAnimation.setRepeatMode(Animation.REVERSE);
                tweenIv.startAnimation(scaleAnimation);
                break;
            case R.id.btn_translate:
                TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 200);
                translateAnimation.setDuration(3000);
                translateAnimation.setFillAfter(true);
                tweenIv.startAnimation(translateAnimation);  //开启动画
                break;
            case R.id.btn_rotate:  //旋转动画
                RotateAnimation rotateAnimation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                rotateAnimation.setDuration(3000);
                rotateAnimation.setRepeatCount(1);
                rotateAnimation.setRepeatMode(Animation.REVERSE);
                tweenIv.startAnimation(rotateAnimation);
                break;
            case R.id.btn_set:

                AnimationSet set = new AnimationSet(true);
                AlphaAnimation alphaAnime = new AlphaAnimation(0.2f, 1.0f);
                RotateAnimation rotateAnime = new RotateAnimation(0, 900, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//                因为补间动画集合指的是同时可以播放两种以上的动画,所以要把动画添加到集合中
                set.addAnimation(alphaAnime);
                set.addAnimation(rotateAnime);
                set.setDuration(3000);
                set.setFillAfter(true);
                tweenIv.startAnimation(set);
                break;
        }
    }
}


然后我们运行代码,就会出现想要的效果了,效果如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值