我想将矩阵应用于文本视图。我可以在onDraw
方法
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
canvas.drawPaint(paint);
paint.setColor(Color.BLACK);
paint.setTextSize(20);
//transform is the matrix
canvas.drawBitmap(textAsBitmap("0ps", 26, Color.RED), transform, paint);
}
public Bitmap textAsBitmap(String text, float textSize, int textColor) {
Paint paint = new Paint(ANTI_ALIAS_FLAG);
paint.setTextSize(textSize);
paint.setColor(textColor);
paint.setTextAlign(Paint.Align.LEFT);
float baseline = -paint.ascent(); // ascent() is negative
int width = (int) (paint.measureText(text) + 0.5f); // round
int height = (int) (baseline + paint.descent() + 0.5f);
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
canvas.drawText(text, 0, baseline, paint);
return image;
}
但问题是,如果翻译大于视图维度,那么就无法看到翻译。如果视图具有matchparent
维,那么就没有问题,它可以正常工作。
如果翻译在白色屏幕上更大,则无法看到它