【问题标题】:setImageBitmap in gridview does not scale the image to fitgridview 中的 setImageBitmap 不会缩放图像以适应
【发布时间】:2015-03-12 02:44:11
【问题描述】:

注意:这个问题只发生在gridview中,如果在单个imageview中设置imagebitmap,效果很好。

看下图,左图是我想要的,右图是我实际得到的。

唯一的区别是我设置图像的方式,如果我设置图像如下,我得到正确的结果(上左图)。

holder.image.setImageResource(imagesArray.get(position));

但是如果我将图像设置为如下所示的位图,则图像不会按我想要的方式缩放(上图右图)

holder.imageView.setImageBitmap(bitmap);

为什么我将图像设置为位图而不是可绘制的? 因为我的图像是加密的,所以我需要在加载到 imageview 之前对其进行解密。基本步骤是:

  1. 将图像解密为字节数组。
  2. 从字节创建位图。
  3. 将位图加载到 imageview 中。

注意,这个问题不是解密引起的,我用正常的图片没有解密测试过,也不行。

这里是代码

GridViewActivity

public class GridViewActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gridview);

        GridView gridView = (GridView)findViewById(R.id.grid_view);
        ImageAdapter imageAdapter = ImageAdapter.getInstance();
        imageAdapter.setContext(this);
        gridView.setAdapter(imageAdapter);

        for (int i = 0; i < 50; i++) {
            int resourceId = getResources().getIdentifier("myimage", "drawable", getPackageName());
            imageAdapter.imagesArray.add(resourceId);
        }
    }
}

图像适配器

public class ImageAdapter extends BaseAdapter {
    private static ImageAdapter imageAdapter = null;
    private Bitmap bitmap;
    private Context mContext;
    public ArrayList<Integer> imagesArray = new ArrayList<Integer>();

    private ImageAdapter() {
    }

    public static ImageAdapter getInstance() {
        if (imageAdapter == null) {
            imageAdapter = new ImageAdapter();
        }

        return imageAdapter;
    }

    public void setContext(Context context) {
        mContext = context;
    }

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

    @Override
    public Object getItem(int position) {
        return imagesArray.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;

        if (row == null) {
            LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
            row = inflater.inflate(R.layout.row_grid, parent, false);
            holder = new ViewHolder();
            holder.imageView = (ImageView)row.findViewById(R.id.image);
            row.setTag(holder);
        }
        else {
            holder = (ViewHolder)row.getTag();
        }

        // Create bitmap from image in assets folder.
        String imageFileName = "myimage.jpg";
        AssetManager assetMgr = mContext.getAssets();
        try {
            InputStream fis = assetMgr.open(imageFileName);
            bitmap = BitmapFactory.decodeStream(fis);
        }
        catch (Exception ex) {
            Log.i("zdd", ex.getLocalizedMessage());
        }

        holder.imageView.setImageBitmap(bitmap);
        //holder.image.setImageResource(imagesArray.get(position));

        return row;
    }

    static class ViewHolder {
        ImageView imageView;
    }
}

SquareImageView

public class SquareImageView extends ImageView
{
    public SquareImageView(Context context)
    {
        super(context);
    }

    public SquareImageView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public SquareImageView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
    }
}

这是布局

activity_gridview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#7f7f7f"
        android:numColumns="4"
        android:horizontalSpacing="2dp"
        android:verticalSpacing="2dp"
        android:gravity="center"
        android:stretchMode="columnWidth"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">
    </GridView>
</RelativeLayout>

row_grid.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp"
    android:background="#ffffff">
    <com.jiyuzhai.setimagebitmaptest.SquareImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>
</LinearLayout>

【问题讨论】:

    标签: android gridview bitmap imageview


    【解决方案1】:

    尝试将行项目的父大小更改为“match_parent”。

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp"
    android:background="#ffffff">
    <com.jiyuzhai.setimagebitmaptest.SquareImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>
    </LinearLayout>
    

    【讨论】:

    • 谢谢,但为什么 setimageReource 在线性布局中与 layout_width="wrap_content" 一起使用?
    【解决方案2】:

    尝试从线性布局中删除“填充”属性(或将其设置为 0dp)并在 SquareImageView 中将 scaleType 设置为 fitXY。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 2019-03-12
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多