Android的 - 触摸抹去前景的ImageView的部分,以暴露后台查看 [英] Android - Touch to erase portions of foreground ImageView to expose background View

查看:277
本文介绍了Android的 - 触摸抹去前景的ImageView的部分,以暴露后台查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在挣扎这一天的大部分时间。假设我有,我想在叠加的背景下查看自定义的ImageView(既有的RelativeLayout内),其中触摸时,它会删除查看源位像MS画图的擦除工具的一部分,露出它下面的视图。我已经检查多少pretty所有的线程(如<一个href=\"http://stackoverflow.com/questions/5368774/make-certain-area-of-bitmap-transparent-on-touch\">this 之一),他们建议使用PorterDuff SRC模式在画图对象以及创建一个Canvas出了源位图的ARGB_8888卷影副本申请屏蔽。

另外,我不能设置覆盖源提前时间,因为我有这样的ImageView规模型需要照顾的比例为我下载它在网络上。

我每次重写的onDraw,当我申请对四的位图擦除,它公布了一个黑色的背景,而不是它下面的观点,即使我将背景设置为透明。所以,我在我的最后一个绳子,以便揭开背景视图做什么。

下面是我到目前为止有:

查看构造器:

  paint.setXfermode(新PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    paint.setColor(Color.TRANSPARENT);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(STROKE_WIDTH);
    paint.setAntiAlias​​(真);

压倒setImageBitmap从我重新的ConfigEd源位图设为我的画布:

 公共无效setImageBitmap(位图位图){
    super.setImageBitmap(位图);
    可绘制BD = getDrawable();
    如果(BD == NULL){
        返回;
    }    位图fullSizeBitmap =((BitmapDrawable)BD).getBitmap();
    覆盖= fullSizeBitmap.copy(Config.ARGB_8888,真);
    C2 =新的Canvas(叠加);
}

的onDraw方法:

 保护无效的onDraw(帆布油画){
    / *
     通过重新绘制视图的位图,然后再覆盖在它上面我们的道路*覆盖漆通话
     * /
    可绘制BD = getDrawable();
    如果(BD == NULL){
        返回;
    }
    位图fullSizeBitmap =((BitmapDrawable)BD).getBitmap();
    如果(fullSizeBitmap = NULL&放大器;!&安培;!C2 = NULL){
        canvas.drawColor(Color.TRANSPARENT);
        c2.drawBitmap(fullSizeBitmap,0,0,NULL);
        c2.drawPath(路径,油漆);
        canvas.drawBitmap(叠加,0,0,NULL);
    }
}


解决方案

我知道这是一个非常古老的问题,但这里是我做过什么来解决类似的问题我有。也许这可以帮助别人的未来。

从API级别11和高达你必须指定这片code,为你的的ImageView 揭开在背面的图像,而不是一个黑色区域。

 如果(Build.VERSION.SDK_INT&GT; = 11){
    imageview.setLayerType(View.LAYER_TYPE_SOFTWARE,NULL);
}

这帮助我成功地显示背面图像

So I've been struggling with this for a better part of a day. Suppose I have a custom ImageView that I want to overlay over a background View (both within a RelativeLayout), which when touched, it erases portions of the View's source bitmap like an erase tool in MS Paint, exposing the View below it. I've checked pretty much all of the threads (like this one) and they suggest to use PorterDuff SRC Mode in the Paint object as well as creating a Canvas out out the ARGB_8888 shadow copy of the source bitmap to apply the masking.

Also, I can't set the source of the overlay ahead of time since I have to download it over the network so that the ImageView's scale type takes care of the scaling for me.

Every time I override onDraw, when I apply the erase on the IV's Bitmap, it unveils a black background instead of the view below it, even though I set the background to transparent. So I'm at my last rope as to what to do in order to unveil the background view.

Here's what I have so far:

view constructor:

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));  
    paint.setColor(Color.TRANSPARENT);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(STROKE_WIDTH);
    paint.setAntiAlias(true);

overrode setImageBitmap to set my canvas from my re-configed source bitmap:

public void setImageBitmap(Bitmap bitmap){
    super.setImageBitmap(bitmap);
    Drawable bd = getDrawable();
    if(bd == null){
        return;
    }

    Bitmap fullSizeBitmap = ((BitmapDrawable) bd).getBitmap();
    overlay = fullSizeBitmap.copy(Config.ARGB_8888, true);
    c2 = new Canvas(overlay);
}

onDraw method:

protected void onDraw(Canvas canvas) {
    /*
     * Override paint call by re-drawing the view's Bitmap first, then overlaying our path on top of it
     */
    Drawable bd = getDrawable();
    if(bd == null){
        return;
    }
    Bitmap fullSizeBitmap = ((BitmapDrawable) bd).getBitmap();
    if(fullSizeBitmap != null && c2 != null){
        canvas.drawColor(Color.TRANSPARENT);
        c2.drawBitmap(fullSizeBitmap, 0, 0, null);
        c2.drawPath(path, paint);
        canvas.drawBitmap(overlay, 0, 0, null);
    }
}

解决方案

I know this is a really old question but here is what I did to fix a similar problem I had. Maybe it helps someone in the future.

From API level 11 and up you have to specify this piece of code for your ImageView to unveil the image in the back and not a black area.

if (Build.VERSION.SDK_INT >= 11) {
    imageview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

This helped me displaying the back image successfully

这篇关于Android的 - 触摸抹去前景的ImageView的部分,以暴露后台查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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