簡體   English   中英

getDrawingCache()返回null

[英]getDrawingCache() returns null

我正在研究一個簡單的繪畫應用程序並嘗試實現在用戶請求時提供更多空間來繪制的功能,我認為可以通過簡單地啟用我的CustomView類(包含在LinearLayout中然后包含在其中)來完成ScrollView類)。 如果我沒有通過運行Chunk 1來調整CustomView類的大小,那么Chunk 2工作正常(它只是保存了一個繪圖畫面。此時,沒有滾動。)真糟糕! 運行Chunk 1時,塊2失敗(view.getDrawingCache()返回null)(此時,啟用滾動)。 我想保存整個視圖,包括由於滾動而遺漏的部分。

塊1:

CustomView view = (CustomView) findViewById(R.id.customViewId);
height = 2 * height;
view.setLayoutParams(new LinearLayout.LayoutParams(width, height));

塊2:

CustomView view = (CustomView) findViewById(R.id.customViewId);
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);

這兩個代碼塊在兩種不同的方法中是獨立的和小的部分。

編輯:已解決

經過數十億次Google查詢后,問題現已解決;)
我提到了這個主題, https://groups.google.com/forum/?fromgroups =#topic / android-Developers / VQM5WmxPilM
我不明白的是getDrawingCache()方法對View類的大小(寬度和高度)有限制。 如果View類太大,getDrawingCache()只返回null。
因此,解決方案不是使用該方法,而是像下面這樣做。

CustomView view = (CustomView) findViewById(R.id.customViewId);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), 
  view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); 
Canvas bitmapHolder = new Canvas(bitmap); 
view.draw(bitmapHolder);
// bitmap now contains the data we need! Do whatever with it!

在能夠使用位圖之前,您需要調用buildDrawingCache()

setDrawingCache(true)事物只是設置標志並等待下一個繪圖過程以創建緩存位圖。

另外,不要忘記在不再需要時調用destroyDrawingCache()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM