Drawing over a view and all it's children

邮差的信 提交于 2019-12-09 05:23:13

问题


I'm trying to apply a visual effect to a viewgroup. My idea is to grab a bitmap of the viewgroup, shrink it down, expand it back up, and draw it over the viewgroup to give it a blocky, low quality effect.

I've got most of the way there using this code:

public class Blocker {

    private static final float RESAMPLE_QUALITY = 0.66f; // less than 1, lower = worse quality


    public static void block(Canvas canvas, Bitmap bitmap_old) {
        block(canvas, bitmap_old, RESAMPLE_QUALITY);
    }


    public static void block(Canvas canvas, Bitmap bitmap_old, float quality) {
        Bitmap bitmap_new = Bitmap.createScaledBitmap(bitmap_old, Math.round(bitmap_old.getWidth() * RESAMPLE_QUALITY), Math.round(bitmap_old.getHeight() * RESAMPLE_QUALITY), true);
        Rect from = new Rect(0, 0, bitmap_new.getWidth(), bitmap_new.getHeight());
        RectF to = new RectF(0, 0, bitmap_old.getWidth(), bitmap_old.getHeight());
        canvas.drawBitmap(bitmap_new, from, to, null);
    }
}

I simply pass in the canvas to draw on and a bitmap of what needs to be scaled down+up and it works well.

public class BlockedLinearLayout extends LinearLayout {

    private static final String TAG = BlockedLinearLayout.class.getSimpleName();


    public BlockedLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        applyCustomAttributes(context, attrs);
        setup();
    }


    public BlockedLinearLayout(Context context) {
        super(context);
        setup();
    }


    private void setup() {
        this.setDrawingCacheEnabled(true);
    }


    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        // block(canvas); If I call this here, it works but no updates
    }


    @Override
    public void onDraw(Canvas canvas) {
        // block(canvas); If I call this here, draws behind children, still no updates
    }

    private void block(Canvas canvas) {
        Blocker.block(canvas, this.getDrawingCache());
    }
}

The problem I'm having is in my viewgroup. If I run the block method in the viewgroup's draw, it draws over everything but doesn't ever update when child views change. I've traced function calls with Log, and the draw method seems to be running, but nothing changes.

I've also tried implementing this in onDraw. This draws the bitmap behind all the children views, and again they aren't updating.

Can anyone explain how I would go about fixing this?


回答1:


Try this:

@Override
protected void dispatchDraw(Canvas canvas) {
    // call block() here if you want to draw behind children
    super.dispatchDraw(canvas);
    // call block() here if you want to draw over children
}

And call destroyDrawingCache() and then, buildDrawingCache() each time you change a child.




回答2:


Draw() method will work well for you.

I'm now trying to make a count time view in a circle shape, when time is passing, the view will reducing his angle. It's used to cover profile photo(a circle shape photo).



来源:https://stackoverflow.com/questions/13346589/drawing-over-a-view-and-all-its-children

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!