我已经发布了两个与此相关的问题(请参阅此内容 custom piechart 1和custom pie chart 2}但是没有得到答案,最后我发展了自己但在某些时候陷入困境。
我需要在绘制区域之间创建线条,如图所示。对于内圈,我使用canvas.drawArc()
。对于外圈,我使用canvas.drawCircle()
,对于中间线我使用canvas.drawBitmap()
不同角度的方法。内圆和外圆正确绘制,但对于中间圆弧,只有前两个位图/弧被正确绘制,但其余两个不在它们的确切位置。
我使用相同的代码绘制所有位图/弧但没有得到确切的输出。我遇到了什么是确切的问题。需要帮助,提前谢谢。
你可以看到我的输出
这里..
听到我的 onCreate(),其中我生成并设置了视图。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv1 = (LinearLayout) findViewById(R.id.linear);
matrix = new Matrix();
MyView myview = new MyView(this);
lv1.addView(myview);
}
这是我要生成的自定义视图类。
public class MyView extends View {
private Paint p ,paint,paint_text;
private int startX;
private int startY;
private int radius;
private ArrayList<Integer> colors;
private ArrayList<Float> values;
Bitmap bitmap;
Context mContext;
RectF rectF , rectF2 ;
public MyView(Context context) {
super(context);
mContext = context;
p = new Paint();
p.setAntiAlias(true);
colors = new ArrayList<Integer>();
values = new ArrayList<Float>();
Display display = getWindowManager().getDefaultDisplay();
width1 = display.getWidth();
height1 = display.getHeight();
startX = 0 ;
startY = 0;
radius = (int) (width1/2);
colors.add(Color.RED);
colors.add(Color.BLUE);
colors.add(Color.YELLOW);
colors.add(Color.GREEN);
values.add(8f);
values.add(2f);
values.add(4f);
values.add(2f);
}
这是我的 onDraw()方法。
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float _angle1 = 0 ,_angle2 = 0 ,_angle3 = 0 , _angle4 =0;
Bitmap myBitmap1 = BitmapFactory.decodeResource(getResources(),R.drawable.saperate_line);
bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
float offset = 0;
float angle = (float) 5.60 ;
matrix.reset();
canvas.translate(0,canvas.getHeight()); //reset where 0,0 is located
canvas.scale(1,-1); // for scaling
rectF = new RectF();
rectF.set(-(getStartX() + getRadius()), -(getStartY() + getRadius()), getStartX() + getRadius(),getStartY() + getRadius());
//for creating outer circle using canvas.drawCircle-----------
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(0, 0, (float) (width1/(1.4)), paint);
//for creating inner circle using canvas.drawArc-----------
for (int i = 0; i < values.size(); i++) {
p.setColor(colors.get(i));
if (i == 0) {
canvas.drawArc(rectF, offset, values.get(i) * angle, true, p);
_angle1 = ((offset+(values.get(i) * angle)))/2;
matrix.postRotate(_angle1);
canvas.drawBitmap(myBitmap1, matrix, null);
}
if(i == 1){
canvas.drawArc(rectF, offset, values.get(i) * angle, true, p);
_angle2 = ((offset + (values.get(i) * angle)))/2;
matrix.postRotate(_angle2);
canvas.drawBitmap(myBitmap1, matrix, null);
}
if(i == 2){
canvas.drawArc(rectF, offset, values.get(i) * angle, true, p);
_angle3 = ((offset + (values.get(i) * angle)))/2;
// _angle3 = (offset + angle);
matrix.postRotate(_angle3);
canvas.drawBitmap(myBitmap1, matrix, null);
}
if (i == 3){
canvas.drawArc(rectF, offset, values.get(i) * angle, true, p);
_angle4 = ((offset + (values.get(i) * angle)))/2;
matrix.postRotate(_angle4);
canvas.drawBitmap(myBitmap1, matrix, null);
}
offset += (values.get(i) * angle);
Log.e("new offset :: ","****************************"+offset);
}
canvas.save();
}
答案 0 :(得分:0)
感谢每一个人。 最后我用替换这些东西做了Rotating Image on A canvas
matrix.postRotate(_angle1);
canvas.drawBitmap(myBitmap1, matrix, null);
用这个
Matrix matrix1 = new Matrix();
matrix1.setRotate(_angle1, 0, 0);
canvas.drawBitmap(myBitmap1, matrix1, null);
这里,首先你要改变这样的值,
float myAngle = 0;
myAngle = ((90 * values.get(i)) / 16);