如何旋轉畫布中的文字?我需要翻轉我倒過來的文字。在畫布中旋轉文字
paint.setTextSize(20);
canvas.drawText("3AM", xStored, yStored, paint);
如何旋轉畫布中的文字?我需要翻轉我倒過來的文字。在畫布中旋轉文字
paint.setTextSize(20);
canvas.drawText("3AM", xStored, yStored, paint);
我從評論的解決方案通過羅曼蓋伊接受的答案
How can you display upside down text with a textview in Android?
下面引用您可以通過-1只是規模上的Y軸。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int cx = this.getMeasuredWidth()/2;
int cy = this.getMeasuredHeight()/2;
canvas.scale(1f, -1f, cx, cy);
canvas.drawText("3AM", cx, cy, p);
}
完整的例子:
public class SView extends View {
Paint p,paint;
public SView(Context context) {
super(context);
// TODO Auto-generated constructor stub
p = new Paint();
p.setColor(Color.RED);
p.setTextSize(40);
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(40);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int cx = this.getMeasuredWidth()/2;
int cy = this.getMeasuredHeight()/2;
canvas.drawText("3AM", cx, cy, paint);
canvas.save();
canvas.scale(1f, -1f, cx, cy);
canvas.drawText("3AM", cx, cy, p);
canvas.restore();
}
}
快
您需要到畫布上的drawText()調用之前旋轉:
canvas.save(); // svare the current state of the canvas
canvas.rotate(180.0f); //rotates 180 degrees
canvas.drawText("3AM", xStored, yStored, paint);
canvas.restore(); //return to 0 degree
**編輯 - 這隻會反轉,但它會回來到前。您實際上需要在文本基線上進行鏡像,假設這就是您的意思。
請參閱本link
int x = 75;
int y = 185;
paint.setColor(Color.GRAY);
paint.setTextSize(25);
String rotatedtext = "Rotated helloandroid :)";
//Draw bounding rect before rotating text:
Rect rect = new Rect();
paint.getTextBounds(rotatedtext, 0, rotatedtext.length(), rect);
canvas.translate(x, y);
paint.setStyle(Paint.Style.FILL);
canvas.drawText(rotatedtext , 0, 0, paint);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(rect, paint);
canvas.translate(-x, -y);
paint.setColor(Color.RED);
canvas.rotate(-45, x + rect.exactCenterX(),y + rect.exactCenterY());
paint.setStyle(Paint.Style.FILL);
canvas.drawText(rotatedtext, x, y, paint);
請指定此答案的部分是與我有關。 – industrychanger
任何解決方案[如何使用RectShape在Android Canvas ShapeDrawable中顯示文本?](http://stackoverflow.com/questions/25401981/how-to-display-text-in-android-canvas-shapedrawable-with-rectshape) –