![](/img/trans.png)
[英]How to alter the output of Android Canvas.drawTextOnPath()
[英]Android canvas:drawTextOnPath() behave differently on different devices
我有一个创建带有8个项目的循环菜单的要求。 为此,我创建了一个自定义视图,如下所示。
public class CircleTextView extends View implements View.OnClickListener {
private String BookName = null;
private int BookIndex;
private Context context;
public CircleTextView(Context context, String bookName, int bookIndex) {
super(context);
this.BookName = bookName;
this.BookIndex = bookIndex;
this.context = context;
}
@Override
protected void onDraw(Canvas canvas) {
if(canvas == null)
return;
Path mArc = new Path();
RectF ractangle;
switch (this.BookIndex){
case 0:
ractangle = new RectF(70,70,270,270);
//ractangle = new RectF(0,0,100,100);
mArc.addArc(ractangle, 110, 230);
break;
case 1:
ractangle = new RectF(-20,50,180,150);
mArc.addArc(ractangle, 120, 290);
break;
case 2:
ractangle = new RectF(-120,70,80,270);
mArc.addArc(ractangle, 180, 270);
break;
case 3:
ractangle = new RectF(50,-10,250,190);
mArc.addArc(ractangle, 90, 180);
break;
case 4:
ractangle = new RectF(0,0,0,0);
mArc.addArc(ractangle, 0, 0);
break;
case 5:
ractangle = new RectF(-100,-10,100,190);
mArc.addArc(ractangle, 310, 95);
break;
case 6:
ractangle = new RectF(80,-110,280,90);
mArc.addArc(ractangle, 60, 140);
break;
case 7:
ractangle = new RectF(10,30,160,100);
mArc.addArc(ractangle, 50, 100);
break;
case 8:
ractangle = new RectF(-110,-80,90,80);
mArc.addArc(ractangle, 0, 80);
break;
}
Paint mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintText.setStyle(Paint.Style.STROKE);
mPaintText.setColor(Color.WHITE);
mPaintText.setTextSize(15f);
mPaintText.setTextAlign(Paint.Align.CENTER);
canvas.drawTextOnPath(getQualifiedName(this.BookName), mArc, 0, 0, mPaintText);
this.setOnClickListener(this);
invalidate();
}
private String getQualifiedName(String name){
int length = name.length();
String qName = name;
if(length >8){
qName = name.substring(0, 3) + ".." + name.substring((length - 5), length);
}
return qName.toUpperCase();
}
@Override
public void onClick(View v) {
CircleTextView _circleTextView = (CircleTextView)v;
if (_circleTextView.getTag() != "") {
((PlayerActivity)context).loadChapters(_circleTextView.getTag().toString());
TextView chapterTextView = (TextView)((PlayerActivity)context).findViewById(R.id.currentChapterTextView);
chapterTextView.setText(((PlayerActivity) context).CurrentlyPlayingBook.getName());
((PlayerActivity)context).setAngleRange();
((PlayerActivity)context).setBackgroundImage(_circleTextView.getTag().toString());
((PlayerActivity)context).stopOngoingRequests();
}
}
}
画布上的图纸在不同设备中的行为不同。 从屏幕1和屏幕2清晰可见
我只是在努力解决这个问题。 请帮助解决此问题。
大概
1.您可以使用相对于View的宽度和高度的值:
int w=getWidth();
int h=getHeight();
.
.
.
new RectF(w*x1, h*y1, w*x2, h*y2);
其中x1,y1和x2,y2可用于适当地适合工程图。 要么
1。
Bitmap bmp=Bitmap.createBitmap(desiredWidth, desiredHeight, Bitmap.Config.ARGB_8888);
Canvas tmp=new Canvas(bmp);
2然后在上面画你的东西:
tmp.drawText....
tmp.drawSomething...
3.然后缩放:
tmp.save();
tmp.scale(x,y);
tmp.restore();
4.最后在onDraw
方法中使用该位图:
canvas.drawBitmap(tmp,0,0,null);
希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.