我有一个要求创建包含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
我正在努力解决这个问题。请帮忙解决这个问题。
答案 0 :(得分:0)
可能
1.您可以使用相对于View的宽度和高度的值:
int w=getWidth();
int h=getHeight();
.
.
.
new RectF(w*x1, h*y1, w*x2, h*y2);
其中x1,y1和x2,y2可用于正确拟合图纸。 OR
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);
希望你觉得它有用。