2015-04-27 48 views
0

我有一個ImageView在XML中定義如下。它位於自定義ViewPager中。我如何知道縮放圖像的大小?

<ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/imageView" 
     android:scaleType="fitXY"/> 

它正在像一個魅力,但我需要知道圖像的實際高度後,已被縮放。這是我在值自定義ViewPager的onMeasure方法,像這樣:

View child = getChildAt(0); 

child.measure(widthMeasureSpec, MeasureSpec 
    .makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 

int h = child.getMeasuredHeight(); 

我得到的高度不縮放圖像的高度,它的前縮放後的圖像的原始大小。

謝謝。

回答

1

您重寫onMeasure函數以獲取圖像矩陣並查找比例值。像這樣

float[] f = new float[9]; 
getImageMatrix().getValues(f); 
final float scaleX = f[Matrix.MSCALE_X]; 
final float scaleY = f[Matrix.MSCALE_Y]; 

// Get the drawable's real width and height 
final Drawable d = imageView.getDrawable(); 
final int origW = d.getIntrinsicWidth(); 
final int origH = d.getIntrinsicHeight(); 

// Calculate the actual dimensions 
final int actW = Math.round(origW * scaleX); 
final int actH = Math.round(origH * scaleY); 
log.v("actual dimensions",""+actW+actH); 

來源:https://stackoverflow.com/a/15538856/4743812

相關問題