Created
October 28, 2022 11:13
-
-
Save ikhlaqmalik13/753496a7e02f48f87e5de6fbc7107329 to your computer and use it in GitHub Desktop.
Drawing View in Java for Android App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<com.maple.core_classes.DrawView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.maple.core_classes; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.Point; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class DrawView extends View implements View.OnTouchListener{ | |
Paint paint = new Paint(); | |
List<Point> points = new ArrayList<Point>(); | |
public DrawView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(); | |
} | |
public DrawView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public DrawView(Context context) { | |
super(context); | |
init(); | |
} | |
private void init() { | |
setFocusable(true); | |
setFocusableInTouchMode(true); | |
setOnTouchListener(this); | |
paint.setColor(Color.BLACK); | |
paint.setAntiAlias(true); | |
} | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
Point point = new Point(); | |
point.x = (int) event.getX(); | |
point.y = (int) event.getY(); | |
points.add(point); | |
invalidate(); | |
return true; | |
} | |
@Override | |
public void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
for(Point point : points){ | |
canvas.drawCircle(point.x, point.y, 10, paint); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment