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

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) { if (bean == null) {
return; return;
} }
// Log.i(TAG, "insertItem: " + bean.getContent());
int size = mList.size(); int size = mList.size();
//设置最大展示99条消息 //设置最大展示99条消息
if (size == 100) { if (size == 100) {
@ -390,7 +391,7 @@ public class LiveChatAdapter extends RecyclerView.Adapter {
notifyItemRangeRemoved(0, 50); notifyItemRangeRemoved(0, 50);
} }
mList.add(bean); mList.add(bean);
if (getItemCount() == 1 ) { if (getItemCount() == 1) {
notifyDataSetChanged(); notifyDataSetChanged();
} else { } else {
notifyItemInserted(getItemCount()); 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.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewOutlineProvider; import android.view.ViewOutlineProvider;
@ -37,6 +36,7 @@ import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; 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.LiveRyAnchorActivity;
import com.yunbao.live.activity.ZhuangBanActivity; import com.yunbao.live.activity.ZhuangBanActivity;
import com.yunbao.live.adapter.LiveChatAdapter; import com.yunbao.live.adapter.LiveChatAdapter;
import com.yunbao.live.adapter.LiveChatRecyclerViewLayoutManager;
import com.yunbao.live.adapter.LiveRoomFastMessageRecyclerViewAdapter; import com.yunbao.live.adapter.LiveRoomFastMessageRecyclerViewAdapter;
import com.yunbao.live.adapter.LiveUserAdapter; import com.yunbao.live.adapter.LiveUserAdapter;
import com.yunbao.live.bean.DrPkbean; import com.yunbao.live.bean.DrPkbean;
@ -158,8 +159,6 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
@ -868,11 +867,54 @@ public class LiveRoomViewHolder extends AbsViewHolder implements View.OnClickLis
mChatRecyclerView.setLayoutParams(params1); mChatRecyclerView.setLayoutParams(params1);
mChatRecyclerView.setHasFixedSize(true); mChatRecyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(mContext); LinearLayoutManager layoutManager = new LiveChatRecyclerViewLayoutManager(mContext);
layoutManager.setStackFromEnd(true); layoutManager.setStackFromEnd(true);
mChatRecyclerView.setLayoutManager(layoutManager); mChatRecyclerView.setLayoutManager(layoutManager);
mChatRecyclerView.addItemDecoration(new TopGradual()); mChatRecyclerView.addItemDecoration(new TopGradual());
mChatRecyclerView.setItemViewCacheSize(10); 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); mLiveChatAdapter = new LiveChatAdapter(mContext);
mChatRecyclerView.setAdapter(mLiveChatAdapter); mChatRecyclerView.setAdapter(mLiveChatAdapter);
mLiveChatAdapter.setOnItemClickListener(new OnItemClickListener<LiveChatBean>() { mLiveChatAdapter.setOnItemClickListener(new OnItemClickListener<LiveChatBean>() {
@ -928,35 +970,39 @@ public class LiveRoomViewHolder extends AbsViewHolder implements View.OnClickLis
mViewMedal.setOnClickListener(this); mViewMedal.setOnClickListener(this);
findViewById(R.id.btn_close).setOnClickListener(this); findViewById(R.id.btn_close).setOnClickListener(this);
mAvatar.setOnClickListener(this); mAvatar.setOnClickListener(this);
//点击侧边消息按钮自动发消息测试用的 /*//点击侧边消息按钮自动发消息测试用的
/*msg.setOnClickListener(new View.OnClickListener() { msg.setOnClickListener(new View.OnClickListener() {
long index=0; long index = 0;
public TimerTask createTask() { public TimerTask createTask() {
return new TimerTask() { return new TimerTask() {
@Override @Override
public void run() { public void run() {
LiveChatBean bean = new LiveChatBean(); LiveChatBean bean = new LiveChatBean();
bean.setContent("test msg = "+index++); bean.setContent("test msg = " + index++);
bean.setUserNiceName("auto"); bean.setUserNiceName("auto");
handler.post(()->mLiveChatAdapter.insertItem(bean)); handler.post(() -> mLiveChatAdapter.insertItem(bean));
} }
}; };
} }
Handler handler = new Handler(Looper.getMainLooper()); Handler handler = new Handler(Looper.getMainLooper());
private TimerTask task; private TimerTask task;
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if(task==null){ if (task == null) {
task=createTask(); task = createTask();
new Timer().schedule(task, 0, 300); new Timer().schedule(task, 0, 1000);
}else{ } else {
task.cancel(); task.cancel();
task=null; task = null;
} }
} }
}); });
*/ userMore.setOnClickListener(v -> {
mLiveChatAdapter.scrollToBottom();
});*/
findViewById(R.id.btn_votes).setOnClickListener(this); findViewById(R.id.btn_votes).setOnClickListener(this);
findViewById(R.id.btn_medal_rank).setOnClickListener(this); findViewById(R.id.btn_medal_rank).setOnClickListener(this);
findViewById(R.id.btn_noble).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>