从活动中的弹出窗口手动调用onDraw

时间:2014-03-25 16:14:57

标签: android invalidation

我不明白。 我尝试过很多像

这样的建议
this.findViewById(android.R.id.content).postInvalidate();
getWindow().getDecorView().findViewById(android.R.id.content).invalidate();
getWindow().getDecorView().findViewById(android.R.id.content).postInvalidate();
    etc
从stackoverflow

但它仍然不起作用。

问题: 活动 - > setContentView(new drawView(this,controller)); 活动 - > PopupWindow

我如何从PopupWindow中调用DrawView中的onDraw?

编辑:

public class GraphActivity extends Activity {
   ... 
   setContentView(new DrawView(this,controller));
   ...
   public void showTransitionTable() {
      ...
      View popupview = getLayoutInflater().inflate(R.layout.sim_pop, sim,false);
      ...
      btnClock.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View arg0) {
      //call here DrawView onDraw
      }
}


public class DrawView extends View {
    protected void onDraw(Canvas canvas) {
    ...
    }
}

1 个答案:

答案 0 :(得分:0)

这就是我的意思......

public class MainActivity extends Activity {

private class DrawView extends View {

    private List<Point> pointList;
    private Paint paint = new Paint();
    private Random random = new Random();
    private Paint background = new Paint();

    public DrawView(Context context) {
        super(context);
        background.setColor(Color.BLACK);
    }

    @Override
    public void onDraw(Canvas canvas) {
        if(pointList==null) return;
        canvas.drawPaint(background);
        for (Point p : pointList) {
            paint.setColor(Color.argb(255, random.nextInt(255), random.nextInt(255), random.nextInt(255)));
            canvas.drawCircle(p.x, p.y, 10+random.nextInt(10), paint);
        }
    }

    public void setPoints(List<Point> pointList) {
        this.pointList = pointList;
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final DrawView view = new DrawView(this);
    setContentView(view);

    final List<Point> pointList = new ArrayList<Point>();
    view.setPoints(pointList);

    final Random random = new Random();

    new AlertDialog.Builder(this).setMessage("Click OK for more circles").setPositiveButton("OK", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            for(int i= 0; i<10; i++) pointList.add(new Point(random.nextInt(view.getWidth()), random.nextInt(view.getHeight())));
            view.invalidate();
        }
    }).create().show();

}

}