【问题标题】:how to scale image width for device width?如何为设备宽度缩放图像宽度?
【发布时间】:2017-10-15 03:25:54
【问题描述】:

我在图库中获取图像,但某些图像宽度不适合设备宽度,因此如何在我的ImageView 中将图像宽度设置为设备宽度,但原始图像高度的高度相同。

下面是我设置的图片代码

Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
// Get the path from the Uri
    String path = getPathFromURI(selectedImageUri);
    Log.i("", "Image Path : " + path);
     // Set the image in ImageView
      try {
       bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
           } catch (IOException e) {
               e.printStackTrace();
           }
           imgEdit.setImageURI(selectedImageUri);
        }

【问题讨论】:

  • 图像或图像视图??您想在 imageView 中显示宽度为设备宽度的图像>?

标签: android image image-processing


【解决方案1】:

使用这个,请根据需要更改纵横比

 Display display = ((NavigationHome) context).getWindowManager().getDefaultDisplay();
    int width = display.getWidth(); // deprecated
    int height = display.getHeight();
    int sliderHeight = (int) ((width) / 2.4);
        if (height != 0)
            imageView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,sliderHeight));

【讨论】:

  • 尝试此代码,但宽度合适,包括显示宽度获取和设置但无法正常工作..谢谢时间...
【解决方案2】:

尝试将高度设置为match_parent

类似

<ImageView
     android:layout_width="match_parent"
     android:layout_height="50dp"/>

【讨论】:

  • 为什么要把原来的高度改成50dp?
  • 这只是一个例子
【解决方案3】:

试试这个

imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT));

或者如果你想填充宽度

imageView.setLayoutParams(new LayoutParams((LayoutParams.FILL_PARENT,LayoutParams.MATCH_PARENT));

【讨论】:

    【解决方案4】:

    你必须扩展 ImageView,这将匹配_parent 宽度和比例高度:

    package com.yourpackage.ui;
    
    import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    import android.widget.ImageView;
    
    public class DynamicImageView extends ImageView {
    
    public DynamicImageView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }
    
    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
        final Drawable d = this.getDrawable();
    
        if (d != null) {
            final int width = MeasureSpec.getSize(widthMeasureSpec);
            final int height = (int) Math.ceil(width * (float) d.getIntrinsicHeight() / d.getIntrinsicWidth());
            this.setMeasuredDimension(width, height);
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    }
    

    然后在你的 XML 布局中:

    <com.yourpackage.ui.DynamicImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"/>
    

    【讨论】:

      【解决方案5】:

      用它改变你的图像视图。

       <ImageView
              android:id="@+id/background"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:adjustViewBounds="true"
              android:scaleType="fitXY"
      
              />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-20
        • 1970-01-01
        • 2019-02-25
        • 2023-04-03
        • 2011-04-09
        • 2012-08-04
        • 2020-08-18
        相关资源
        最近更新 更多