New toy
This commit is contained in:
parent
b72d12278d
commit
4326eafac3
|
|
@ -0,0 +1,53 @@
|
||||||
|
package com.github.catvod.spider;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.view.Gravity;
|
||||||
|
import android.widget.FrameLayout;
|
||||||
|
|
||||||
|
import com.github.catvod.crawler.Spider;
|
||||||
|
import com.github.catvod.ui.MarqueeView;
|
||||||
|
import com.github.catvod.utils.Utils;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class Notice extends Spider {
|
||||||
|
|
||||||
|
private MarqueeView view;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(Context context, String extend) {
|
||||||
|
super.init(context, extend);
|
||||||
|
Init.run(() -> createView(extend));
|
||||||
|
}
|
||||||
|
|
||||||
|
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.parseColor("#CCFFFFFF"));
|
||||||
|
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, Utils.dp2px(56));
|
||||||
|
params.gravity = Gravity.TOP;
|
||||||
|
Utils.addView(view, params);
|
||||||
|
updateColor();
|
||||||
|
hide();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void hide() {
|
||||||
|
Init.run(() -> Utils.removeView(view), 30 * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateColor() {
|
||||||
|
Init.run(runnable, 250);
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Runnable runnable = new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Random random = new Random();
|
||||||
|
view.setTextColor(Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256)));
|
||||||
|
updateColor();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,159 @@
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -171,6 +171,15 @@ public class Utils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void removeView(View view) {
|
||||||
|
try {
|
||||||
|
ViewGroup group = Init.getActivity().getWindow().getDecorView().findViewById(android.R.id.content);
|
||||||
|
group.removeView(view);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void loadWebView(String url, WebViewClient client) {
|
public static void loadWebView(String url, WebViewClient client) {
|
||||||
Init.run(() -> {
|
Init.run(() -> {
|
||||||
WebView webView = new WebView(Init.context());
|
WebView webView = new WebView(Init.context());
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -1 +1 @@
|
||||||
b33ab66a0bc515720d7e9083fb52fbfc
|
4fc27bf2f140902e9d7a5bad4e051946
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue