|
|
|
|
@ -28,8 +28,15 @@ package com.github.tvbox.osc.subtitle.widget; |
|
|
|
|
import android.annotation.SuppressLint; |
|
|
|
|
import android.content.Context; |
|
|
|
|
import androidx.annotation.Nullable; |
|
|
|
|
|
|
|
|
|
import android.graphics.Canvas; |
|
|
|
|
import android.graphics.Color; |
|
|
|
|
import android.graphics.Paint; |
|
|
|
|
import android.text.Html; |
|
|
|
|
import android.text.TextPaint; |
|
|
|
|
import android.text.TextUtils; |
|
|
|
|
import android.util.AttributeSet; |
|
|
|
|
import android.view.ViewGroup; |
|
|
|
|
import android.widget.TextView; |
|
|
|
|
|
|
|
|
|
import com.github.tvbox.osc.cache.CacheManager; |
|
|
|
|
@ -59,19 +66,24 @@ public class SimpleSubtitleView extends TextView |
|
|
|
|
|
|
|
|
|
public boolean hasInternal = false; |
|
|
|
|
|
|
|
|
|
private TextView backGroundText = null;//用于描边的TextView
|
|
|
|
|
|
|
|
|
|
public SimpleSubtitleView(final Context context) { |
|
|
|
|
super(context); |
|
|
|
|
backGroundText = new TextView(context); |
|
|
|
|
init(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public SimpleSubtitleView(final Context context, final AttributeSet attrs) { |
|
|
|
|
super(context, attrs); |
|
|
|
|
backGroundText = new TextView(context, attrs); |
|
|
|
|
init(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public SimpleSubtitleView(final Context context, final AttributeSet attrs, |
|
|
|
|
final int defStyleAttr) { |
|
|
|
|
super(context, attrs, defStyleAttr); |
|
|
|
|
backGroundText = new TextView(context, attrs, defStyleAttr); |
|
|
|
|
init(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -176,4 +188,57 @@ public class SimpleSubtitleView extends TextView |
|
|
|
|
destroy(); |
|
|
|
|
super.onDetachedFromWindow(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void setLayoutParams(ViewGroup.LayoutParams params) { |
|
|
|
|
//同步布局参数
|
|
|
|
|
backGroundText.setLayoutParams(params); |
|
|
|
|
super.setLayoutParams(params); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
|
|
|
|
CharSequence tt = backGroundText.getText(); |
|
|
|
|
//两个TextView上的文字必须一致
|
|
|
|
|
if (TextUtils.isEmpty(tt) || !tt.equals(this.getText())) { |
|
|
|
|
backGroundText.setText(getText()); |
|
|
|
|
this.postInvalidate(); |
|
|
|
|
} |
|
|
|
|
backGroundText.measure(widthMeasureSpec, heightMeasureSpec); |
|
|
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { |
|
|
|
|
if (backGroundText != null) { |
|
|
|
|
backGroundText.setText(text); |
|
|
|
|
} |
|
|
|
|
super.onTextChanged(text, start, lengthBefore, lengthAfter); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
|
|
|
|
backGroundText.layout(left, top, right, bottom); |
|
|
|
|
super.onLayout(changed, left, top, right, bottom); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void onDraw(Canvas canvas) { |
|
|
|
|
//其他地方,backGroundText和super的先后顺序影响不会很大,但是此处必须要先绘制backGroundText,
|
|
|
|
|
drawBackGroundText(); |
|
|
|
|
backGroundText.draw(canvas); |
|
|
|
|
super.onDraw(canvas); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void drawBackGroundText() { |
|
|
|
|
TextPaint tp = backGroundText.getPaint(); |
|
|
|
|
//设置描边宽度
|
|
|
|
|
tp.setStrokeWidth(10); |
|
|
|
|
//背景描边并填充全部
|
|
|
|
|
tp.setStyle(Paint.Style.FILL_AND_STROKE); |
|
|
|
|
//设置描边颜色
|
|
|
|
|
backGroundText.setTextColor(Color.BLACK); |
|
|
|
|
//将背景的文字对齐方式做同步
|
|
|
|
|
backGroundText.setGravity(getGravity()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|