我需要为Android创建一个应用程序,在其中将显示两色文本和两色背景。请参见左边的图片。然后,应使用动画移动行,并且结果图像应该像右边的图片一样。
我有以下问题:
- 我应该使用2D引擎来完成此操作吗?还是使用标准视图可以?如何操作?
- 如何像图片上那样绘制文本?
---------
我需要为Android创建一个应用程序,在其中将显示两色文本和两色背景。请参见左边的图片。然后,应使用动画移动行,并且结果图像应该像右边的图片一样。
我有以下问题:
---------
在安卓图形API中,我会使用剪辑路径创建剪辑区域。
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();
}
}
这不是完整的答案,我只是提供一些建议。 我知道如何在左边和右边放置图片的一个可能的解决方案。但我无法想出动画部分。我的意思是,如果你想要平滑的状态之间的动画。如果你只想交换视图,那很容易,只需要使用视图翻转器就可以了,但我不认为你想要实现这个...
你可以做的其中一件事是设置背景,比如说320宽度,0-160白色,160-320黑色。然后
tv.setText(Html.fromHtml("<font color='black'>black on white</font> <font color='white'>white on black</font>"));
当然,您需要精确计算黑色和白色字母的数量,但这就是概念。