1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package rs.example.android.com.healingbrush; 18 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.graphics.Canvas; 22 import android.graphics.Color; 23 import android.graphics.Matrix; 24 import android.graphics.Paint; 25 import android.graphics.Path; 26 import android.graphics.RectF; 27 import android.graphics.drawable.Drawable; 28 import android.util.AttributeSet; 29 import android.view.View; 30 import android.widget.ImageView; 31 32 import java.util.ArrayList; 33 import java.util.Arrays; 34 35 public class DrawView extends View { 36 private static final String TAG = "DrawView"; 37 private ImageView mImageView; 38 Path mPoints_backup = new Path(); 39 float[] path = new float[200]; 40 Path mPoints = new Path(); 41 int len; 42 Paint mPaint1; 43 Paint mPaint2; 44 private boolean mDone; 45 private boolean mUseDefaultRegion = true; 46 47 ArrayList<Drawable> drawList = new ArrayList<Drawable>(); 48 setup(Context context)49 private void setup(Context context) { 50 mPaint1 = new Paint(); 51 mPaint2 = new Paint(); 52 mPaint1.setStyle(Paint.Style.STROKE); 53 mPaint1.setColor(Color.BLACK); 54 mPaint1.setStrokeWidth(2); 55 mPaint2.setStyle(Paint.Style.STROKE); 56 mPaint2.setColor(Color.YELLOW); 57 } 58 DrawView(Context context)59 public DrawView(Context context) { 60 super(context); 61 setup(context); 62 } 63 DrawView(Context context, AttributeSet attrs)64 public DrawView(Context context, AttributeSet attrs) { 65 super(context, attrs); 66 setup(context); 67 } 68 DrawView(Context context, AttributeSet attrs, int defStyleAttr)69 public DrawView(Context context, AttributeSet attrs, int defStyleAttr) { 70 super(context, attrs, defStyleAttr); 71 setup(context); 72 } 73 74 /** 75 * Assumes imageView is using matrix mode 76 * 77 * @param imageView 78 */ setImageView(ImageView imageView)79 public void setImageView(ImageView imageView) { 80 mImageView = imageView; 81 } 82 83 RectF rec = new RectF(); 84 85 @Override onDraw(Canvas canvas)86 protected void onDraw(Canvas canvas) { 87 Matrix m = mImageView.getImageMatrix(); 88 Drawable d = mImageView.getDrawable(); 89 canvas.concat(m); 90 91 for (Drawable elem : drawList) { 92 elem.draw(canvas); 93 } 94 canvas.drawPath(mPoints, mPaint1); 95 canvas.drawPath(mPoints, mPaint2); 96 } 97 getRegion(Bitmap img)98 public Region getRegion(Bitmap img) { 99 Region ret; 100 if (mUseDefaultRegion) { 101 float[] defaultPath = {10.0f, 110.0f, 102 110.0f, 10.0f, 103 210.0f, 110.0f, 104 110.0f, 210.0f}; 105 ret = new Region(Arrays.copyOf(defaultPath, defaultPath.length), img); 106 } else { 107 ret = new Region(Arrays.copyOf(path, len), img); 108 } 109 invalidate(); 110 return ret; 111 } 112 downPoint(float[] imgPoint)113 public void downPoint(float[] imgPoint) { 114 path[0] = imgPoint[0]; 115 path[1] = imgPoint[1]; 116 len = 2; 117 mPoints_backup.reset(); 118 mPoints_backup.addPath(mPoints); 119 mPoints.reset(); 120 mPoints.moveTo(imgPoint[0], imgPoint[1]); 121 mUseDefaultRegion = false; 122 } 123 undo()124 public void undo() { 125 mPoints.reset(); 126 mPoints.addPath(mPoints_backup); 127 } 128 movePoint(float[] imgMoveList, int size)129 public void movePoint(float[] imgMoveList, int size) { 130 if (len + size * 2 >= path.length) { 131 path = Arrays.copyOf(path, 2 * (len + size * 2)); 132 } 133 for (int i = size * 2 - 2; i >= 0; i -= 2) { 134 mPoints.lineTo(imgMoveList[i], imgMoveList[i + 1]); 135 path[len] = imgMoveList[i]; 136 path[len + 1] = imgMoveList[i + 1]; 137 len += 2; 138 } 139 } 140 upPoint(float[] imgPoint)141 public void upPoint(float[] imgPoint) { 142 if (len + 2 >= path.length) { 143 path = Arrays.copyOf(path, 2 * (len + 2)); 144 } 145 path[len] = imgPoint[0]; 146 path[len + 1] = imgPoint[1]; 147 len += 2; 148 mPoints.lineTo(imgPoint[0], imgPoint[1]); 149 mPoints.close(); 150 mDone = true; 151 } 152 addDrawable(Drawable d)153 public void addDrawable(Drawable d) { 154 drawList.add(d); 155 } 156 clearDrawables()157 public void clearDrawables() { 158 drawList.clear(); 159 } 160 } 161