画布:使用片段更改方向上的错误渲染

时间:2013-04-16 08:00:32

标签: java android android-canvas

在我的项目中,我有一个用于绘制画布的视图。

当加载了acvitity / fragment时,一切都很好,并且图像被正确渲染。

但是当方向改变时,它会部分呈现。 以下是两个例子:

  1. 这里是正确的渲染
  2. This is the correct rendering

    2.这是方向改变时的渲染:

    Bar rendering of canvas

    我编写的类扩展了View,并覆盖了onDraw()方法。这是代码:

        @Override
    protected void onDraw(Canvas canvas){       
         super.onDraw(canvas);  
         int size = this.width/2;
         int left = size/2;
         int right = size +left;
         canvas.drawRoundRect(resistorRects[0], 6, 6,resPaint[0]);      
         canvas.drawRect(resistorRects[FIRST_COLOR], resPaint[1]);       
         canvas.drawRect(resistorRects[SECOND_COLOR], resPaint[2]);
         canvas.drawRect(resistorRects[THIRD_COLOR], resPaint[3]);
         canvas.drawRect(resistorRects[MULTIPLIER], resPaint[4]);
         canvas.drawRect(resistorRects[TOLERANCE], resPaint[5]);
         canvas.drawLine((float)left-15, (float)16.5, (float)left, (float)17.5, resPaint[6]);
         canvas.drawLine((float)right, (float)16.5, (float)right+15, (float)17.5, resPaint[6]);
    }   
    

    该类的构造函数是:

        public ResistorGraphicsView(Context context, int width) {       
        super(context);     
        this.width = width;     
        int i=0;        
        resPaint = new Paint[7];
        resistorRects = new RectF[6];
        while(i < 6){
            resPaint[i] = new Paint(Paint.ANTI_ALIAS_FLAG);
            resPaint[i].setColor(Color.parseColor("#DEADBEEF"));
            i++;
        }
        int size = this.width/2;
        int left = size/2;
        int right = size +left;
        resPaint[6] = new Paint(Paint.ANTI_ALIAS_FLAG);
        resPaint[6].setColor(Color.parseColor("#FFFFFFFF"));
    
    
        resistorRects[0]= new RectF(left,0,right,35);               
        resistorRects[FIRST_COLOR]= new RectF(left+10,0,left+20,35);
        resistorRects[SECOND_COLOR]= new RectF(left+30,0,left+40,35);
        resistorRects[THIRD_COLOR] = new RectF(left+50,0, left+60,35);
        resistorRects[MULTIPLIER]= new RectF(left+70,0,left+80,35);
        resistorRects[TOLERANCE]= new RectF(right-30,0,right-20,35);                        
    }
    

    奇怪的是,当我选择膨胀布局的选项时,在侧窗格上正确渲染图像:

    case RESISTOR_VALUE:
        getFragmentManager().beginTransaction().replace(R.id.activity_container, new ResistorCalcFragment()).commit();
            break;
    

    实际上,如果在糟糕的渲染之后,我从侧窗格中选择它,那么视图就会正确渲染。

    我尝试添加一些断点,似乎所有内容都被正确调用。请注意,每次旋转视图时,我都会创建该对象的新实例。

0 个答案:

没有答案