Untitled
unknown
plain_text
a year ago
1.6 kB
8
Indexable
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatTextView;
public class OutlineTextView extends AppCompatTextView {
private Paint strokePaint;
private float strokeWidthInPx;
public OutlineTextView(Context context) {
super(context);
init(context);
}
public OutlineTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public OutlineTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
float strokeWidthInDp = 0.5f;
strokeWidthInPx = strokeWidthInDp * context.getResources().getDisplayMetrics().density;
strokePaint = new Paint();
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setStrokeWidth(strokeWidthInPx);
strokePaint.setColor(Color.parseColor("#40000000")); // Outline color with 25% opacity
strokePaint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
int currentTextColor = getCurrentTextColor();
// Draw outline
setTextColor(strokePaint.getColor());
getPaint().setStyle(Paint.Style.STROKE);
getPaint().setStrokeWidth(strokeWidthInPx);
super.onDraw(canvas);
// Draw text
setTextColor(currentTextColor);
getPaint().setStyle(Paint.Style.FILL);
super.onDraw(canvas);
}
}
Editor is loading...
Leave a Comment