2011-11-22 58 views
4

所以我有這個位圖我想用作視圖的背景,但是位圖有時可能比視圖大。位圖剪輯?

讓我們說我的位圖是500×500像素 和我的看法是200x200px

當我設置位圖作爲背景的視圖,該視圖獲取的拉伸位圖(500×500)的大小...

我嘗試設置重力clip_horizo​​ntal | clip_vertical但沒有幫助...

<?xml version="1.0" encoding="utf-8"?> 
    <bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
     android:src="@drawable/old_wall" 
     android:tileMode="repeat" 
     android:gravity="top|left|clip_horizontal|clip_vertical" /> 

我怎麼能得到位圖會被切去,而不是視圖被拉伸位圖的大小?

+0

'gravity'被忽略。請參閱[位圖可繪製資源](http://developer.android.com/guide/topics/resources/drawable-resource.html#Bmapmap)。 –

回答

1

您可以隨時在代碼中剪切原始位圖。然後將其設置爲視圖。其中x,y是你的出發點,X_WIDTHY_HEIGHT是你的終點。而iv是你的目標ImageView:當`tileMode`設置

Bitmap bmSource; 
Bitmap bmTarget; 

bmTarget = Bitmap.createBitmap(bmSource, x, y, X_WIDTH, Y_HEIGHT); 

iv.setImageBitmap(bmTarget);