绘制视图及其所有子项 [英] Drawing over a view and all it's children

查看:98
本文介绍了绘制视图及其所有子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将视觉效果应用于视图组.我的想法是获取视图组的位图,将其缩小,再向上扩展,然后在视图组上绘制,以提供块状,低质量的效果.

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());
    }
}

我遇到的问题在我的视图组中.如果我在视图组的绘图中运行block方法,它将覆盖所有内容,但在子视图更改时永远不会更新.我已经用Log跟踪了函数调用,并且draw方法似乎正在运行,但是没有任何变化.

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.

我也尝试过在onDraw中实现它.这会将位图绘制在所有子视图的后面,并且它们也不会更新.

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?

推荐答案

尝试一下:

@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
}

并在每次更改子对象时调用destroyDrawingCache(),然后调用buildDrawingCache().

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

这篇关于绘制视图及其所有子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆