【问题标题】:How to delete Additional space ImageView in Android如何在Android中删除额外的空间ImageView
【发布时间】:2015-12-07 08:19:48
【问题描述】:

我想在我的项目中使用ImageView,但它在布局中显示了额外的空间。如何删除额外的空间?

这张图显示了问题:

XML 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.tellfa.smsbox.activities.Dialog_image_show">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:src="@drawable/cover_menu_bg" />

    </RelativeLayout>

</RelativeLayout>

我不想使用android:scaleType="centerCrop"

【问题讨论】:

  • 使用android:scaleType="fitXY"
  • @Aashvi, tnx 但我不想使用scaleType 方法
  • @Stankovitch,感谢我亲爱的朋友。我用这个代码修复它:android:scaleType="fitStart" android:adjustViewBounds="true"
  • 只是剪切和缩小图像的透明部分。

标签: android android-layout


【解决方案1】:

试试这个自定义的 ImageView 类

public class ResizableImageView extends ImageView {

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        Drawable d = getDrawable();

        if(d!=null){
            // ceil not round - avoid thin vertical gaps along the left/right edges
            int width = View.MeasureSpec.getSize(widthMeasureSpec);
            int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
            setMeasuredDimension(width, height);
        }else{
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

}

并像下面这样使用这个类

<your_package.ResizableImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:src="@drawable/cover_menu_bg" />

【讨论】:

  • 感谢我亲爱的朋友的帮助
  • 工作,但是两个文本视图不适用于大图像
【解决方案2】:

像这样使用图像视图

<ImageView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:adjustViewBounds="true"
   android:src="@drawable/icon"
   android:id="@+id/imv"
   android:scaleType="fitCenter" />

【讨论】:

  • 感谢我亲爱的朋友的帮助
  • @Dre.Dre666 wc dost :)
【解决方案3】:

适用于寻求两种方向的解决方案的任何人。这是我的示例方法:

首先,我的xml:

<?xml version="1.0" encoding="utf-8"?>
<com.example.michal.fillimageview.CustomLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="#f0f000"
    android:orientation="vertical">

    <com.example.michal.fillimageview.ResizableImageView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:src="@drawable/p600_900" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/background_dark"
        android:text="Hello World!"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="30sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/background_dark"
        android:text="Hello World!"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="30sp" />

</com.example.michal.fillimageview.CustomLinearLayout>

类:

public class ResizableImageView extends ImageView {

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Drawable d = getDrawable();

        if (d != null) {
            // ceil not round - avoid thin vertical gaps along the left/right edges
            int width = View.MeasureSpec.getSize(widthMeasureSpec);
            int height = View.MeasureSpec.getSize(heightMeasureSpec);

            if (height > width) {//portrait
                if (d.getIntrinsicWidth() > d.getIntrinsicHeight()) {
                    height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
                    setMeasuredDimension(width, height);
                } else {
                    width = (int) Math.ceil((float) height * (float) d.getIntrinsicWidth() / (float) d.getIntrinsicHeight());
                    setMeasuredDimension(width, height);
                }
            } else {//landscape
                width = (int) Math.ceil((float) height * (float) d.getIntrinsicWidth() / (float) d.getIntrinsicHeight());
                setMeasuredDimension(width, height);
            }

        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}

特定图像的示例:

  • 600x900 纵向

  • 600x900 横向

  • 800x600 纵向

  • 800x600 横向

【讨论】:

    猜你喜欢
    • 2016-11-23
    • 2020-09-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 2016-07-24
    相关资源
    最近更新 更多