【问题标题】:Scale an image ,keeping aspect ratio without being cropped-android app缩放图像,保持纵横比而不被裁剪-android应用
【发布时间】:2016-09-17 04:15:34
【问题描述】:

我从图库中拍摄照片并将其显示在图像视图中的另一个活动中。一切都很好,除了显示照片时。我希望照片以完全相同的尺寸显示,就像图像视图保持纵横比并且不被裁剪。我尝试了 xml(centerCrop,fitxy...) 中的所有内容,这是我正在尝试的,但它不起作用。

showPhotoActivity.class:

       ImageView showPhoto = (ImageView) findViewById(R.id.image);
       Bundle extras = getIntent().getExtras();
       Uri uri = Uri.parse(extras.getString("imagePath"));

    if (showPhoto != null) {
        showPhoto.setAdjustViewBounds(true);
        showPhoto.setImageURI(uri);
    }

文件.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="match_parent"

tools:context="com.example.ga.photoeditor.ShowPhotoActivity"
android:background="#93212121">


<com.example.ga.photoeditor.ScaleImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="320dp"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

ScaleImageView.java

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

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

public ScaleImageView(Context context) {
    super(context);
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    // Call super() so that resolveUri() is called.
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    // If there's no drawable we can just use the result from super.
    if (getDrawable() == null)
        return;

    final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
    final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);

    int w = getDrawable().getIntrinsicWidth();
    int h = getDrawable().getIntrinsicHeight();
    if (w <= 0)
        w = 1;
    if (h <= 0)
        h = 1;

    // Desired aspect ratio of the view's contents (not including padding)
    float desiredAspect = (float) w / (float) h;

    // We are allowed to change the view's width
    boolean resizeWidth = widthSpecMode != MeasureSpec.EXACTLY;

    // We are allowed to change the view's height
    boolean resizeHeight = heightSpecMode != MeasureSpec.EXACTLY;

    int pleft = getPaddingLeft();
    int pright = getPaddingRight();
    int ptop = getPaddingTop();
    int pbottom = getPaddingBottom();

    // Get the sizes that ImageView decided on.
    int widthSize = getMeasuredWidth();
    int heightSize = getMeasuredHeight();

    if (resizeWidth && !resizeHeight)
    {
        // Resize the width to the height, maintaining aspect ratio.
        int newWidth = (int) (desiredAspect * (heightSize - ptop - pbottom)) + pleft + pright;
        setMeasuredDimension(newWidth, heightSize);
    }
    else if (resizeHeight && !resizeWidth)
    {
        int newHeight = (int) ((widthSize - pleft - pright) / desiredAspect) + ptop + pbottom;
        setMeasuredDimension(widthSize, newHeight);
    }
}
}

【问题讨论】:

  • 您可以简单地应用 scaleType:"fitCenter" 我认为在您的 ImageView 中
  • 它不起作用,我试过了:/但是,还是谢谢你:)

标签: android android-bitmap


【解决方案1】:

您知道可用的属性“scaleType”吗?

<com.example.ga.photoeditor.ScaleImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="320dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

插入实验:

android:scaleType= [center, centerCrop, centerInside, fitCenter, fitEnd, fitStart, fitXY, or matrix]

看起来您实际上可能正在尝试更改图像视图本身的尺寸...这是不同的,所以如果我错过了标记,我深表歉意。

【讨论】:

  • 这就是为什么我说我尝试了所有方法(centerCrop 是最好的,图像保持纵横比并显示在图像视图的尺寸中)除了矩阵。你能告诉我更多关于矩阵的信息吗?谢谢。
  • 对不起,我不知道矩阵的作用。因此,您希望仅调整图像视图的一个维度 以适应图像的原始纵横比,对吧?也许如果您将原始图像视图的宽度和高度更改为“包装内容”,然后在将图像分配给图像视图之前缩小图像(保持纵横比)?
猜你喜欢
  • 1970-01-01
  • 2018-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-14
  • 2017-11-04
  • 2013-03-04
  • 1970-01-01
相关资源
最近更新 更多