从URL下载图像并在画布上绘制 - Android

时间:2016-08-15 14:20:59

标签: android canvas android-canvas

我正在尝试使用Retrofit库从服务器下载图像。服务器返回图像的字节数组,然后使用BitmapFactory.decodeByteArray(..)将其转换为位图。但是当我调用canvas.drawBitmap(..)时,我的应用程序崩溃,并且后续消息显示在LogCat

Fatal signal 11 (SIGSEGV), code 1, fault addr 0x188 android

在stackoverflow上搜索后,我遇到了一个答案,其中说android:hardwareAccelerated设置为false。我做了,不仅我的应用程序变得迟缓和缓慢,图像仍然没有画在画布上。我知道left中传递的topcanvas.drawBitmap(....)坐标是正确的,因为我可以显示从mipmap加载的图像。

任何有关这方面的帮助都会受到赞赏,因为我暂时停留在这个问题上并且无法找到合适的解决方案。

修改

崩溃时的LogCat

                         ----Beginning of crash
A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x188 in tid 22133
W/System: ClassLoader referenced unknown path: /data/app/com.i2c.bb4me-2/lib/x86_64
I/MultiDex: VM with version 2.1.0 has multidex support
I/MultiDex: install
I/MultiDex: VM has multidex support, MultiDex support library is disabled.
I/GMPM: App measurement is starting up, version: 8487
I/GMPM: To enable debug logging run: adb shell setprop log.tag.GMPM VERBOSE
I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable

调用提取和绘制图像的方法

我在计算左侧和顶部之后在onDraw()中调用此方法,这正如我上面所说的那样正确。

private void getBitmap(final Canvas canvas, long imageID, final float left, final float top, final Paint paint) {
        ProfileService profileServiceRetrieveImage = AppManager.getServiceManager().getService(ProfileService.class);
        Call<ServerResponse<FetchProfileImageResponse>> call = profileServiceRetrieveImage.fetchProfileImage("retrieveImage.action", "", imageID);
        call.enqueue(new RetrofitCallback<ServerResponse<FetchProfileImageResponse>>() {
            @Override
            protected void onSuccess(ServerResponse<FetchProfileImageResponse> objectServerResponse) {
                FetchProfileImageResponse dao = objectServerResponse.getResponsePayload();
                Bitmap bmp = BitmapFactory.decodeByteArray(dao.getBinaryData(), 0, dao.getBinaryData().length);
                canvas.drawBitmap(bmp, left, top, paint);
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

发布解决方案以防万一有人像我一样坚持这个问题。

首先,请勿在{{1​​}}中致电canvas.drawBitmap(....)。只需将onSuccess中检索到的位图保存到班级中,拨打onSuccess并拨打invalidate()中的canvas.drawBitmap(...)