调用invalidate时,在其他自定义视图中调用onDraw

时间:2013-12-15 20:31:07

标签: java android android-canvas

我有两个具有相同方法的自定义视图,当我在其中调用invalidate()时,它会调用onDraw方法。我尝试了几种方法,包括this.invalidate(),但它仍然会调用它。我能想到的唯一方法就是在另一个类中添加一个布尔值来检查它是否在onDraw()内调用它。

自定义视图1

public void showPercentage(double percent, double out){
    cropx =  (int) percent;
    outof = (int) out;
    this.invalidate();
}
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if(cropx != 0){
    croppedBmp = Bitmap.createBitmap(drawGraph2, 0, 0, (drawGraph2.getWidth()*cropx/100), drawGraph2.getHeight());
    canvas.drawBitmap(croppedBmp, 0, 0, new Paint());
    croppedBmp.recycle();
    System.gc();
    Runtime.getRuntime().gc();
    }
}

自定义视图2

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //System.out.print to check if its being called
    System.out.println("hey");
    Bitmap croppedBmp = Bitmap.createBitmap(drawGraph2);
    canvas.drawBitmap(croppedBmp, 0, 0, new Paint());
}

0 个答案:

没有答案