Creating a Paint
suggest changeYou can create a new paint with one of these 3 constructors:
new Paint()
Create with default settingsnew Paint(int flags)
Create with flagsnew Paint(Paint from)
Copy settings from another paint
It is generally suggested to never create a paint object, or any other object in onDraw() as it can lead to performance issues. (Android Studio will probably warn you) Instead, make it global and initialize it in your class constructor like so:
public class CustomView extends View {
private Paint paint;
public CustomView(Context context) {
super(context);
paint = new Paint();
//...
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(0xFF000000);
// ...
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents