java - View的渲染完畢事件是什么?
問題描述
問題描述現(xiàn)在有這么一個簡單的Fragment:
public class Level1Fragment extends Fragment { TextView tv; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {ViewGroup v=inflater.inflate(R.layout.activity_test,null);tv= (TextView) v.findViewById(R.id.level1_textview);tv.setText('啦啦啦');return v; } //給View截圖,代碼應(yīng)該沒有問題 public static Bitmap convertViewToBitmap(View view){view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());view.buildDrawingCache();return view.getDrawingCache(); }
現(xiàn)在,我想在Fragment剛加載完成時,獲取這個TextView的截圖,我應(yīng)該把代碼放在哪個事件里?
嘗試解決我嘗試過tv.postDelayed()、tv.post()以及tv.getViewTreeObserver().addOnPreDrawListener()。
嘗試用的代碼也貼出來吧:
@Override public void onResume() {super.onResume();tv.postDelayed(new Runnable() { @Override public void run() {final Bitmap bit=convertViewToBitmap(tv);//iv只是個簡單的ImageView,與本問題無關(guān)iv.setImageBitmap(bit);iv.startAnimation(new AlphaAnimation(1,0)); }},100); }
但是都無法獲取到。
問題所以,請問到底該用什么方法來正確獲取到tv的截圖呢?
(btw,F(xiàn)ragment是直接通過FragmentTransaction.replace()顯示的,而不是通過ViewPager,因此生命周期應(yīng)該不會被打亂..)
求解惑!
問題解答
回答1:你說的這三種方法前兩種都是可以的啊,最后一種有問題,應(yīng)該是getViewTreeObserver().addOnGlobalLayoutListener()
相關(guān)文章:
1. python - pymysql建立連接出錯2. 默認(rèn)輸出類型為json,如何輸出html3. mysql 遠(yuǎn)程連接出錯10060,我已經(jīng)設(shè)置了任意主機了。。。4. python的正則怎么同時匹配兩個不同結(jié)果?5. 數(shù)組排序,并把排序后的值存入到新數(shù)組中6. php多任務(wù)倒計時求助7. MySQL的聯(lián)合查詢[union]有什么實際的用處8. PHP訂單派單系統(tǒng)9. html5 - css3scale和rotate同時使用轉(zhuǎn)換成matrix寫法該如何轉(zhuǎn)換?10. win10 python3.5 matplotlib使用報錯
