【问题标题】:Can't resize image in android without loosing content无法在不丢失内容的情况下在android中调整图像大小
【发布时间】:2017-12-13 16:24:32
【问题描述】:

我有一些 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>

图像适配器:

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

【问题讨论】:

    标签: android android-bitmap


    【解决方案1】:

    好的,我会发布我所做的,它在 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"
                />
    

    【讨论】:

      猜你喜欢
      • 2018-05-20
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多