我正在开发可扩展的布局系统。
我想知道如何检测视图或其子视图是否被称为绘图。
下面:我正在使用的代码。
scaleView = new View(this) {
private Paint paint = new Paint();
private View rootView;
private Rect rootViewRect = new Rect();
private Rect windowVisibleDisplayFrameRect = new Rect();
{
this.rootView = ((ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content)).getChildAt(0);
this.getWindowVisibleDisplayFrame(windowVisibleDisplayFrameRect);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
postInvalidate();
}
}, 0, 100);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
rootView.buildDrawingCache();
Bitmap drawingCache = rootView.getDrawingCache();
if (rootViewRect.right == 0) {
rootViewRect = new Rect(0, 0, rootView.getWidth(), rootView.getHeight());
return;
}
canvas.drawBitmap(drawingCache, rootViewRect, windowVisibleDisplayFrameRect, null);
rootView.destroyDrawingCache();
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
event.setLocation(event.getX() * ((float) rootViewRect.right / windowVisibleDisplayFrameRect.right), event.getY() * ((float) rootViewRect.bottom / windowVisibleDisplayFrameRect.bottom));
rootView.dispatchTouchEvent(event);
return false;
}
};
this.addContentView(scaleView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
我目前正在使用计时器来调用invalidate,但它很慢并需要CPU使用。
所以我想只在视图或其子项被更改时才会失效。
我该怎么做?