Skip to content

Commit

Permalink
Fix NPE when trying to create state bitmap #2
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz committed Dec 14, 2018
1 parent 24fdd76 commit 10080e8
Showing 1 changed file with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,27 @@ private BitmapDrawable createStateDrawable(@NonNull String text,
stateTV.measure(MeasureSpec.makeMeasureSpec(0 /* any */, View.MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0 /* any */, View.MeasureSpec.UNSPECIFIED));
stateTV.layout(0, 0, stateTV.getMeasuredWidth(), stateTV.getMeasuredHeight());
stateTV.buildDrawingCache(true);
// Convert text view to bitmap
Bitmap stateBm = Bitmap.createBitmap(stateTV.getDrawingCache());
stateTV.setDrawingCacheEnabled(false);
Bitmap stateBm = createBitmapFromView(stateTV);
// Create shadow
Bitmap shadowBm = createShadow(stateBm, shadowBottomOverflowPx / 2);
return new BitmapDrawable(getResources(), combineBitmaps(shadowBm, shadow ? 50 : 0, stateBm, shadowTopOverflowPx));
}

/**
* Creates a bitmap from a view.
*/
public Bitmap createBitmapFromView(@NonNull View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null) {
bgDrawable.draw(canvas);
}
view.draw(canvas);
return returnedBitmap;
}

/**
* Creates a bitmap representing the shadow of given bitmap.
*/
Expand Down

0 comments on commit 10080e8

Please sign in to comment.