2016-09-13 15 views
1

私はギャラリーから写真を撮り、別のアクティビティに画像ビューで表示します。写真が表示されている場合を除いて、すべてうまくいきます。アスペクト比を維持した画像ビューと同じサイズで写真を表示し、トリミングしないようにしたい。私は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); 
    } 
} 
} 
+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

それは私がすべてを試したと言った理由です画像はアスペクト比を保持し、それは画像ビューの次元で表示されます)。行列以外についてもっと教えてください。ありがとうございました 。 –

+0

すみません、私はマトリックスが何であるか知りません。画像の元のアスペクト比に合わせるために、画像ビューの1次元のみ*のサイズを変更する必要がありますか?おそらく元のイメージビューの幅と高さを「コンテンツをラップする」ように変更し、画像ビューにアサインする前に画像を縮小(アスペクト比を維持)する場合はどうでしょうか? –

関連する問題