Wednesday, 15 July 2015

cannot seem to get bitmap from view in android -


My problem is similar to one.

my code to work with the method

P>

  private bitmap getBitmapFromView (View v) {v.setDrawingCacheEnabled (true); Wkmasur (Measurespeskmakemeasurespes (0, Measurespeskunspesifaiad), Measurespeskmakemeasurespes (0, Measurespeskunspesifaiad)); V.layout (0, 0, v.getmesuredWidth), v.getMeasuredHeight ()); V.buildDrawingCache (true); Bitmap b = bitmap.kreatebitmap (v.getDrawingCache ()); V.setDrawingCacheEnabled (false); // Clear drawing cache returns b; }  

But today I after changes have changed the XML layout files I line Bitmap b = Bitmapkkretebitmap (v.getDrawingCache ()) NullPointerException Receiving. I

  public static bitmap loadBitmapFromView (View v) {Bitmap b = Bitmap.createBitmap (v.getLayoutParams (). Width method  getBitmapFromView  try to change Key, V.getLayoutParams (). Height, Bitmap.sf. ARGB_8888); Canvas C = new canvas (B); V.layout (0, 0, v.getLayoutParams (). Width, v.getLayoutParams (). Height); V.draw (c); Return b; }  

But this did not help, I get null in the first line.

The situation is: I am trying to convert the view into bitmap. But I'm doing this without displaying the scene on the screen. Again, it all worked until I changed the content of the XML layout file. The change was not too big I just removed the stuff and removed - yes it was removed - some sub-scenes.

In any case, I know that there is no problem with my layout file because I can display it on screen if I want to; Which I have done as part of troubleshooting.

Someone has suggested using a handler to call getDrawingCache () . If he answers this question, how would I write that code?

Update

  View View = activity.getLayoutInflater () increase. (R Layout My_view, blank, incorrect); FindViewsByIds (see); // where I inflate sub-views and add content to them  

After

createBitmapFromView is executed when the view is measured.

  public static zero performance when see (Last View View, Final Runnab Runner) {if (view.getMeasuredHeight () == 0 || view.getMeasuredWidth () == 0) {view.getViewTreeObserver (). AddOnGlobalLayoutListener (New ViewTreeObserver.OnGlobalLayoutListener) {@Override Public Layer on Public Zero () {runnable.run (); RemoveGlobalOnLayoutListener (see, this);}}); } And {runnable.run (); }} Public static bitmap createBitmapFromView (View) {Bitmap bitmap = Bitmap.createBitmap (view.getMeasuredWidth (), view.getMeasuredHeight (), Bitmap.Config.ARGB_8888); Canvas canvas = new canvas (bitmap); View.draw (canvas); Return bitmap; } @TargetApi (Build.VERSION_CODES.JELLY_BEAN) public to view static void removeGlobalOnLayoutListener (see, ViewTreeObserver.OnGlobalLayoutListener listener) {try {view.getViewTreeObserver () removeOnGlobalLayoutListener (listener). } Hold (Noschamparter E. E) {view.getViewTreeObserver (). RemoveGlobalOnLayoutListener (listener); }} ExecuteWhenViewMeasured (see, new Runnabel) {@Override Public Zero Run () {bitmap bitmap = createBitmapFromView (see);}});  

No comments:

Post a Comment