Goal:
Create an imageview that is drawable and zoomable,
That means when I press a button to on , it is drawable,
or when I turn off, that is zoomable.
*Notice the drawings should zoom align with the imageview
===============================================================
Recently I wrote a custom drawable image view like this:
public class DrawView extends ImageView {
private int color = Color.BLACK;
private float width = 4f;
private List<Holder> holderList = new ArrayList<Holder>();
private class Holder {
Path path;
Paint paint;
Holder(int color, float width) {
path = new Path();
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(width);
paint.setColor(color);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
}
}
public DrawView(Context context) {
super(context);
init();
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
holderList.add(new Holder(color, width));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (Holder holder : holderList) {
canvas.drawPath(holder.path, holder.paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
holderList.add(new Holder(color,width));
holderList.get(holderList.size() - 1).path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
holderList.get(holderList.size() - 1).path.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
break;
default:
return false;
}
invalidate();
return true;
}
public void resetPaths() {
for (Holder holder : holderList) {
holder.path.reset();
}
invalidate();
}
public void setBrushColor(int color) {
this.color = color;
}
public void setWidth(float width) {
this.width = width;
}
}
And the XML is :
<com.example.tool.DrawView
android:id="@+id/draw"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true" />
The problem is , how to make it zoom-able as well? notice that the drawings should align with the imageview when zooming.
Attempt using some custom imageview library but no luck.
e.g. When I use photoview it can be zoom but the drawings not align, and zoom level will reset after I turn on / off the zooming https://github.com/chrisbanes/PhotoView
Also , find some other library like that but not fit the custom view case https://github.com/matabii/scale-imageview-android
Update1: demo
Recommended to reference this app, the drawing function is actually the same as what I am struggling to achieve, but I can't figure out how they get it done
https://play.google.com/store/apps/details?id=com.zentertain.photoeditor&hl=en
Thanks
Update2: From Sandeep Maram Source
Thanks a lot Sandeep Maram, after testing the code, everything work well, the only thing remain is the drawings is not align with the zoom view. Please take a look at the screenshot
Before:
After:
The circle is not scale up / down when zoom, would be really nice if fix that. also that is not matter if the image overlap the button.
Answer updated with zoom enable/disable and drawableview
activity_main.xml
MainActivity.java
DrawableView.java
CustomImageView
I hope it will work for you.
I don't have complete solution. However I can suggest you to take a look at PhotoView which has zoom implementation and my custom
CanvasView
which I used in one of the old projects. It allows to draw using "pen" tool and type text on it. Text feature is unnecessary for you and probably was too specific for that part so you can skip it. Maybe it'll help you. It supports screen rotation which leads to new drawable dimensions and both text and curves are scaled in this case.