Android : canvas.drawBitmap() 方法无法正常工作

Android : canvas.drawBitmap() method not working properly

我已经 post 两个与此相关的问题(请参阅此 custom piechart 1 and this custom pie chart 2) 但没有得到答案,最后我开发了自己的但在某个时候卡住了。

我需要在绘制区域之间创建线条,如图所示。对于内圈我使用了 canvas.drawArc()。对于外圈我使用了 canvas.drawCircle(),对于中间线我使用了 [=17] =] 不同角度的方法。这里正确绘制了内圆和外圆,但对于中间圆弧,只有前两个 bitmap/arcs 正确绘制,但其余两个不在其确切位置。

我使用相同的代码绘制所有 bitmap/arcs 但没有得到准确的输出。我被卡住什么是确切的 problem.need 帮助,在此先感谢。

你可以看到我的输出

这里..

听到的是我的 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);
    }

这是我要生成的 customview class

    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();    

    }

谢谢大家。 最后我用替换这些东西做到了 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);