上面是效果图
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>
用属性动画简单的实现了一下,有大佬可以给完善一下