Bạn có thể tạo chế độ xem tùy chỉnh che dấu văn bản của bạn bằng bộ lọc PorterDuff.
Sau đây là cách nó có thể xem xét:
public class MaskedText extends View {
String sText;
Paint p, pReplace, pMask;
public MaskedText(Context context, AttributeSet attrs) {
super(context, attrs);
// base paint for you text
p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setTextSize(40);
p.setTextAlign(Paint.Align.CENTER);
p.setColor(0xFF000000);
p.setStyle(Paint.Style.FILL);
// replacement color you want for your the part of the text that is masked
pReplace = new Paint(p);
pReplace.setColor(0xFFFFFFFF);
pReplace.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
// color of the drawing you want to mask with text
pMask = new Paint();
pMask.setColor(0xFFFF0000);
pMask.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
}
public void setText(String text) {
sText = text;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
// here you draw the text with the base color. In your case black
canvas.drawText(sText, getWidth()/2, getHeight()/2, p);
// then draw the shape any graphics that you want on top of it
canvas.drawCircle(getWidth()/2, getHeight()/2, 50, pMask);
canvas.drawCircle(getWidth()/2 + 50, getHeight()/2 + 5, 20, pMask);
canvas.drawCircle(getWidth()/2 - 45, getHeight()/2 - 10, 30, pMask);
// finally redraw the text masking the graphics
canvas.drawText(sText, getWidth()/2, getHeight()/2, pReplace);
canvas.restore();
}
}
Đây là kết quả: Masked text
tôi có thể cao bạn này, đây không phải là nhiệm vụ dễ dàng chút nào. Nhưng bắt đầu rất tốt có thể là sử dụng một số thành phần tùy chỉnh như canvas – Lukap
Có chức năng nào, có thể hoàn nguyên màu văn bản tùy thuộc vào nền không? –
bạn đã tìm ra cách để thực hiện điều này? tôi có thể có một câu trả lời nếu bạn không – Ludevik