缩放图像,保持纵横比而不被裁剪-android app

时间:2016-09-13 02:12:05

标签: android android-bitmap

我从图库中拍摄照片,并将其显示在图像视图中的其他活动上。一切都很好,除非显示照片。我希望照片以与图像视图完全相同的尺寸显示,保持纵横比并且不被裁剪。我尝试了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);
    }

file.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);
    }
}
}

1 个答案:

答案 0 :(得分:0)

您是否了解可用属性“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]

看起来你实际上可能正在尝试改变imageview本身的尺寸......这是不同的,所以如果我错过了标记,我会道歉。