直播间加入观众动画

该博客主要展示了如何在Android应用中,当用户点击按钮时,动态加载一个布局并使用动画使其从屏幕右侧滑入,然后在动画结束时从左侧滑出。通过调整不同的等级条件,改变显示的图片资源,实现了不同等级的视觉效果。文章还提及了使用属性动画来实现这一过程,并邀请读者提供改进意见。

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

在这里插入图片描述
上面是效果图

1,MainAcitivty

public class MainActivity extends AppCompatActivity {

    private RelativeLayout rrrrr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rrrrr = this.findViewById(R.id.rrrrr);
        TextView tv_one = this.findViewById(R.id.tv_one);


        tv_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                initAnim(rrrrr, (int)(Math.random() * 50)+1 , "我来看你直播了");
            }
        });
    }

    private void initAnim(RelativeLayout view, int grade, String name) {
        View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_add_live, null);
        LinearLayout tv_ll_text = inflate.findViewById(R.id.tv_ll_text);
        ImageView iv_add_live_giftTotal = inflate.findViewById(R.id.iv_add_live_giftTotal);
        TextView tv_add_live_grade = inflate.findViewById(R.id.tv_add_live_grade);
        TextView tv_add_live_name = inflate.findViewById(R.id.tv_add_live_name);

        if (grade == 50) {
            iv_add_live_giftTotal.setImageResource(R.drawable.icon_my_class_six);
        } else if (grade <= 49 && grade >= 40) {
            iv_add_live_giftTotal.setImageResource(R.drawable.icon_my_class_five);
        } else if (grade <= 39 && grade >= 30) {
            iv_add_live_giftTotal.setImageResource(R.drawable.icon_my_class_fore);
        } else if (grade <= 29 && grade >= 20) {
            iv_add_live_giftTotal.setImageResource(R.drawable.icon_my_class_three);
        } else if (grade <= 19 && grade >= 10) {
            iv_add_live_giftTotal.setImageResource(R.drawable.icon_my_class_two);
        } else {
            iv_add_live_giftTotal.setImageResource(R.drawable.icon_my_class_one);
        }

        tv_add_live_grade.setText(grade + "");
        tv_add_live_name.setText(name + "  加入直播间");

        view.addView(inflate);

        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        tv_ll_text.measure(w, h);
        int width = tv_ll_text.getMeasuredWidth();
        int height = tv_ll_text.getMeasuredHeight();

        TranslateAnimation animation = new TranslateAnimation(width, -width / 3, 0, 0);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                TranslateAnimation anim = new TranslateAnimation(-width / 3, -width * 3, 0, 0);
                anim.setDuration(1000);
                tv_ll_text.startAnimation(anim);
                anim.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        rrrrr.removeView(inflate);

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator();
        animation.setDuration(3000);
        animation.setInterpolator(decelerateInterpolator);
        tv_ll_text.startAnimation(animation);
    }

}

MainActicity的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="500dp">


        <RelativeLayout
            android:id="@+id/rrrrr"
            android:gravity="center|right"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </RelativeLayout>

    </LinearLayout>


    <TextView
        android:id="@+id/tv_one"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="加入直播间"
        android:textColor="#000000"
        android:gravity="center"/>

</LinearLayout>

动态添加布局的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tv_ll_text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/shape_bg"
    android:orientation="horizontal"
    android:padding="4dp"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <ImageView
            android:id="@+id/iv_add_live_giftTotal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:src="@drawable/icon_my_class_six" />

        <TextView
            android:id="@+id/tv_add_live_grade"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@id/iv_add_live_giftTotal"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:text="33"
            android:textColor="@color/white"
            android:textSize="10sp"
            android:textStyle="italic"
            tools:text="44" />
    </RelativeLayout>


    <TextView
        android:id="@+id/tv_add_live_name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:maxLines="1"
        android:layout_marginRight="5dp"
        android:text="回复笔试部分我   加入直播间"
        android:textColor="#000000" />

</LinearLayout>

用属性动画简单的实现了一下,有大佬可以给完善一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值