调整直播间聊天室动画效果

This commit is contained in:
zlzw 2022-12-24 11:08:09 +08:00
parent a069562bbe
commit c4cfa0becd
4 changed files with 119 additions and 16 deletions

View File

@ -383,6 +383,7 @@ public class LiveChatAdapter extends RecyclerView.Adapter {
if (bean == null) {
return;
}
// Log.i(TAG, "insertItem: " + bean.getContent());
int size = mList.size();
//设置最大展示99条消息
if (size == 100) {
@ -390,7 +391,7 @@ public class LiveChatAdapter extends RecyclerView.Adapter {
notifyItemRangeRemoved(0, 50);
}
mList.add(bean);
if (getItemCount() == 1 ) {
if (getItemCount() == 1) {
notifyDataSetChanged();
} else {
notifyItemInserted(getItemCount());

View File

@ -0,0 +1,40 @@
package com.yunbao.live.adapter;
import android.content.Context;
import android.graphics.PointF;
import android.util.DisplayMetrics;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;
public class LiveChatRecyclerViewLayoutManager extends LinearLayoutManager {
private static final float MILLISECONDS_PER_INCH = 200f;
public LiveChatRecyclerViewLayoutManager(Context context) {
super(context);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
// super.smoothScrollToPosition(recyclerView, state, position);
LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext())
{
@Nullable
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return LiveChatRecyclerViewLayoutManager.this.computeScrollVectorForPosition(targetPosition);
}
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
// System.out.println("----->自定义滑动 = "+MILLISECONDS_PER_INCH+" > "+( MILLISECONDS_PER_INCH / displayMetrics.densityDpi));
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
}

View File

@ -16,7 +16,6 @@ import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
@ -37,6 +36,7 @@ import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
@ -116,6 +116,7 @@ import com.yunbao.live.activity.LiveAudienceActivity;
import com.yunbao.live.activity.LiveRyAnchorActivity;
import com.yunbao.live.activity.ZhuangBanActivity;
import com.yunbao.live.adapter.LiveChatAdapter;
import com.yunbao.live.adapter.LiveChatRecyclerViewLayoutManager;
import com.yunbao.live.adapter.LiveRoomFastMessageRecyclerViewAdapter;
import com.yunbao.live.adapter.LiveUserAdapter;
import com.yunbao.live.bean.DrPkbean;
@ -158,8 +159,6 @@ import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
@ -868,11 +867,54 @@ public class LiveRoomViewHolder extends AbsViewHolder implements View.OnClickLis
mChatRecyclerView.setLayoutParams(params1);
mChatRecyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
LinearLayoutManager layoutManager = new LiveChatRecyclerViewLayoutManager(mContext);
layoutManager.setStackFromEnd(true);
mChatRecyclerView.setLayoutManager(layoutManager);
mChatRecyclerView.addItemDecoration(new TopGradual());
mChatRecyclerView.setItemViewCacheSize(10);
mChatRecyclerView.setItemAnimator(new SimpleItemAnimator() {
@Override
public boolean animateRemove(RecyclerView.ViewHolder holder) {
return false;
}
@Override
public boolean animateAdd(RecyclerView.ViewHolder holder) {
Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.live_chat_msg_in);
holder.itemView.startAnimation(animation);
return false;
}
@Override
public boolean animateMove(RecyclerView.ViewHolder holder, int fromX, int fromY, int toX, int toY) {
return false;
}
@Override
public boolean animateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromLeft, int fromTop, int toLeft, int toTop) {
return false;
}
@Override
public void runPendingAnimations() {
}
@Override
public void endAnimation(@NonNull RecyclerView.ViewHolder item) {
}
@Override
public void endAnimations() {
}
@Override
public boolean isRunning() {
return false;
}
});
mLiveChatAdapter = new LiveChatAdapter(mContext);
mChatRecyclerView.setAdapter(mLiveChatAdapter);
mLiveChatAdapter.setOnItemClickListener(new OnItemClickListener<LiveChatBean>() {
@ -928,35 +970,39 @@ public class LiveRoomViewHolder extends AbsViewHolder implements View.OnClickLis
mViewMedal.setOnClickListener(this);
findViewById(R.id.btn_close).setOnClickListener(this);
mAvatar.setOnClickListener(this);
//点击侧边消息按钮自动发消息测试用的
/*msg.setOnClickListener(new View.OnClickListener() {
long index=0;
/*//点击侧边消息按钮自动发消息测试用的
msg.setOnClickListener(new View.OnClickListener() {
long index = 0;
public TimerTask createTask() {
return new TimerTask() {
@Override
public void run() {
LiveChatBean bean = new LiveChatBean();
bean.setContent("test msg = "+index++);
bean.setContent("test msg = " + index++);
bean.setUserNiceName("auto");
handler.post(()->mLiveChatAdapter.insertItem(bean));
handler.post(() -> mLiveChatAdapter.insertItem(bean));
}
};
}
Handler handler = new Handler(Looper.getMainLooper());
private TimerTask task;
@Override
public void onClick(View v) {
if(task==null){
task=createTask();
new Timer().schedule(task, 0, 300);
}else{
if (task == null) {
task = createTask();
new Timer().schedule(task, 0, 1000);
} else {
task.cancel();
task=null;
task = null;
}
}
});
*/
userMore.setOnClickListener(v -> {
mLiveChatAdapter.scrollToBottom();
});*/
findViewById(R.id.btn_votes).setOnClickListener(this);
findViewById(R.id.btn_medal_rank).setOnClickListener(this);
findViewById(R.id.btn_noble).setOnClickListener(this);

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="500"
android:fromAlpha="0.1"
android:toAlpha="1"
android:fromYDelta="2.5%p"
android:toYDelta="0" />
<alpha
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="500"
android:fromAlpha="0.1"
android:toAlpha="1"
/>
</set>