android drawBitmap与PorterDuff.Mode.DST_IN,应该是透明的区域变成黑色

时间:2015-08-21 09:39:00

标签: android canvas bitmap draw

我试图用特定形状的面具绘制图像。 我正在使用Facebook的Fresco库,这里是代码:

    ImageRequest request = builder.setPostprocessor(new BasePostprocessor() {
        @Override
        public String getName() {
            return "ChatViewImageProcessor";
        }

        @Override
        public void process(Bitmap bitmap) {
            int bitmap_width = bitmap.getWidth();
            int bitmap_height = bitmap.getHeight();
            ViewGroup.LayoutParams params = iv.getLayoutParams();
            if (width == -1 && height == -1) {//not relavant with question
                horizontal[0] = bitmap_width >= bitmap_height;
                params.width = horizontal[0] ? max_image_size : (int) (max_image_size * 1.0 * bitmap_width / bitmap_height);
                params.height = (horizontal[0] ? (int) (max_image_size * 1.0 * bitmap_height / bitmap_width) : max_image_size);

            }

            Bitmap temp = Bitmap.createBitmap(params.width, params.height, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(temp);
            if (bg != null) {
                bg.setBounds(0, 0, params.width, params.height);
                bg.draw(canvas);
            }
            Logger.out("temp[0,0]:" + Util.getColorNote(temp.getPixel(0, 0), true));
            canvas = new Canvas(bitmap);

            Rect src = new Rect(0, 0, temp.getWidth(), temp.getHeight());
            Rect dst = new Rect(0, 0, bitmap_width, bitmap_height);
            canvas.drawBitmap(temp, src, dst, paint);//<----------problem here

            temp.recycle();
            Logger.out("result[0,0]:" + Util.getColorNote(bitmap.getPixel(0, 0), true));
        }
    }).build();

    PipelineDraweeController controller = (PipelineDraweeController)
            Fresco.newDraweeControllerBuilder()
                    .setImageRequest(request)
                    .setOldController(iv.getController())
                    .build();
    iv.setController(controller);

此代码在5.0.2上运行正常... 我无法在此处发布图片,请参阅http://i.stack.imgur.com/9DBbj.jpg

但4.4.4和4.1.2失败。 我无法在此处发布图片,请参阅http://i.stack.imgur.com/8wbLp.jpg

我还打印了左上角像素&#34; temp&#34;的颜色。并修剪了#34;位图&#34;,在5.0.2上,即#00000000和#00000000,在4.4.4和4.1.2上它们是#00000000和#FF000000

1 个答案:

答案 0 :(得分:1)

我很高兴在Fresco的github问题页面上找到答案here

正如它所说,我只需要打电话

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
        at android.widget.ListView.dispatchDraw(ListView.java:3329)
        at android.view.View.draw(View.java:16181)
        at android.widget.AbsListView.draw(AbsListView.java:4142)
        at android.view.View.updateDisplayListIfDirty(View.java:15174)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
        at android.view.View.updateDisplayListIfDirty(View.java:15134)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
        at android.view.View.updateDisplayListIfDirty(View.java:15134)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
        at android.view.View.updateDisplayListIfDirty(View.java:15134)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
        at android.view.View.updateDisplayListIfDirty(View.java:15134)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
        at android.view.View.updateDisplayListIfDirty(View.java:15134)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
        at android.view.View.updateDisplayListIfDirty(View.java:15134)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2615)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2434)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
        at android.view.Choreographer.doCallbacks(Choreographer.java:670)
        at android.view.Choreographer.doFrame(Choreographer.java:606)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

    bitmap.setHasAlpha(true);