To create a drawing view in Android using Java, you can follow a similar approach as the Kotlin example but translated into Java. Here’s how you can implement a basic drawing view with all features in Java:
- CustomDrawView.java: This is the custom view class for drawing.
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class CustomDrawView extends View {
private Paint mPaint;
private Path mPath;
private Bitmap mBitmap;
private Canvas mCanvas;
private final int mBackgroundColor = Color.WHITE;
private final float mBrushSize = 8f;
public CustomDrawView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPath = new Path();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeWidth(mBrushSize);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(mBackgroundColor);
canvas.drawPath(mPath, mPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPath.moveTo(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
mPath.lineTo(touchX, touchY);
break;
case MotionEvent.ACTION_UP:
mPath.lineTo(touchX, touchY);
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
break;
default:
return false;
}
invalidate();
return true;
}
public void clearDrawing() {
mPath.reset();
mCanvas.drawColor(mBackgroundColor);
invalidate();
}
public void setBrushColor(int color) {
mPaint.setColor(color);
}
public void setBrushSize(float size) {
mPaint.setStrokeWidth(size);
}
}
- activity_main.xml: Include the custom view in your layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<your.package.name.CustomDrawView
android:id="@+id/customDrawView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Make sure to replace your.package.name
with your actual package name.
- MainActivity.java: Initialize your custom view and add any additional features as needed.
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private CustomDrawView customDrawView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customDrawView = findViewById(R.id.customDrawView);
// Example: Set brush color
// customDrawView.setBrushColor(Color.RED);
}
}
This setup provides a basic drawing view. You can extend it with more features like undo/redo, saving the drawing, adding text, shapes, and much more by leveraging the Paint
and Canvas
classes.