[英]getDrawingCache() always returning null
我想使用DrawingCache捕獲ImageView的內容。 我寫了下面的代碼。
iv1 = (ImageView)findViewById(R.id.iv1);
iv2 = (ImageView)findViewById(R.id.iv2);
iv1.setDrawingCacheEnabled(true);
Bitmap myScreenshot = iv1.getDrawingCache();
iv2.setImageBitmap(myScreenshot);
但我在屏幕上只得到一張圖片。 后來我才知道myScreenshot是空的
我看到很多關於同樣問題的帖子,但沒有適當的解決方案。
我認為我們必須在清單中添加任何權限? 實現這一目標需要root權限? 請幫我解決這個問題。
嘗試在buildDrawingCache()
之前調用getDrawingCache()
編輯:在頁面加載后調用getDrawingCache()
,而不是onCreate
在onWindowFocusChanged()中調用getDrawingCache()而不在onCreate()中調用,那么你就不會得到null。
你不能做iv1.buildDrawingCache(true)
; 之前添加行
Bitmap myScreenshot = iv1.getDrawingCache();
imageview.setBackgroundResource(R.drawable.imageview);
imageview1.setBackgroundResource(R.drawable.imageview1);
使用這兩個圖像是screan
<RelativeLayout
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/image"
android:layout_width="320dp"
android:layout_height="486dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="@drawable/iv1" />
<ImageView
android:id="@+id/ic_launcer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/iv2" />
</RelativeLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.