2013-08-01 6 views
1

가로 세로 비율을 유지하면서 높이에 맞추고 싶습니다. 여기서, 이미지는 다음 ImageView 높이 유지 가로 세로 비율

enter image description here

난 적색 대역의 종횡비 (증가한다 이미지 뷰의 폭)을 유지 그레이 행의 높이에 맞는 원하는. XML에서 scaleType 특성을 시도했지만 원하는대로 작동하지 않습니다.

어떤 제안을 해주시겠습니까?

+0

검색해 본 적이 있습니까? – deadfish

+0

질문에 대답하는 것은 쓸모가 없다고 생각합니다. 그것을 해결하는 방법을 안다면 나를 가르쳐주세요. –

답변

2

당신은 사용자 정의 이미지 뷰의 코드를 변경 할 수 있어야한다 :

  • 필요한 이미지 뷰
  • 작물 높이를 오버 플로우

코드의 너비 비율을

  • 뻗어 이미지를 유지 :

    public class ImageViewScaleTypeTopCrop extends ImageView { 
        public ImageViewScaleTypeTopCrop(Context context) { 
         super(context); 
         setup(); 
        } 
    
        public ImageViewScaleTypeTopCrop(Context context, AttributeSet attrs) { 
         super(context, attrs); 
         setup(); 
        } 
    
        public ImageViewScaleTypeTopCrop(Context context, AttributeSet attrs, int defStyle) { 
         super(context, attrs, defStyle); 
         setup(); 
        } 
    
        private void setup() { 
         setScaleType(ScaleType.MATRIX); 
        } 
    
        @Override 
        protected boolean setFrame(int frameLeft, int frameTop, int frameRight, int frameBottom) { 
    
         float frameWidth = frameRight - frameLeft; 
         float frameHeight = frameBottom - frameTop; 
    
         if (getDrawable() != null) { 
    
          Matrix matrix = getImageMatrix(); 
          float scaleFactor, scaleFactorWidth, scaleFactorHeight; 
    
          scaleFactorWidth = (float) frameWidth/(float) getDrawable().getIntrinsicWidth(); 
          scaleFactorHeight = (float) frameHeight/(float) getDrawable().getIntrinsicHeight(); 
    
          if (scaleFactorHeight > scaleFactorWidth) { 
           scaleFactor = scaleFactorHeight; 
          } else { 
           scaleFactor = scaleFactorWidth; 
          } 
    
          matrix.setScale(scaleFactor, scaleFactor, 0, 0); 
          setImageMatrix(matrix); 
         } 
    
         return super.setFrame(frameLeft, frameTop, frameRight, frameBottom); 
        } 
    
    } 
    
  • 관련 문제