Android : canvas.drawBitmap() method not working properly

陌路散爱 提交于 2019-12-20 04:38:13

问题


I already post two question related this (plz refer this custom piechart 1 and this custom pie chart 2) but not getting answer ,finally i develop my own but getting stuck at some point .

I need to create lines between drawn area as you can see in image .for inner circle i used canvas.drawArc() .for outer circle i used canvas.drawCircle() and for middle lines i used canvas.drawBitmap() method with differ angle .here inner and outer circle drawn properly but for middle arc ,only first two bitmap/arcs are drawn properly but remaining two are not in their exact position .

I used same code for drawing all bitmap/arcs but not getting exact output .I am getting stuck what is the exact problem.need help ,thanks in advance .

you can see my output

here..

Hear is my onCreate() , in which i generate and set view .

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

this is my customview class that i want to generate.

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

This is my onDraw() method .

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

    }

回答1:


Thanks to every one. finally i did it using replacing this stuff Rotating Image on A canvas

 matrix.postRotate(_angle1);    
 canvas.drawBitmap(myBitmap1, matrix, null);

with this

Matrix matrix1 = new Matrix();
matrix1.setRotate(_angle1, 0, 0);
canvas.drawBitmap(myBitmap1, matrix1, null);

here ,first you should change the value like this ,

 float myAngle = 0;
 myAngle = ((90 * values.get(i)) / 16);


来源:https://stackoverflow.com/questions/28690202/android-canvas-drawbitmap-method-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!