1. 程式人生 > 其它 >線上直播系統原始碼,滾動式內容展示控制元件

線上直播系統原始碼,滾動式內容展示控制元件

線上直播系統原始碼,滾動式內容展示控制元件實現的相關程式碼

原始碼:

public class RollView extends FrameLayout {
private static final int DEFAULTTEXTSIZE = 16;
private static final int DEFAULTTEXTHEIGHT = 72;
private List<?> contents = new LinkedList<>();
private float textSize;
private int textColor = Color.BLACK;
//當前顯示內容在集合中的下標
private int curIndex = -1;
//下一個顯示的下標
private TextView curText;
private TextView nextText;
private int hSize;
private int wSize;
private TranslateAnimation outAnim;
private TranslateAnimation inAnim;
//是否執行動畫中
private boolean isAnimationing = false;
private onItemClickListener listener;
private MyHandler handler = new MyHandler(this);
public RollView(Context context) {
this(context, null);
}
public RollView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public RollView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_rollview_layout, this);
curText = ((TextView) view.findViewById(R.id.curText));
nextText = ((TextView) view.findViewById(R.id.nextText));
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RollView);
float tmpTextSize = ta.getInteger(R.styleable.RollView_rollSize, DEFAULTTEXTSIZE);
textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, tmpTextSize, getResources().getDisplayMetrics());
int colorRes = ta.getResourceId(R.styleable.RollView_rollColor, -1);
int colorRes2 = ta.getColor(R.styleable.RollView_rollColor, -1);
curText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//如果是執行動畫中,就不響應點選事件
if (isAnimationing) {
return;
} else {
if (listener != null) {
listener.onClick(curIndex);
}
}
}
});
if (colorRes != -1) {
textColor = colorRes;
} else if (colorRes2 != -1) {
textColor = colorRes2;
}
curText.setTextSize(textSize);
curText.setTextColor(textColor);
nextText.setTextSize(textSize);
nextText.setTextColor(textColor);
ta.recycle();
}
public void setListener(onItemClickListener listener) {
this.listener = listener;
}
/**
* 可以是自定義型別,只需要重新toString() 方法
* @param contents
*/
public void setObjContents(List<?> contents) {
if (contents == null || contents.size() == 0) {
return;
}
this.contents.clear();
List<String> list = new LinkedList<>();
for (Object content : contents) {
list.add(content.toString());
}
setContents(list);
}
public void setContents(List<String> contents) {
if (contents == null || contents.size() == 0) {
return;
}
this.contents = contents;
initData();
}
private void initData() {
curIndex = 0;
if (contents.size() > 0) {
curText.setText(contents.get(0).toString());
}
if (contents.size() > 1) {
nextText.setText(contents.get(1).toString());
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int wMode = MeasureSpec.getMode(widthMeasureSpec);
int hMode = MeasureSpec.getMode(heightMeasureSpec);
hSize = MeasureSpec.getSize(heightMeasureSpec);
wSize = MeasureSpec.getSize(widthMeasureSpec);
switch (hMode) {
case MeasureSpec.EXACTLY:
break;
case MeasureSpec.AT_MOST:
case MeasureSpec.UNSPECIFIED:
hSize = DEFAULTTEXTHEIGHT;
break;
}
setMeasuredDimension(wSize, hSize);
outAnim = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -hSize);
//動畫結束時,將控制元件還原到初始化狀態,必須設定這個
outAnim.setFillEnabled(true);
outAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
isAnimationing = true;
}
@Override
public void onAnimationEnd(Animation animation) {
isAnimationing = false;
curIndex++;
curIndex = curIndex % contents.size();//滾動到最後一條資料了,下一條顯示第一條資料
int nextIndex = (curIndex + 1) % contents.size();//獲取nextText 需要顯示的下一條資料的下標
/**
* 這裡的含義是:動畫結束後,控制元件還原到初始化狀態,
* curText 就需要顯示滾動後的 nextText 的內容,起到介面沒有視覺差
* nextText 就需要顯示,下下條資料,為了下次滾動做準備
*/
curText.setText(contents.get(curIndex).toString());
nextText.setText(contents.get(nextIndex).toString());
//動畫結束後,延遲3秒繼續滾動
handler.sendEmptyMessageDelayed(0, 2000);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
outAnim.setDuration(1000);
inAnim = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, hSize, Animation.ABSOLUTE, 0);
inAnim.setFillEnabled(true);
inAnim.setDuration(1000);
}
/**
* 開始滾動,數量必須大於1才會滾動,每3秒滾動一次
*/
public void startRoll() {
if (contents.isEmpty() || contents.size() == 1) {
return;
}
handler.sendEmptyMessageDelayed(0, 2000);
}
private void roll() {
if (isAnimationing) {
return;
}
curText.startAnimation(outAnim);
nextText.startAnimation(inAnim);
}
private static class MyHandler extends Handler {
private WeakReference<RollView> weakReference;
public MyHandler(RollView rollView) {
this.weakReference = new WeakReference<>(rollView);
}
@Override
public void handleMessage(Message msg) {
RollView rollView = weakReference.get();
if (rollView == null) {
return;
}
rollView.roll();
}
}
public interface onItemClickListener {
/**
* 這裡也可以用泛型,返回當前顯示的自定義資料,為了簡單,這裡就只返回當前點選的位置
* @param position
*/
void onClick(int position);
}
}

​自定義的佈局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/nextText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:background="@color/white"
android:gravity="center_vertical"
android:maxLines="1"
android:text="nextText"
android:textColor="@color/black"
android:textSize="@dimen/sp_16" />
<TextView
android:id="@+id/curText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:background="@color/white"
android:gravity="center_vertical"
android:maxLines="1"
android:text="curText"
android:textColor="@color/black"
android:textSize="@dimen/sp_16" />
</FrameLayout>

自定義屬性:

<declare-styleable name="RollView">
<attr name="rollSize" format="integer" />
<attr name="rollColor" format="reference|color" />
</declare-styleable>

使用:

<RollView
android:id="@+id/rollView"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_56"
android:background="@color/white"
android:paddingLeft="@dimen/dp_20"
app:rollSize="20" />

程式碼:

List<String> contents = new LinkedList<>();
contents.add("lingtao");
contents.add("lingtao1");
contents.add("lingtao2");
contents.add("lingtao3");
rollView.setContents(contents);
rollView.setListener(new RollView.onItemClickListener() {
@Override
public void onClick(int position) {
LogUtils.d("TestActivity_log", "onClick: " + contents.get(position));
}
});
rollView.startRoll();

以上就是 線上直播系統原始碼,滾動式內容展示控制元件實現的相關程式碼,更多內容歡迎關注之後的文章