A simple Android view for drawing.
If you are using Gradle 6.8 or higher version, add it in setting.gradle
at the end of repositories:
dependencyResolutionManagement {
repositories {
maven { url 'https://jitpack.io' }
}
}
If not, add it in your root build.gradle
at the end of repositories:
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.DonghanX:Draw:v1.0.3'
}
- Support multiple types of lines, including solid line, dashed line and Chisel Tip line.
- Eraser, Redo, Undo and ClearAll.
- Change the background of the Canvas by setting image resource or color.
- Keep track of the state after performing drawing actions.
- Save the Canvas as Bitmap.
<com.redhoodhan.draw.DrawView
android:id="@+id/draw_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.redhoodhan.draw.DrawView
android:id="@+id/draw_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultBrushColor="@color/purple"
app:defaultBrushSize="20F"
app:defaultCanvasBackgroundImageRes="@drawable/img_draw_background_1"
app:defaultEraserSize="15F" />
Attribute Name | Format | Description |
---|---|---|
defaultBrushSize | Float | The default value of brush size |
defaultEraserSize | Float | The default value of eraser size |
defaultBrushColor | Color | The default stroke color of brush paint |
defaultCanvasBackgroundColor | Color | The default color of canvas background |
defaultCanvasBackgroundImageRes | Reference | The default image resource ID of canvas background |
Note that setting
defaultCanvasBackgroundImageRes
will override the effect ofdefaultCanvasBackgroundColor
.
drawViewPressCallback
: Invoked when you press theDrawView
.undoStateCallback
/redoStateCallback
: Invoked when the content in theDrawView
changes. The given receiver parameter returns true if Undo / Redo action is available after the change.
binding.drawView.apply {
drawViewPressCallback = {
// Do your stuff here
}
undoStateCallback = { isUndoAvailable ->
// Do your stuff here
}
redoStateCallback = { isRedoAvailable ->
// Do your stuff here
}
}
binding.drawView.lineType = LineType.SOLID
LineType
is utilized by DrawView
to modify the path properties and paint options, such as StrokeStyle
, Alpha
and PathEffect
.
LineType | Description |
---|---|
LineType.SOLID | Solid line |
LineType.DASH | Dashed line with gap interval that is calculated by brushSize |
LineType.CHISEL | Chisel Tip line with alpha channel set to a specific value |
LineType.ERASER | Solid line with Xfermode set to PorterDuff.Mode.CLEAR |
binding.drawView.undo()
binding.drawView.redo()
binding.drawView.clearCanvas(needsSaving = true)
If needsSaving
is true, then this ClearAll action is capable of being redone.
binding.drawView.canvasBackgroundColor = Color.BLACK
or
binding.drawView.canvasBackgroundColor = ResourcesCompat.getColor(resources, yourColorResId, null)
Note that
canvasBackgroundColor
receives a single color value in the form 0xAARRGGBB.
binding.drawView.canvasBackgroundImg = yourImageResId
binding.drawView.brushColor = Color.BLACK
or
binding.drawView.brushColor = ResourcesCompat.getColor(resources, yourcolorResId, null)
Note that
brushColor
receives a single color value in the form 0xAARRGGBB.
binding.drawView.brushSize = 10F
Note that
brushSize
receives a single float value.
Also note that the minimum stroke width is set to 3F.
binding.drawView.eraserSize = 10F
Note that
eraserSize
receives a single float value.
Also note that the minimum stroke width is set to 3F.
val bitmap = binding.drawView.saveAsBitmap()
- Inspired by
DrawingView
in mvojtkovszky.
Copyright 2022 Donghan X
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.