图库视图中的空白图像 - Android

时间:2014-01-29 20:11:36

标签: java android view gallery android-adapter

我知道画廊视图已经过时,但我仍然需要它。

我在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;
    }
}

它出了什么问题?

1 个答案:

答案 0 :(得分:0)

我使用了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);

}

相关问题