Clean code
This commit is contained in:
parent
d7a31b4eda
commit
a60e9dc323
|
|
@ -2,38 +2,56 @@ package com.github.catvod.spider;
|
|||
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Typeface;
|
||||
import android.view.Gravity;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import com.github.catvod.crawler.Spider;
|
||||
import com.github.catvod.ui.MarqueeView;
|
||||
import com.github.catvod.ui.ScrollTextView;
|
||||
import com.github.catvod.utils.Utils;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class Notice extends Spider {
|
||||
|
||||
private MarqueeView view;
|
||||
private static final String SPACE = " ";
|
||||
private ScrollTextView view;
|
||||
|
||||
@Override
|
||||
public void init(Context context, String extend) {
|
||||
super.init(context, extend);
|
||||
Init.run(() -> createView(extend));
|
||||
String[] splits = extend.split("#");
|
||||
String text = splits[0];
|
||||
int duration = splits.length > 1 ? Integer.parseInt(splits[1]) : 30;
|
||||
Init.run(() -> createView(text, duration));
|
||||
}
|
||||
|
||||
private void createView(String extend) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < 3; i++) sb.append(extend).append(" ");
|
||||
view = new MarqueeView(Init.context());
|
||||
view.setText(sb.toString());
|
||||
view.setBackgroundColor(Color.argb(200, 255, 255, 255));
|
||||
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, Utils.dp2px(56));
|
||||
params.gravity = Gravity.TOP;
|
||||
Utils.addView(view, params);
|
||||
private void createView(String text, int duration) {
|
||||
createText(text, duration);
|
||||
createLayout();
|
||||
updateColor();
|
||||
hide();
|
||||
}
|
||||
|
||||
private void createLayout() {
|
||||
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
|
||||
params.gravity = Gravity.TOP;
|
||||
Utils.addView(view, params);
|
||||
}
|
||||
|
||||
private void createText(String text, int duration) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < 2; i++) sb.append(SPACE).append(text);
|
||||
view = new ScrollTextView(Init.context());
|
||||
view.setTextSize(20);
|
||||
view.setDuration(duration);
|
||||
view.setText(sb.toString());
|
||||
view.setTypeface(null, Typeface.BOLD);
|
||||
view.setPadding(0, Utils.dp2px(16), 0, Utils.dp2px(16));
|
||||
view.setBackgroundColor(Color.argb(200, 255, 255, 255));
|
||||
view.startScroll();
|
||||
}
|
||||
|
||||
private void hide() {
|
||||
Init.run(() -> Utils.removeView(view), 30 * 1000);
|
||||
}
|
||||
|
|
@ -46,7 +64,7 @@ public class Notice extends Spider {
|
|||
@Override
|
||||
public void run() {
|
||||
Random random = new Random();
|
||||
view.setTextColor(Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256)));
|
||||
view.setTextColor(Color.argb(255, random.nextInt(128), random.nextInt(128), random.nextInt(128)));
|
||||
updateColor();
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,159 +0,0 @@
|
|||
package com.github.catvod.ui;
|
||||
|
||||
import android.animation.ValueAnimator;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.text.method.LinkMovementMethod;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.Gravity;
|
||||
import android.view.MotionEvent;
|
||||
import android.widget.HorizontalScrollView;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class MarqueeView extends HorizontalScrollView {
|
||||
|
||||
private TextView mTextView;
|
||||
private TextView mGhostTextView;
|
||||
private int viewWidth;
|
||||
private int measureText;
|
||||
private int mOffset = 0;
|
||||
private int mGhostOffset = 0;
|
||||
private int spacing = 100;
|
||||
private int speed = 1;
|
||||
|
||||
private ValueAnimator valueAnimator;
|
||||
|
||||
public MarqueeView(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public MarqueeView(Context context, AttributeSet attrs) {
|
||||
this(context, attrs, 0);
|
||||
}
|
||||
|
||||
public MarqueeView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
initLayout();
|
||||
initAnim();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
viewWidth = getMeasuredWidth();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
||||
super.onSizeChanged(w, h, oldw, oldh);
|
||||
if (measureText > viewWidth) {
|
||||
startAnim();
|
||||
} else {
|
||||
stopAnim();
|
||||
}
|
||||
}
|
||||
|
||||
private void initLayout() {
|
||||
RelativeLayout relativeLayout = new RelativeLayout(getContext());
|
||||
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
|
||||
relativeLayout.setLayoutParams(layoutParams);
|
||||
addView(relativeLayout);
|
||||
mTextView = createTextView();
|
||||
mGhostTextView = createTextView();
|
||||
relativeLayout.addView(mTextView);
|
||||
relativeLayout.addView(mGhostTextView);
|
||||
}
|
||||
|
||||
private void initAnim() {
|
||||
valueAnimator = ValueAnimator.ofFloat(0, measureText);
|
||||
valueAnimator.addUpdateListener(animatorUpdateListener);
|
||||
valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
|
||||
valueAnimator.setRepeatMode(ValueAnimator.RESTART);
|
||||
}
|
||||
|
||||
public void setSpacing(int spacing) {
|
||||
this.spacing = spacing;
|
||||
}
|
||||
|
||||
public void setSpeed(int speed) {
|
||||
this.speed = speed;
|
||||
}
|
||||
|
||||
public void setText(CharSequence text) {
|
||||
mTextView.setText(text);
|
||||
mGhostTextView.setText(text);
|
||||
measureText = (int) mTextView.getPaint().measureText(text, 0, text.length());
|
||||
resetMarqueeView();
|
||||
if (measureText > viewWidth) {
|
||||
startAnim();
|
||||
} else {
|
||||
stopAnim();
|
||||
}
|
||||
}
|
||||
|
||||
public void setTextColor(int color) {
|
||||
mTextView.setTextColor(color);
|
||||
mGhostTextView.setTextColor(color);
|
||||
}
|
||||
|
||||
private TextView createTextView() {
|
||||
TextView textView = new TextView(getContext());
|
||||
textView.setPadding(0, 0, 0, 0);
|
||||
textView.setSingleLine();
|
||||
textView.setTextSize(20);
|
||||
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
|
||||
textView.setLayoutParams(layoutParams);
|
||||
textView.setGravity(Gravity.CENTER_VERTICAL);
|
||||
textView.setMovementMethod(LinkMovementMethod.getInstance());
|
||||
return textView;
|
||||
}
|
||||
|
||||
private void resetMarqueeView() {
|
||||
mOffset = 0;
|
||||
mGhostOffset = measureText + spacing;
|
||||
mGhostTextView.setX(mGhostOffset);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void startAnim() {
|
||||
valueAnimator.setDuration(measureText);
|
||||
stopAnim();
|
||||
valueAnimator.start();
|
||||
}
|
||||
|
||||
public void stopAnim() {
|
||||
valueAnimator.cancel();
|
||||
resetMarqueeView();
|
||||
}
|
||||
|
||||
ValueAnimator.AnimatorUpdateListener animatorUpdateListener = new ValueAnimator.AnimatorUpdateListener() {
|
||||
@Override
|
||||
public void onAnimationUpdate(ValueAnimator animation) {
|
||||
mOffset -= speed;
|
||||
mGhostOffset -= speed;
|
||||
if (mOffset + measureText < 0) {
|
||||
mOffset = mGhostOffset + measureText + spacing;
|
||||
}
|
||||
if (mGhostOffset + measureText < 0) {
|
||||
mGhostOffset = mOffset + measureText + spacing;
|
||||
}
|
||||
invalidate();
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
if (mTextView == null || mGhostTextView == null) return;
|
||||
mTextView.setX(mOffset);
|
||||
mGhostTextView.setX(mGhostOffset);
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent ev) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
package com.github.catvod.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Rect;
|
||||
import android.text.TextPaint;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.animation.LinearInterpolator;
|
||||
import android.widget.Scroller;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class ScrollTextView extends TextView {
|
||||
|
||||
private Scroller scroller;
|
||||
private int duration = 30;
|
||||
|
||||
public ScrollTextView(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public ScrollTextView(Context context, AttributeSet attrs) {
|
||||
this(context, attrs, android.R.attr.textViewStyle);
|
||||
}
|
||||
|
||||
public ScrollTextView(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
setSingleLine();
|
||||
setEllipsize(null);
|
||||
setVisibility(INVISIBLE);
|
||||
}
|
||||
|
||||
public void setDuration(int duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public void startScroll() {
|
||||
int width = -1 * getWidth();
|
||||
setHorizontallyScrolling(true);
|
||||
setScroller(scroller = new Scroller(getContext(), new LinearInterpolator()));
|
||||
int scrollingLen = calculateScrollingLen();
|
||||
int distance = scrollingLen - (getWidth() + width);
|
||||
scroller.startScroll(width, 0, distance, 0, duration * 1000);
|
||||
setVisibility(VISIBLE);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
private int calculateScrollingLen() {
|
||||
TextPaint paint = getPaint();
|
||||
Rect rect = new Rect();
|
||||
String text = getText().toString();
|
||||
paint.getTextBounds(text, 0, text.length(), rect);
|
||||
return rect.width() + getWidth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void computeScroll() {
|
||||
super.computeScroll();
|
||||
if (scroller == null) return;
|
||||
if (scroller.isFinished()) {
|
||||
scroller.abortAnimation();
|
||||
setVisibility(GONE);
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
|
@ -1 +1 @@
|
|||
8cb598596c187e9b24ff1fe92523f43c
|
||||
00525bd9b7eaf9976bac9fa981c9bcda
|
||||
|
|
|
|||
Loading…
Reference in New Issue