2017-01-07 8 views
0

私はimageviewを持っています。 私はimageView.setScaleX()を使用しましたが、この方法は画像内の画像のみを拡大し、画像ビューX、Y、幅、高さは同じです。 画像でimageviewのサイズを変更するにはどうすればよいですか?AndroidスケールImageViewのフレーム

see this figure

+0

画像ビューのいくつかのプロパティを既に確認してください。 – Saveen

+0

なぜscaleXを変更しないのですか? – Saeid

+0

android:scaleType = "center"を設定します。 –

答えて

0

T @saeidからのハンクス。私はsetScaleをsetWidthに置き換えて、私の問題を解決しました。

0

幅を変更したときに高さを自動的に変更したい場合は、このカスタムImageViewを使用してください。

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.ImageView; 

import java.lang.reflect.Field; 

public class AspectRatioImageView extends ImageView { 

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

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

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

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     try { 
      int width = MeasureSpec.getSize(widthMeasureSpec); 
      int height = width * getDrawable().getIntrinsicHeight() 
        /getDrawable().getIntrinsicWidth(); 
      int maxHeight; 
      try { 
       Field f = ImageView.class.getDeclaredField("mMaxHeight"); 
       f.setAccessible(true); 
       maxHeight = (Integer) f.get(this); 
      } catch (NoSuchFieldException e) { 
       maxHeight = Integer.MAX_VALUE; 
      } 
      setMeasuredDimension(width, height > maxHeight ? maxHeight : height); 
     } catch (Exception e) { 
      super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     } 
    } 
}