【问题标题】:android - bitmap as a background in item in ListViewandroid - 位图作为 ListView 中项目的背景
【发布时间】:2015-08-01 13:26:41
【问题描述】:

我加载位图,然后以这种方式将其设置为我的 ListView 适配器中的项目的背景:

currentView.setBackground(new BitmapDrawable(getContext().getResources(), sImgArray[position]));

位图之前从服务器加载到数组中。在 20 或 21 之前的 API 上一切正常,但是当更高时,不显示位图。如何解决?

我也尝试了毕加索的这段代码,希望它能解决我的问题,但也没有。而且我不喜欢它,因为我需要同时显示位图。

Picasso.with(getContext())
            .load(URL)
            .into(new Target() {
                @Override
                @TargetApi(16)
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    int sdk = android.os.Build.VERSION.SDK_INT;
                    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                        currentView.setBackgroundDrawable(new BitmapDrawable(bitmap));
                    } else {
                        currentView.setBackground(new BitmapDrawable(getContext().getResources(), bitmap));
                    }
                }
            });

【问题讨论】:

    标签: android listview bitmap android-arrayadapter


    【解决方案1】:

    你可以这样做吗?

    int sdk = android.os.Build.VERSION.SDK_INT;
        if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
          Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, Width_of_currentView,  Height_of_currentView, false);
          currentView.setBackgroundDrawable(new BitmapDrawable(bitmap));
         } else {
          Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, Width_of_currentView,  Height_of_currentView, false);
          currentView.setBackground(new BitmapDrawable(getContext().getResources(), resizedBitmap));
        }
    

    希望对你有帮助。

    【讨论】:

    • 我在使用 Width_of_currentView 和 Height_of_currentView 时遇到了一些问题。我使用了 currentView.getWidth 和 currentView.getHeight() 但出了点问题,日志说它们应该> 0。那么如何获取Width_of_currentView和Height_of_currentView呢?
    • @JustinMcGuire,你可以通过 int height=yourView.getLayoutParams().height 获取宽度和高度; int width=yourView.getLayoutParams().width;
    • 不幸的是,我的模拟器上的灰色背景相同:(
    • @JustinMcGuire,我做了什么,我已经根据你的视图的高度和宽度缩放了你的位图,所以在这种情况下,位图是主要问题,你能仔细检查你的主位图吗?
    • 我使用了你发布的代码,用 currentView.getLayoutParams().width/height 获得了宽度和高度(实际上,图片出了点问题),但在我的 Nexus 5 模拟器上一切都是一样的
    猜你喜欢
    • 1970-01-01
    • 2014-06-20
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多