84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
学习是最好的投资!
Since onDraw() will be called frequently, it is not recommended to create a new object instance in this method.
Your problem may be that you don’t know how to correctly obtain the size of the View. The obtained width and height are always 0.
I’m going to tell you the simplest way, so let’s use it as a starting point
public class CustomView extends View { private Bitmap mBitmap; public CustomView(Context context) { super(context) ... this.post(new Runnable() { @Override public void run() { mBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); } }); } }
You can use View.onLayout or ViewTreeObserver
Since you inherit ImageView, I think you will naturally use the setImageBitmap(), setImageDrawable and other methods of ImageView, so you can get the width and height in these methods or get the bitmap directly
Since onDraw() will be called frequently, it is not recommended to create a new object instance in this method.
Your problem may be that you don’t know how to correctly obtain the size of the View. The obtained width and height are always 0.
I’m going to tell you the simplest way, so let’s use it as a starting point
Use view.post()
You can use View.onLayout or ViewTreeObserver
Since you inherit ImageView, I think you will naturally use the setImageBitmap(), setImageDrawable and other methods of ImageView, so you can get the width and height in these methods or get the bitmap directly