Skip to content

Commit

Permalink
Used AndroidDraw library for zoom and pan
Browse files Browse the repository at this point in the history
  • Loading branch information
sowmen committed Jan 16, 2020
1 parent c831e34 commit b7d6e95
Show file tree
Hide file tree
Showing 42 changed files with 1,309 additions and 76 deletions.
11 changes: 8 additions & 3 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ dependencies {
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

implementation "com.cleveroad:loopbar:1.2.0"
implementation project(":draw")
}
repositories {
mavenCentral()
}
27 changes: 27 additions & 0 deletions app/src/main/java/com/example/painter/DrawView.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.Toast;

Expand Down Expand Up @@ -53,9 +54,27 @@ public class DrawView extends View
public boolean flagline = false;
public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;

private static float MIN_ZOOM = 1f;
private static float MAX_ZOOM = 5f;

private float scaleFactor = 1.f;
private ScaleGestureDetector detector;


public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
setupDraw();
detector = new ScaleGestureDetector(getContext(), new ScaleListener());
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
scaleFactor *= detector.getScaleFactor();
scaleFactor = Math.max(MIN_ZOOM, Math.min(scaleFactor, MAX_ZOOM));
invalidate();
return true;
}
}

public void startNew() {
Expand Down Expand Up @@ -111,6 +130,7 @@ public float getLastBrushSize()
}

public void setErase(boolean isErase) {
this.setColor("#FFFFFF");
erase = isErase;

if (erase)
Expand All @@ -128,13 +148,18 @@ public void setErase(boolean isErase) {
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

canvas.save();
canvas.scale(scaleFactor, scaleFactor);

canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
// canvas.drawPath(drawPath, drawPaint);

for (int i = 0; i < mPaths.size(); ++i) {
canvas.drawPath(mPaths.get(i), mPaints.get(i));
invalidate();
}

canvas.restore();
}

private void addPath(boolean fill)
Expand Down Expand Up @@ -191,6 +216,8 @@ public void drawLine(boolean flag) {
@Override
public boolean onTouchEvent(MotionEvent event) {

detector.onTouchEvent(event);

float touchX = event.getX();
float touchY = event.getY();

Expand Down
127 changes: 60 additions & 67 deletions app/src/main/java/com/example/painter/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
Expand All @@ -16,11 +17,12 @@
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;

import com.divyanshu.draw.widget.DrawView;

//java imports


public class MainActivity extends AppCompatActivity implements OnClickListener
{
public class MainActivity extends AppCompatActivity implements OnClickListener {
private DrawView draw;
private ImageButton currPaint, drawBtn, eraseBtn, newBtn, saveBtn;
static ImageButton redoBtn, undoBtn;
Expand All @@ -31,29 +33,29 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

draw = (DrawView)findViewById(R.id.drawing);
draw = (DrawView) findViewById(R.id.drawing);

LinearLayout paintLayout = (LinearLayout)findViewById(R.id.colors);
LinearLayout paintLayout = (LinearLayout) findViewById(R.id.colors);

currPaint = (ImageButton)paintLayout.getChildAt(0);
currPaint = (ImageButton) paintLayout.getChildAt(0);
currPaint.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.paint_pressed, null));

smallBrush = getResources().getInteger(R.integer.smallSize);
mediumBrush = getResources().getInteger(R.integer.mediumSize);
largeBrush = getResources().getInteger(R.integer.largeSize);

drawBtn = (ImageButton)findViewById(R.id.brushBtn);
drawBtn = (ImageButton) findViewById(R.id.brushBtn);
drawBtn.setOnClickListener(this);

draw.setBrushSize(mediumBrush);
// draw.setBrushSize(mediumBrush);

eraseBtn = (ImageButton)findViewById(R.id.eraseBtn);
eraseBtn = (ImageButton) findViewById(R.id.eraseBtn);
eraseBtn.setOnClickListener(this);

// newBtn = (ImageButton)findViewById(R.id.newBtn);
// newBtn.setOnClickListener(this);

saveBtn = (ImageButton)findViewById(R.id.saveBtn);
saveBtn = (ImageButton) findViewById(R.id.saveBtn);
saveBtn.setOnClickListener(this);

redoBtn = findViewById(R.id.redoBtn);
Expand All @@ -62,104 +64,98 @@ protected void onCreate(Bundle savedInstanceState) {
redoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
draw.onClickRedo();
draw.redo();
}
});
undoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
redoBtn.setEnabled(true);
draw.onClickUndo();
draw.undo();
}
});

if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 112);
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 112);
}
}

public void onClick(View view)
{
if (view.getId() == R.id.brushBtn)
{
public void onClick(View view) {
if (view.getId() == R.id.brushBtn) {
final Dialog brushDialog = new Dialog(this, R.style.CustomDialog);
brushDialog.setTitle("Brush Size:");
brushDialog.setContentView(R.layout.brush_chooser);

ImageButton smallBtn = (ImageButton)brushDialog.findViewById(R.id.small_brush);
smallBtn.setOnClickListener(new OnClickListener()
{
ImageButton smallBtn = (ImageButton) brushDialog.findViewById(R.id.small_brush);
smallBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
draw.setErase(false);
draw.setBrushSize(smallBrush);
draw.setLastBrushSize(smallBrush);
draw.setStrokeWidth(smallBrush);
// draw.setErase(false);
// draw.setBrushSize(smallBrush);
// draw.setLastBrushSize(smallBrush);
brushDialog.dismiss();
}
});

ImageButton mediumBtn = (ImageButton)brushDialog.findViewById(R.id.medium_brush);
mediumBtn.setOnClickListener(new OnClickListener()
{
ImageButton mediumBtn = (ImageButton) brushDialog.findViewById(R.id.medium_brush);
mediumBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
draw.setErase(false);
draw.setBrushSize(mediumBrush);
draw.setLastBrushSize(mediumBrush);
draw.setStrokeWidth(mediumBrush);
// draw.setErase(false);
// draw.setBrushSize(mediumBrush);
// draw.setLastBrushSize(mediumBrush);
brushDialog.dismiss();
}
});

ImageButton largeBtn = (ImageButton)brushDialog.findViewById(R.id.large_brush);
largeBtn.setOnClickListener(new OnClickListener()
{
ImageButton largeBtn = (ImageButton) brushDialog.findViewById(R.id.large_brush);
largeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
draw.setErase(false);
draw.setBrushSize(largeBrush);
draw.setLastBrushSize(largeBrush);
draw.setStrokeWidth(largeBrush);
// draw.setErase(false);
// draw.setBrushSize(largeBrush);
// draw.setLastBrushSize(largeBrush);
brushDialog.dismiss();
}
});

brushDialog.show();
}
else if (view.getId() == R.id.eraseBtn)
{
} else if (view.getId() == R.id.eraseBtn) {
final Dialog brushDialog = new Dialog(this, R.style.CustomDialog);
brushDialog.setTitle("Eraser Size:");
brushDialog.setContentView(R.layout.brush_chooser);

ImageButton smallBtn = (ImageButton)brushDialog.findViewById(R.id.small_brush);
smallBtn.setOnClickListener(new OnClickListener()
{
ImageButton smallBtn = (ImageButton) brushDialog.findViewById(R.id.small_brush);
smallBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
draw.setErase(true);
draw.changeBrushSize((int)smallBrush);
draw.setColor(Color.WHITE);
// draw.setErase(true);
draw.setStrokeWidth(smallBrush);
brushDialog.dismiss();
}
});

ImageButton mediumBtn = (ImageButton)brushDialog.findViewById(R.id.medium_brush);
mediumBtn.setOnClickListener(new OnClickListener()
{
ImageButton mediumBtn = (ImageButton) brushDialog.findViewById(R.id.medium_brush);
mediumBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
draw.setErase(true);
draw.changeBrushSize((int)mediumBrush);
draw.setColor(Color.WHITE);
// draw.setErase(true);
draw.setStrokeWidth(mediumBrush);
brushDialog.dismiss();
}
});

ImageButton largeBtn = (ImageButton)brushDialog.findViewById(R.id.large_brush);
largeBtn.setOnClickListener(new OnClickListener()
{
ImageButton largeBtn = (ImageButton) brushDialog.findViewById(R.id.large_brush);
largeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
draw.setErase(true);
draw.changeBrushSize((int)largeBrush);
draw.setColor(Color.WHITE);
// draw.setErase(true);
draw.setStrokeWidth(largeBrush);
brushDialog.dismiss();
}
});
Expand Down Expand Up @@ -187,15 +183,14 @@ public void onClick(View v) {
//
// newDialog.show();
// }
else if (view.getId() == R.id.saveBtn)
{
else if (view.getId() == R.id.saveBtn) {
AlertDialog.Builder saveDialog = new AlertDialog.Builder(this);
saveDialog.setTitle("Save Drawing");
saveDialog.setMessage("Save drawing to device gallery?");
saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
draw.saveDrawing();
// draw.saveDrawing();
}
});
saveDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
Expand All @@ -208,20 +203,18 @@ public void onClick(DialogInterface dialog, int which) {
}
}

public void colorClicked(View view)
{
if (view != currPaint)
{
ImageButton imgView = (ImageButton)view;
public void colorClicked(View view) {
if (view != currPaint) {
ImageButton imgView = (ImageButton) view;
String color = view.getTag().toString();

draw.setErase(false);
draw.setBrushSize(draw.getLastBrushSize());
draw.setColor(color);
// draw.setErase(false);
// draw.setStrokeWidth(draw.getLastBrushSize());
draw.setColor(Color.parseColor(color));

imgView.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.paint_pressed, null));
currPaint.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.paint, null));
currPaint = (ImageButton)view;
currPaint = (ImageButton) view;
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

</LinearLayout>

<com.example.painter.DrawView
<com.divyanshu.draw.widget.DrawView
android:id="@+id/drawing"
android:layout_width="match_parent"
android:layout_height="0dp"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">drawApp</string>
<string name="app_name">Painter</string>
<string name="newFile">New</string>
<string name="brush">Brush</string>
<string name="erase">Eraser</string>
Expand Down
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.61'
repositories {
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand All @@ -18,7 +20,7 @@ allprojects {
repositories {
google()
jcenter()

maven { url 'https://jitpack.io' }
}
}

Expand Down
1 change: 1 addition & 0 deletions draw/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
Loading

0 comments on commit b7d6e95

Please sign in to comment.