java - 图库 View 中的空白图像 – Android

标签 java android view gallery android-adapter

我知道图库 View 已经过时,但我仍然需要它。

我已经在 Nexus 7、三星 S4、LG 手机上进行了测试,效果很好,但在 HTC 手机上,我在图库中看到了空白,而不是一些图像,如下所示:

enter image description here

这里是一个代码示例:

<Gallery
            android:layout_marginTop="5dp"
            android:id="@+id/gallery1"
            android:layout_width="match_parent"
            android:layout_height="350dp" />

类 ImageAdapter 扩展了 BaseAdapter {

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public Object getItem(int arg0) {

        return null;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup vg) {
        ImageView image = new ImageView(getApplicationContext());
        image.setImageResource(images.get(position));
        image.setPadding(20, 20, 20, 20);
        image.setLayoutParams(new Gallery.LayoutParams(550, 450));
        image.setScaleType(ImageView.ScaleType.FIT_XY);
        return image;
    }
}

有什么问题吗?

最佳答案

我用过article现在它对我有用。

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
    int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);

}

关于java - 图库 View 中的空白图像 – Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21441604/

相关文章:

java - Vertx 因多个文件上传请求而变慢

android - 在 ViewPager 中引用 EditText 会产生空指针异常

Android Java 项目在 Android Studio 中转换为 Kotlin

java - Actionbar Overflow 中的单元测试菜单项

android - setClickable() 不适用于按钮

java - 当我在导航栏中单击某个项目时如何重定向到另一个页面

java - AndEngine GLES 2 - 黑屏,无错误

java - 如何在java中更新zip文件中的文本文件内容

android - 在 Android 上的其他 fragment 发生更改后刷新 fragment 的 View

C++ 高级 3D 坐标到 2D 坐标