2016-09-13 68 views
1

我从图库中拍摄照片,并将其显示在图像视图中的其他活动中。除了显示照片时,一切都很好。我希望照片以与图像视图完全相同的尺寸显示,以保持纵横比并且不会被裁剪。我尝试了所有的xml(centerCrop,fitxy ...)这是我正在尝试,但它不工作。缩放图像,保持纵横比不被裁剪 - android应用程序

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); 
    } 
} 
} 
+0

您可以简单地应用scaleType:“fitCenter”我认为在您的ImageView中 –

+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的本身......这是不同的尺寸,所以我很抱歉,如果我错过标记。

+0

这就是为什么我说我尝试了一切(centerCrop是最好的,图像保持宽高比,它显示在图像视图的尺寸中)除了矩阵。可以告诉我更多的矩阵吗?谢谢 。 –

+0

对不起,我不知道矩阵是做什么的。所以你想调整图像视图的尺寸*以适应图像的原始高宽比,对吧?也许如果您将原始的imageview宽度和高度更改为“包装内容”,然后在将它们分配给imageview之前缩小图像(保持宽高比)? –