Advertisement
Guest User

FingerPaintDemoActivity

a guest
Mar 9th, 2012
2,245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.94 KB | None | 0 0
  1. /*
  2. * Copyright (C) 2007 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 com.project.fingerpaint;
  18.  
  19.  
  20.  
  21.  
  22. import java.util.LinkedList;
  23. import java.util.Queue;
  24.  
  25. import android.app.Activity;
  26. import android.content.Context;
  27. import android.graphics.Bitmap;
  28. import android.graphics.BitmapFactory;
  29. import android.graphics.BlurMaskFilter;
  30. import android.graphics.Canvas;
  31. import android.graphics.Color;
  32. import android.graphics.EmbossMaskFilter;
  33. import android.graphics.MaskFilter;
  34. import android.graphics.Paint;
  35. import android.graphics.Path;
  36. import android.graphics.Point;
  37. import android.graphics.PorterDuff;
  38. import android.graphics.PorterDuffXfermode;
  39. import android.os.Bundle;
  40. import android.util.AttributeSet;
  41. import android.view.Display;
  42. import android.view.Menu;
  43. import android.view.MenuItem;
  44. import android.view.MotionEvent;
  45. import android.view.View;
  46. import android.view.WindowManager;
  47. import android.widget.RelativeLayout;
  48.  
  49. public class FinderPaintDemo extends Activity implements ColorPickerDialog.OnColorChangedListener {
  50. private RelativeLayout drawingLayout;
  51. private MyView myView;
  52. private Display display;
  53. private Bitmap photoBitmap;
  54. public static int targetColor=Color.WHITE;
  55. public static int replaceColor = Color.GREEN;
  56.  
  57. @Override
  58. protected void onCreate(Bundle savedInstanceState) {
  59. super.onCreate(savedInstanceState);
  60. display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
  61. photoBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
  62. setContentView(R.layout.main);
  63. drawingLayout = (RelativeLayout)findViewById(R.id.drawingLayout);
  64.  
  65. myView = new MyView(this);
  66. drawingLayout.addView(myView);
  67.  
  68.  
  69. // here to put image
  70.  
  71.  
  72. mPaint = new Paint();
  73. mPaint.setAntiAlias(true);
  74. mPaint.setDither(true);
  75. mPaint.setColor(0xFFFF0000);
  76. mPaint.setStyle(Paint.Style.STROKE);
  77. mPaint.setStrokeJoin(Paint.Join.ROUND);
  78. mPaint.setStrokeCap(Paint.Cap.ROUND);
  79. mPaint.setStrokeWidth(12);
  80.  
  81. mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
  82. 0.4f, 6, 3.5f);
  83.  
  84. mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
  85. }
  86.  
  87. private Paint mPaint;
  88. private MaskFilter mEmboss;
  89. private MaskFilter mBlur;
  90.  
  91. public void colorChanged(int color) {
  92. mPaint.setColor(color);
  93. }
  94.  
  95. public class MyView extends View {
  96.  
  97. private static final float MINP = 0.25f;
  98. private static final float MAXP = 0.75f;
  99.  
  100. private Bitmap mBitmap;
  101. private Canvas mCanvas;
  102. private Path mPath;
  103. private Paint mBitmapPaint;
  104.  
  105. public MyView(Context c) {
  106. super(c);
  107.  
  108. mBitmap = Bitmap.createBitmap(display.getWidth(), display.getHeight(), Bitmap.Config.ARGB_8888);
  109. mCanvas = new Canvas(mBitmap);
  110. mPath = new Path();
  111. mBitmapPaint = new Paint(Paint.DITHER_FLAG);
  112. }
  113.  
  114. public MyView(Context c, AttributeSet atrs ) {
  115. super(c, atrs);
  116. mBitmap = Bitmap.createBitmap(display.getWidth(), display.getHeight(), Bitmap.Config.ARGB_8888);
  117. mCanvas = new Canvas(mBitmap);
  118. mPath = new Path();
  119. mBitmapPaint = new Paint(Paint.DITHER_FLAG);
  120.  
  121. }
  122.  
  123.  
  124. @Override
  125. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  126. super.onSizeChanged(w, h, oldw, oldh);
  127. }
  128.  
  129. @Override
  130. protected void onDraw(Canvas canvas) {
  131. canvas.drawColor(0xFFFFFFFF);
  132.  
  133. canvas.drawBitmap (photoBitmap, 0, 0, null); // Extra to show Bitmap
  134.  
  135. canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
  136. canvas.drawPath(mPath, mPaint);
  137. }
  138.  
  139. private float mX, mY;
  140. private static final float TOUCH_TOLERANCE = 4;
  141.  
  142. private void touch_start(float x, float y) {
  143. mPath.reset();
  144. mPath.moveTo(x, y);
  145. mX = x;
  146. mY = y;
  147.  
  148. }
  149. private void touch_move(float x, float y) {
  150. float dx = Math.abs(x - mX);
  151. float dy = Math.abs(y - mY);
  152. if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
  153. mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
  154. mX = x;
  155. mY = y;
  156. }
  157. }
  158. private void touch_up() {
  159. mPath.lineTo(mX, mY);
  160. // commit the path to our offscreen
  161.  
  162. mCanvas.drawPath(mPath, mPaint);
  163. ////////////////////////////////////
  164. // Point pn = new Point();
  165. // FloodFill(photoBitmap,pn , 0xffffffff, 0Xff000000);
  166. //////////////////////////////
  167. // kill this so we don't double draw
  168. mPath.reset();
  169. }
  170.  
  171. @Override
  172. public boolean onTouchEvent(MotionEvent event) {
  173. float x = event.getX();
  174. float y = event.getY();
  175. Point pnt =new Point((int)x,(int)y);
  176.  
  177. targetColor = photoBitmap.getPixel((int)event.getX(),(int) event.getY());
  178.  
  179. System.out.println(targetColor);
  180. System.out.println(replaceColor);
  181.  
  182. //FloodFill(photoBitmap, pnt , targetColor,replaceColor );// extra
  183.  
  184. //invalidate();
  185. switch (event.getAction()) {
  186. case MotionEvent.ACTION_DOWN:
  187. //touch_start(x, y);
  188. FloodFill(photoBitmap, pnt , targetColor, replaceColor );// extra
  189. myView.invalidate();
  190. break;
  191. case MotionEvent.ACTION_MOVE:
  192. //touch_move(x, y);
  193. //invalidate();
  194. break;
  195. case MotionEvent.ACTION_UP:
  196. //touch_up();
  197. // invalidate();
  198. break;
  199. }
  200. return true;
  201. }
  202. }
  203.  
  204. private static final int COLOR_MENU_ID = Menu.FIRST;
  205. private static final int EMBOSS_MENU_ID = Menu.FIRST + 1;
  206. private static final int BLUR_MENU_ID = Menu.FIRST + 2;
  207. private static final int ERASE_MENU_ID = Menu.FIRST + 3;
  208. private static final int SRCATOP_MENU_ID = Menu.FIRST + 4;
  209.  
  210. @Override
  211. public boolean onCreateOptionsMenu(Menu menu) {
  212. super.onCreateOptionsMenu(menu);
  213.  
  214. menu.add(0, COLOR_MENU_ID, 0, "Color").setShortcut('3', 'c');
  215. menu.add(0, EMBOSS_MENU_ID, 0, "Emboss").setShortcut('4', 's');
  216. menu.add(0, BLUR_MENU_ID, 0, "Blur").setShortcut('5', 'z');
  217. menu.add(0, ERASE_MENU_ID, 0, "Erase").setShortcut('5', 'z');
  218. menu.add(0, SRCATOP_MENU_ID, 0, "SrcATop").setShortcut('5', 'z');
  219.  
  220. /**** Is this the mechanism to extend with filter effects?
  221. Intent intent = new Intent(null, getIntent().getData());
  222. intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
  223. menu.addIntentOptions(
  224. Menu.ALTERNATIVE, 0,
  225. new ComponentName(this, NotesList.class),
  226. null, intent, 0, null);
  227. *****/
  228. return true;
  229. }
  230.  
  231. @Override
  232. public boolean onPrepareOptionsMenu(Menu menu) {
  233. super.onPrepareOptionsMenu(menu);
  234. return true;
  235. }
  236.  
  237. @Override
  238. public boolean onOptionsItemSelected(MenuItem item) {
  239. mPaint.setXfermode(null);
  240. mPaint.setAlpha(0xFF);
  241.  
  242. switch (item.getItemId()) {
  243. case COLOR_MENU_ID:
  244. new ColorPickerDialog(this, this, mPaint.getColor()).show();
  245. return true;
  246. case EMBOSS_MENU_ID:
  247. if (mPaint.getMaskFilter() != mEmboss) {
  248. mPaint.setMaskFilter(mEmboss);
  249. } else {
  250. mPaint.setMaskFilter(null);
  251. }
  252. return true;
  253. case BLUR_MENU_ID:
  254. if (mPaint.getMaskFilter() != mBlur) {
  255. mPaint.setMaskFilter(mBlur);
  256. } else {
  257. mPaint.setMaskFilter(null);
  258. }
  259. return true;
  260. case ERASE_MENU_ID:
  261. mPaint.setXfermode(new PorterDuffXfermode(
  262. PorterDuff.Mode.CLEAR));
  263. return true;
  264. case SRCATOP_MENU_ID:
  265. mPaint.setXfermode(new PorterDuffXfermode(
  266. PorterDuff.Mode.SRC_ATOP));
  267. mPaint.setAlpha(0x80);
  268. return true;
  269. }
  270. return super.onOptionsItemSelected(item);
  271. }
  272.  
  273. private void FloodFill(Bitmap bmp, Point pt, int targetColor, int replacementColor){
  274.  
  275. System.out.println("Method Call thai");
  276. Queue<Point> q = new LinkedList<Point>();
  277. q.add(pt);
  278. while (q.size() > 0) {
  279. System.out.println("While ma aavyo.");
  280. Point n = q.poll();
  281. if (bmp.getPixel(n.x, n.y) != targetColor)
  282. continue;
  283.  
  284. Point w = n, e = new Point(n.x + 1, n.y);
  285. while ((w.x > 0) && (bmp.getPixel(w.x, w.y) == targetColor)) {
  286. bmp.setPixel(w.x, w.y, replacementColor);
  287. System.out.println("Pixcel Set thase");
  288. if ((w.y > 0) && (bmp.getPixel(w.x, w.y - 1) == targetColor))
  289. q.add(new Point(w.x, w.y - 1));
  290. if ((w.y < bmp.getHeight() - 1)
  291. && (bmp.getPixel(w.x, w.y + 1) == targetColor))
  292. q.add(new Point(w.x, w.y + 1));
  293. w.x--;
  294. }
  295. while ((e.x < bmp.getWidth() - 1)
  296. && (bmp.getPixel(e.x, e.y) == targetColor)) {
  297. bmp.setPixel(e.x, e.y, replacementColor);
  298.  
  299. if ((e.y > 0) && (bmp.getPixel(e.x, e.y - 1) == targetColor))
  300. q.add(new Point(e.x, e.y - 1));
  301. if ((e.y < bmp.getHeight() - 1)
  302. && (bmp.getPixel(e.x, e.y + 1) == targetColor))
  303. q.add(new Point(e.x, e.y + 1));
  304. e.x++;
  305. }
  306. }
  307. }
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement