2012-12-29 60 views
2

好了,所以我試圖找出了這一點,怎麼不知道如何下方繼續 是滾動型看起來背景設置爲@繪製/ patterrepeat挑戰對於Android的ImageButton的背景

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="false" 
    android:background="@drawable/patternrepeat"> 
</ScrollView> 

現在patternrepeat是xml drawable看起來像下面這樣

<?xml version="1.0" encoding="utf-8"?> 
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/pattern" 
    android:tileMode="repeat"/> 

現在這個位圖的src是一個PNG圖像。

的挑戰是如何訪問此「編程」滾動型---->位圖----->位圖原始PNG資源

這樣做的原因是,原來的來源平鋪創建位圖然後將其加載到滾動型創建的背景...

如何將一個與該繼續嗎?

+1

這將工作。沒有問題。你有沒有得到任何錯誤? –

+0

哦,我可能需要一提的,我想是因爲我希望能夠改變它在用戶偏好 –

回答

1
// get bg drawable first 
Drawable d = scrollView.getBackground(); 

// there are many types of drawables. In you case BitmapDrawable 
// (according to your xml) should be returned. So... 
if (d instanceof BitmapDrawable) { // check drawable type just in case 
    Bitmap sourceBitmap = ((BitmapDrawable)d).getBitmap(); 
} 

sourceBitmap是你需要的。

+0

飛行基地,這是非常接近,讓我什麼,我想這個問題是我不知道如何以編程方式做到這一點我得到它後更改src?任何想法如何?我越來越從PNG在src在我的文件夾繪製敬畏 –

+0

沒關係位圖廠... BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_resource); 一千upvotes你順便說一句感謝 –

+0

歡迎您) – Leonidos