无法在不丢失内容的情况下调整android中的图像大小

时间:2017-12-12 15:56:57

标签: android android-bitmap

我有一些尺寸为400x550(wxh)的图像。我想将它们调整为200 x 300,以便在两列的网格视图中显示它们。但是图像完全不适合网格的ImageView。

例如我有this图片

将其调整为200 x 300后,它看起来像网格中的this

正如您所看到的,内容完全不适合图像。

这是我的代码片段。

frame_layout_gallery.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageViewGallery"
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:scaleType="centerCrop"/>

</FrameLayout>

ImageAdapter:

public View getView(int position, View convertView, ViewGroup parent) {
    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {

        grid = inflater.inflate(R.layout.frame_layout_gallery, null);
    } else {
        grid = (View) convertView;
    }

    ImageView imageView = (ImageView) grid.findViewById(R.id.imageViewGallery);
    Bitmap mBitmap = BitmapFactory.decodeResource(mContext.getResources(), imageSource[position]);
    Bitmap imageBitmap = Bitmap.createScaledBitmap(mBitmap, 200, 300, false);
    imageView.setImageBitmap(imageBitmap);
    return grid;
}

我已经尝试过缩放图像的其他选项,但它们似乎都没有工作。

public static Bitmap scaleDown(Bitmap realImage, float maxWidth, float maxHeight,
                               boolean filter) {
    float wRatio = (float) maxWidth / realImage.getWidth();
    float hRatio =  (float) maxHeight / realImage.getHeight();
    int width = Math.round((float) wRatio * realImage.getWidth());
    int height = Math.round((float) hRatio * realImage.getHeight());

    Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,
            height, filter);
    return newBitmap;
}

1 个答案:

答案 0 :(得分:0)

好的,我会发布我所做的事情,它在gridView中发布并制作图像列表,但您可以轻松地对其进行调整。

<强> PhotoScaleAdapter:

    public class PhotoScaleAdapter extends BaseAdapter {

    static class ViewHolder {
        ImageView image;
    }


    List<MediaFile> mListMedia;
    Context mContext;
    LayoutInflater mInflater;

    public PhotoScaleAdapter(Context context, List<MediaFile> mediaFiles){
        mContext = context;
        mListMedia = mediaFiles;
        mInflater =  LayoutInflater.from(mContext);
    }


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

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

    @Override
    public long getItemId(int position) {
        return mListMedia.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        View view = convertView;
        ViewHolder holder = null;

        if (view == null) {
            view = mInflater.inflate(R.layout.scale_image, parent, false);
            holder = new ViewHolder();
            holder.image = (ImageView) view.findViewById(R.id.image);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }


        String path = mListMedia.get(position).getPath();

        Bitmap bitmap;
        bitmap = BitmapHelper.resizeBitmap(path, 400, 1);

        holder.image.setImageBitmap(bitmap);
        holder.image.setScaleType(ImageView.ScaleType.FIT_XY);
        holder.image.setAdjustViewBounds(true);

        if(mListMedia.get(position).isSelected()){
            LinearLayout linearLayout = (LinearLayout) holder.image.getParent();
            linearLayout.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
        }
        else{
            LinearLayout linearLayout = (LinearLayout) holder.image.getParent();
            linearLayout.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
        }


        return view;

    }
}

<强> scale_image.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="5dp"
             >

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

,在最终活动/片段中...图片在gridView中:

 <GridView
            android:id="@+id/grid_saisie_photo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="2"
            android:verticalSpacing="5dp"
            android:horizontalSpacing="5dp"
            android:stretchMode="columnWidth"
            android:choiceMode="singleChoice"
            android:drawSelectorOnTop="true"
            android:clickable="true"
            />