2014-06-11 98 views
0

試圖調整位圖大小並將其設置爲imageview的特定部分。 imageview是方形的,我希望在右下角有位圖。寬度爲imageview的10%,高度爲30%。將位圖設置爲imageview內的特定大小

int w = imageview.getWidth(); 
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.vertical_bar_green); 
imageview.setImageBitmap(Bitmap.createScaledBitmap(bm, w/10, w*30/100, false)); 
imageview.setScaleType(ScaleType.FIT_END); 

結果是位圖是imageview的全高,寬度要大得多。

如何設置特定點以放置位圖?

回答

0

ENDFIT_END使用的矩陣)的文檔。

計算將保持原始src寬高比的比例,但 也將確保src完全符合dst。 至少有一個軸 (X或Y)將完全適合。 END將結果對齊到dst的右邊和 的底邊。

你很可能會想爲此使用自定義矩陣,可能是使用setRectToRect()構建的。

例如:

Matrix matrix = new Matrix(); 
RectF from = new RectF(0, 0, bm.getIntrinsicWidth(), bm.getIntrinsicHeight()); 
RectF to = new RectF(view.getWidth() * 0.9, view.getHeight() * 0.7, view.getWidth(), view.getHeight()); 
matrix.setRectToRect(from, to, Matrix.ScaleToFit.FILL); 

view.setScaleType(ScaleType.MATRIX); 
view.setImageMatrix(matrix); 

(我不知道,如果你想保持原來的比例或沒有,如果你想它,然後用FIT_ENDsetRectToRect())。

+0

工程很棒。謝謝。 – user3528766

相關問題