【问题标题】:Size of images in imageview androidimageview android中的图像大小
【发布时间】:2013-09-13 08:57:22
【问题描述】:

我在 listView 中有一个 imageView。像这样设置: main.xml

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

  <ListView
    android:id="@+id/listView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
  </ListView>

</LinearLayout>

图像.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="wrap_content"
  android:orientation="vertical" >

  <ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"    
    android:scaleType="center"    
    android:src="@drawable/ic_launcher" />

</RelativeLayout>

这很好用,用 http 请求从 url 填充许多图像。但是我对图像的大小有疑问。无论分辨率或大小,我都希望它们填满屏幕。尝试了不同的布局高度、宽度和比例类型,但无法正常工作。

第一张是现在的样子,第二张是我想要的样子。

编辑:尝试使用 scaleType="fitXY",它给了我 100% 的宽度,但在较长的图像上的高度很差。

【问题讨论】:

  • 你的问题的任何解决方案???

标签: android android-layout android-imageview


【解决方案1】:

我遇到了同样的问题,除了自己上课之外没有找到其他解决方案。

public class Banner extends View {

private final Drawable mDrawable;

public Banner(Context context) {
    this(context, null);
}

public Banner(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

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

    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Banner, 0, 0);
    try {
        mDrawable = a.getDrawable(R.styleable.Banner_image);
    } finally {
        a.recycle();
    }

    setBackgroundDrawable(mDrawable);
}

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = 0;
    int height = 0;
    if(mDrawable != null) {
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = width * mDrawable.getIntrinsicHeight() / mDrawable.getIntrinsicWidth();
    }
    setMeasuredDimension(width, height);
}

}

然后将属性添加到/values文件夹中的某个文件

<declare-styleable name="Banner">
    <attr name="image" format="reference" />
</declare-styleable>

并像这样使用这个新控件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:banner="http://schemas.android.com/apk/res/your.project.name.here"
    android:layout_width="fill_parent"
    android:layout_ height="wrap_content"
    android:orientation="vertical" >

<your.project.name.here.Banner
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"    
    banner:image="@drawable/ic_launcher" />

</RelativeLayout>

它会根据图像宽度按比例调整高度

【讨论】:

  • 认为这是他们的方式,是的,但不能让它正常工作。我会尝试更多。谢谢:)
【解决方案2】:
// try this
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true" />

【讨论】:

    【解决方案3】:

    使用 NinePatch 图像

    Link Here

    【讨论】:

      【解决方案4】:

      您将其用作:

      <?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="wrap_content"
      android:orientation="vertical" >
      
      <ImageView
          android:id="@+id/imageView1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"    
          android:scaleType="center"    
          android:src="@drawable/ic_launcher" />
      
      </RelativeLayout>
      

      试试这个,我已经改变了一些:

      <?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="wrap_content"
      android:orientation="vertical" >
      
      <ImageView
          android:id="@+id/imageView1"
          android:layout_width="200dp"
          android:layout_height="200dp"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"    
          android:scaleType="FITXY"    
          android:src="@drawable/ic_launcher" />
      
      </RelativeLayout>
      

      设置imageView的固定大小,然后缩放到FITXY。我希望这个问题能得到解决。

      【讨论】:

      • 这给了每张图片相同的高度,我想要全宽和不同的高度,具体取决于图像。对不起,如果我的问题不够清楚。
      【解决方案5】:

      根据您的要求。你应该把你的代码这样,

      <?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="wrap_content"
          android:orientation="vertical" >
      
      <ImageView
          android:id="@+id/imageView1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
      
          android:scaleType="fitXY"    
          android:src="@drawable/ic_launcher" />
      </RelativeLayout>
      

      【讨论】:

        【解决方案6】:

        代替android:src="@drawable/ic_launcher" 使用android:background="@drawable/ic_launcher" 并在java 文件中使用:

        yourImageView.setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap));
        

        如果我们使用android:src,它会根据其分辨率设置图像以适应屏幕。如果我们将图像设置为背景,我们可以设置所需的widthheight

        【讨论】:

          【解决方案7】:

          听起来您想在图像视图上使用android:adjustViewBounds="true"android:scaleType="centerInside"

          此外,除非您使用 image.xml 中的 RelativeLayout 执行问题中未显示的其他操作,否则这是没有意义的。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-07-18
            相关资源
            最近更新 更多