Android:canvas.drawBitmap()方法无法正常工作 [英] Android : canvas.drawBitmap() method not working properly

查看:1254
本文介绍了Android:canvas.drawBitmap()方法无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发布了两个相关的问题(请参考

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

 

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

这里,首先你应该这样更改值,

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


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

    }

解决方案

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

这篇关于Android:canvas.drawBitmap()方法无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆