PorterDuffフィルタを使用してテキストをマスクするカスタム表示を作成できます。ここで
それがどのように見えるかです:
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();
}
}
これが結果です: Masked text
出典
2015-10-23 19:02:17
Pol
ます。この私は背の高い、これはまったく簡単な作業ではありませんすることができます。しかし、非常に良い始めは、キャンバスのようなカスタマイズされたコンポーネントを使用することができます – Lukap
背景に応じてテキストの色を元に戻すことができる関数はありますか? –
あなたはこれを実装する方法を理解しましたか?あなたがしなかった場合私は答えを持っている可能性があります – Ludevik