(imgView);
127 | }
128 |
129 | @Override
130 | protected Bitmap doInBackground(Integer... params) {
131 | mPosition = params[0];
132 | File currentfile = mFileList[mPosition];
133 | Log.e(TAG, "img file:"+currentfile);
134 | //for explanation of why to use BMFactory options see
135 | //http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966
136 |
137 | String filePath = currentfile.getAbsolutePath();
138 |
139 | //Decode with inSampleSize
140 | Bitmap loadedBM = BitmapFactory.decodeFile(filePath, mBitmapFactoryOpts);
141 |
142 | Bitmap thumbBitMap = Bitmap.createBitmap(loadedBM.getWidth(), loadedBM.getHeight(),
143 | Bitmap.Config.RGB_565);
144 |
145 | Canvas newCanvas = new Canvas();
146 | newCanvas.setBitmap(thumbBitMap);
147 | thumbBitMap.eraseColor(mBackgroundColor);
148 | newCanvas.drawBitmap(loadedBM, 0, 0, mPainter);
149 |
150 | loadedBM.recycle();
151 |
152 | Log.d(TAG, "MkThumb Aft Recycle "+SKNotes.getMemUsageString());
153 |
154 | return thumbBitMap;
155 | }
156 |
157 | @Override
158 | protected void onPostExecute(Bitmap thumb) {
159 | if (mImgView != null) {
160 | ImageView iView = mImgView.get();
161 | if (iView != null) {
162 | iView.setImageBitmap(thumb);
163 | }
164 | }
165 | }
166 | }
167 | }
168 |
169 |
170 |
--------------------------------------------------------------------------------
/src/com/manichord/sketchnotes/PagesList.java:
--------------------------------------------------------------------------------
1 | package com.manichord.sketchnotes;
2 |
3 | import java.util.Date;
4 |
5 | import android.app.Activity;
6 | import android.content.DialogInterface;
7 | import android.content.Intent;
8 | import android.content.DialogInterface.OnClickListener;
9 | import android.os.Bundle;
10 | import android.os.Debug;
11 | import android.util.Log;
12 | import android.view.ContextMenu;
13 | import android.view.Menu;
14 | import android.view.MenuItem;
15 | import android.view.View;
16 | import android.widget.AdapterView;
17 | import android.widget.AdapterView.AdapterContextMenuInfo;
18 | import android.widget.AdapterView.OnItemClickListener;
19 | import android.widget.GridView;
20 |
21 | public class PagesList extends Activity implements OnItemClickListener {
22 |
23 | private static final String TAG = "PagesList";
24 |
25 | //==Pop up menu ==
26 | /** Menu ID for the command to clear the window. */
27 | private static final int RENAME_ID = Menu.FIRST;
28 |
29 | /** Menu ID for the command to clear the window. */
30 | private static final int DELETE_ID = Menu.FIRST + 1;
31 |
32 | /** Menu ID for the command to clear the window. */
33 | private static final int TAG_ID = Menu.FIRST + 2;
34 | //=====
35 |
36 | /** Menu ID for the command to create new sketch page. */
37 | private static final int NEW_ID = Menu.FIRST + 3;
38 |
39 | private PageImageAdapter mAdapter;
40 |
41 | private GridView mView;
42 |
43 | @Override
44 | public void onCreate(Bundle savedInstanceState) {
45 | super.onCreate(savedInstanceState);
46 |
47 | Log.d(TAG, "Create PAGESLIST - "+SKNotes.getMemUsageString());
48 |
49 | setContentView(R.layout.pages);
50 |
51 | mView = (GridView) findViewById(R.id.gridview);
52 | if (mView == null) {
53 | Log.e(TAG, "NO GRIDVIEW!!");
54 | } else {
55 | mAdapter = new PageImageAdapter(this);
56 | mView.setAdapter(mAdapter);
57 | mView.setOnItemClickListener(this);
58 | registerForContextMenu(mView);
59 | }
60 | }
61 |
62 | @Override
63 | public boolean onCreateOptionsMenu(Menu menu) {
64 | menu.add(0, NEW_ID, 0, "New");
65 |
66 | return super.onCreateOptionsMenu(menu);
67 | }
68 |
69 | @Override
70 | public boolean onOptionsItemSelected(MenuItem item) {
71 | switch (item.getItemId()) {
72 | case NEW_ID:
73 | Intent intent = new Intent(this, SKNotes.class);
74 | startActivity(intent);
75 | return true;
76 | default:
77 | return super.onOptionsItemSelected(item);
78 | }
79 | }
80 |
81 | @Override
82 | protected void onPause() {
83 | super.onPause();
84 | Log.d(TAG, "Paused PAGESLIST"+SKNotes.getMemUsageString());
85 | }
86 |
87 |
88 | @Override
89 | public void onCreateContextMenu(ContextMenu menu, View v,
90 | ContextMenu.ContextMenuInfo menuInfo) {
91 | populateMenu(menu);
92 | }
93 |
94 | @Override
95 | public boolean onContextItemSelected(MenuItem item) {
96 | AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
97 | .getMenuInfo();
98 | int index = info.position;
99 |
100 | switch (item.getItemId()) {
101 | case RENAME_ID:
102 | Log.i(TAG, "reName Page" + index);
103 | // TODO
104 | return true;
105 | case TAG_ID:
106 | Log.i(TAG, "Tag Page" + index);
107 | // TODO
108 | return true;
109 | case DELETE_ID:
110 | //TODO: add confirmation dialog
111 | mAdapter.deletePage(index);
112 | mView.invalidateViews();
113 | return true;
114 | default:
115 | return super.onContextItemSelected(item);
116 | }
117 | }
118 |
119 | private void populateMenu(Menu menu) {
120 | //TODO: implement rename function
121 | //menu.add(Menu.NONE, RENAME_ID, Menu.NONE, "Rename");
122 | menu.add(Menu.NONE, DELETE_ID, Menu.NONE, "Delete");
123 | //TODO: implement tagging function
124 | //menu.add(Menu.NONE, TAG_ID, Menu.NONE, "Tag");
125 |
126 | }
127 |
128 |
129 | public void onItemClick(AdapterView> parent, View v,
130 | int position, long id) {
131 | Log.e(TAG, "pos:" + position);
132 | Intent intent = new Intent(this, SKNotes.class);
133 | intent.putExtra(SKNotes.LOAD_FILENAME, mAdapter.getFileName(position));
134 | startActivity(intent);
135 | }
136 |
137 | }
138 |
--------------------------------------------------------------------------------
/src/com/manichord/sketchnotes/SKNPrefs.java:
--------------------------------------------------------------------------------
1 | package com.manichord.sketchnotes;
2 |
3 | import android.os.Bundle;
4 | import android.preference.PreferenceActivity;
5 |
6 | public class SKNPrefs extends PreferenceActivity {
7 |
8 | @Override
9 | protected void onCreate(Bundle savedInstanceState) {
10 | super.onCreate(savedInstanceState);
11 | addPreferencesFromResource(R.xml.preferences);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/com/manichord/sketchnotes/SKNotes.java:
--------------------------------------------------------------------------------
1 | package com.manichord.sketchnotes;
2 |
3 | import java.io.File;
4 | import java.io.FileInputStream;
5 | import java.io.FileOutputStream;
6 | import java.io.IOException;
7 | import java.io.InputStream;
8 | import java.io.OutputStream;
9 | import java.util.Date;
10 |
11 | import android.app.Activity;
12 | import android.content.Context;
13 | import android.content.Intent;
14 | import android.net.Uri;
15 | import android.os.Bundle;
16 | import android.os.Debug;
17 | import android.util.Log;
18 | import android.view.LayoutInflater;
19 | import android.view.Menu;
20 | import android.view.MenuItem;
21 | import android.view.View;
22 | import android.view.View.OnClickListener;
23 | import android.view.ViewGroup;
24 | import android.widget.Button;
25 | import android.widget.ImageButton;
26 | import android.widget.TableRow;
27 | import android.widget.Toast;
28 |
29 | public class SKNotes extends Activity {
30 |
31 | private static final String TAG = "SKNotes";
32 |
33 | /** Menu ID for the command to clear the window. */
34 | private static final int CLEAR_ID = Menu.FIRST;
35 |
36 | /** Menu ID for the command to list pages */
37 | private static final int PAGELIST_ID = Menu.FIRST + 1;
38 |
39 | /** Menu ID for the command to Save current page */
40 | private static final int SETTINGS_ID = Menu.FIRST + 2;
41 |
42 | /** Menu ID for the command to Save current page */
43 | private static final int SHARE_ID = Menu.FIRST + 3;
44 |
45 | protected static final String LOAD_FILENAME = "com.manichord.sketchnotes.load_filename";
46 |
47 | /** The view responsible for drawing the window. */
48 | SketchView sView;
49 |
50 | private ImageButton colourMenuButton;
51 |
52 |
53 | private String mCurrentFileName;
54 |
55 | /** Called when the activity is first created. */
56 | @Override
57 | public void onCreate(Bundle savedInstanceState) {
58 |
59 | super.onCreate(savedInstanceState);
60 |
61 | setupViews();
62 | }
63 |
64 | @Override
65 | public boolean onCreateOptionsMenu(Menu menu) {
66 | menu.add(0, CLEAR_ID, 0, "New");
67 | menu.add(0, PAGELIST_ID, 0, "Pages");
68 | menu.add(0, SETTINGS_ID, 0, "Settings");
69 | menu.add(0, SHARE_ID, 0, "Share");
70 |
71 | return super.onCreateOptionsMenu(menu);
72 | }
73 |
74 | @Override
75 | public boolean onOptionsItemSelected(MenuItem item) {
76 | switch (item.getItemId()) {
77 | case CLEAR_ID:
78 | if (sView != null) {
79 | sView.saveCurrentBitMap(mCurrentFileName);
80 |
81 | Date now = new Date();
82 | mCurrentFileName = "skpage" + now.getTime() + ".png";
83 | sView.clear();
84 | } else {
85 | Log.e(TAG, "NO Sketch VIEW!!!");
86 | }
87 | return true;
88 | case PAGELIST_ID:
89 | Intent intent = new Intent(this, PagesList.class);
90 | sView.saveCurrentBitMap(mCurrentFileName);
91 | startActivity(intent);
92 | return true;
93 | case SETTINGS_ID:
94 | startActivity(new Intent(this, SKNPrefs.class));
95 | return true;
96 | case SHARE_ID:
97 | shareSketch();
98 | return true;
99 | default:
100 | return super.onOptionsItemSelected(item);
101 | }
102 | }
103 |
104 | private void shareSketch() {
105 | // make sure current version is saved
106 | sView.saveCurrentBitMap(mCurrentFileName);
107 | Intent sharingIntent = new Intent(Intent.ACTION_SEND);
108 | File extFile = new File(getShareDir(), mCurrentFileName);
109 | FileInputStream internalFile = null;
110 | try {
111 | internalFile = getApplicationContext().openFileInput(
112 | mCurrentFileName);
113 | copyToFile(internalFile, extFile);
114 | Uri sketchUri = Uri.fromFile(extFile);
115 | sharingIntent.setType("image/png");
116 | sharingIntent.putExtra(Intent.EXTRA_STREAM, sketchUri);
117 | startActivity(Intent.createChooser(sharingIntent,
118 | "Share Sketch Using..."));
119 | } catch (IOException e) {
120 | Log.e(TAG, "error sharing " + mCurrentFileName, e);
121 | } finally {
122 | if (internalFile != null) {
123 | try {
124 | internalFile.close();
125 | } catch (IOException e) {
126 | // can't do anything about this
127 | }
128 | }
129 | }
130 | }
131 |
132 | private File getShareDir() {
133 | File dir = new File(getExternalCacheDir(), "share");
134 | if (!dir.exists()) {
135 | dir.mkdirs();
136 | }
137 | return dir;
138 | }
139 |
140 | /**
141 | * Copies src file to dst file. If the dst file does not exist, it will be
142 | * created.
143 | *
144 | * @param src
145 | * @param dst
146 | * @throws IOException
147 | */
148 | public static void copyToFile(InputStream src, File dst)
149 | throws IOException {
150 | OutputStream out = null;
151 | try {
152 | out = new FileOutputStream(dst);
153 |
154 | // Transfer bytes from in to out
155 | byte[] buf = new byte[1024];
156 | int len;
157 | while ((len = src.read(buf)) > 0) {
158 | out.write(buf, 0, len);
159 | }
160 | } finally {
161 | try {
162 | if (out != null) {
163 | out.close();
164 | }
165 | } catch (IOException e) {
166 | // can't do anything about this
167 | }
168 | }
169 | }
170 |
171 | @Override
172 | protected void onPause() {
173 | super.onPause();
174 | sView.saveCurrentBitMap(mCurrentFileName);
175 | sView.nullBitmaps();
176 | System.gc();
177 | Log.d(TAG, "SKNotes onPause:"+mCurrentFileName+" mem:" + getMemUsageString());
178 | }
179 |
180 | @Override
181 | protected void onResume() {
182 | super.onResume();
183 | Log.d(TAG, "SKNotes onResume:"+mCurrentFileName+" mem:" + getMemUsageString());
184 | setupViews();
185 | }
186 |
187 | public String getCurrentFileName() {
188 | return mCurrentFileName;
189 | }
190 |
191 | public static String getMemUsageString() {
192 | int usedNativeKbs = (int) (Debug.getNativeHeapAllocatedSize() / 1024L);
193 |
194 | return String.format(" SKNotes Memory Used: %d KB", usedNativeKbs);
195 | }
196 |
197 | private void setupViews() {
198 |
199 | setContentView(R.layout.main);
200 | sView = (SketchView) findViewById(R.id.skview);
201 |
202 | findViewById(R.id.eraserButton).setOnClickListener(sView);
203 | findViewById(R.id.penButton).setOnClickListener(sView);
204 |
205 | Date now = new Date();
206 |
207 | String fileToLoad = getIntent().getStringExtra(LOAD_FILENAME);
208 | getIntent().removeExtra(LOAD_FILENAME);//clear intent
209 | if (fileToLoad != null && !"".equals(fileToLoad)) {
210 | Log.d(TAG, "file from Intent filename:"+fileToLoad);
211 | mCurrentFileName = fileToLoad;
212 | } else {
213 | if (mCurrentFileName != null) {
214 | Log.d(TAG, "have current filename:"+mCurrentFileName);
215 | } else {
216 | mCurrentFileName = "skpage" + now.getTime() + ".png";
217 | Log.d(TAG, "NEW filename:"+mCurrentFileName);
218 | }
219 | }
220 |
221 | this.colourMenuButton = (ImageButton) this.findViewById(R.id.penColourButton);
222 | this.colourMenuButton.setOnClickListener(new View.OnClickListener() {
223 |
224 | public void onClick(View v) {
225 | ColoursPopupWindow dw = new ColoursPopupWindow(v, sView);
226 | dw.showLikePopDownMenu();
227 | }
228 | });
229 |
230 | Log.d(TAG, " SKNotes - " + SKNotes.getMemUsageString());
231 | }
232 |
233 | /**
234 | * Extends {@link BetterPopupWindow}
235 | *
236 | * Overrides onCreate to create the view and register the button listeners
237 | *
238 | * @author qbert
239 | *
240 | */
241 | private static class ColoursPopupWindow extends BetterPopupWindow implements
242 | OnClickListener {
243 |
244 | //ref to view used to set current pen/pencil drawing colour
245 | private SketchView sView;
246 |
247 | public ColoursPopupWindow(View anchor, SketchView view) {
248 | super(anchor);
249 | this.sView = view;
250 | }
251 |
252 | @Override
253 | protected void onCreate() {
254 | // inflate layout
255 | LayoutInflater inflater = (LayoutInflater) this.anchor.getContext()
256 | .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
257 |
258 | ViewGroup root = (ViewGroup) inflater.inflate(
259 | R.layout.colours_popup, null);
260 |
261 | // setup button events
262 | for (int i = 0, icount = root.getChildCount(); i < icount; i++) {
263 | View v = root.getChildAt(i);
264 |
265 | if (v instanceof TableRow) {
266 | TableRow row = (TableRow) v;
267 |
268 | for (int j = 0, jcount = row.getChildCount(); j < jcount; j++) {
269 | View item = row.getChildAt(j);
270 | if (item instanceof Button) {
271 | Button b = (Button) item;
272 | b.setOnClickListener(this);
273 | }
274 | }
275 | }
276 | }
277 |
278 | // set the inflated view as what we want to display
279 | this.setContentView(root);
280 | }
281 |
282 |
283 | public void onClick(View v) {
284 | Button b = (Button) v;
285 |
286 | sView.setCurrentPenColour(b.getText().toString());
287 |
288 | //display a simple toast as a confirmation to user
289 | Toast.makeText(this.anchor.getContext(), b.getText(),
290 | Toast.LENGTH_SHORT).show();
291 |
292 | this.dismiss();
293 | }
294 | }
295 | }
296 |
--------------------------------------------------------------------------------
/src/com/manichord/sketchnotes/SketchView.java:
--------------------------------------------------------------------------------
1 | package com.manichord.sketchnotes;
2 |
3 | import java.io.File;
4 | import java.io.FileOutputStream;
5 | import java.util.ArrayList;
6 | import java.util.Date;
7 | import java.util.List;
8 |
9 | import android.app.Activity;
10 | import android.content.Context;
11 | import android.content.SharedPreferences;
12 | import android.content.SharedPreferences.Editor;
13 | import android.content.res.Resources;
14 | import android.graphics.Bitmap;
15 | import android.graphics.BitmapFactory;
16 | import android.graphics.Canvas;
17 | import android.graphics.Color;
18 | import android.graphics.Paint;
19 | import android.graphics.Paint.Style;
20 | import android.graphics.Path;
21 | import android.graphics.Point;
22 | import android.graphics.PorterDuff;
23 | import android.graphics.PorterDuffXfermode;
24 | import android.preference.PreferenceManager;
25 | import android.util.AttributeSet;
26 | import android.util.Log;
27 | import android.view.Display;
28 | import android.view.MotionEvent;
29 | import android.view.View;
30 | import android.view.View.OnClickListener;
31 | import android.view.View.OnTouchListener;
32 |
33 | public class SketchView extends View implements OnTouchListener, OnClickListener {
34 |
35 | private static final String TAG = "SketchView";
36 |
37 | List points = new ArrayList();
38 |
39 | private long mLastSaveTime;
40 |
41 | private int SAVE_DELAY = 30 * 1000; //30sec
42 |
43 | private Bitmap mBitmap;
44 | private Canvas mCanvas;
45 |
46 | private Bitmap mBackgroundBitmap;
47 | private Canvas mBackgroundCanvas;
48 |
49 | Path mCurrentPath;
50 |
51 | private final Paint mPagePainter;
52 | private final Paint mGridPainter;
53 | private final Paint mPenPainter;
54 | private final Paint mEraserPainter;
55 |
56 | int width = 0;
57 | int height = 0;
58 | int pass = 0;
59 |
60 | private boolean mUnsaved = false;
61 | private boolean mEraserMode = false;
62 |
63 | public SketchView(Context context) {
64 | this(context, null);
65 | }
66 |
67 | public SketchView(Context context, AttributeSet attrs) {
68 | super(context, attrs);
69 |
70 | setFocusable(true);
71 |
72 | String penSizePref = PreferenceManager.getDefaultSharedPreferences(context).getString("penSizePref", "medium");
73 |
74 | Integer penColour = PreferenceManager.getDefaultSharedPreferences(context).getInt("penColour", R.color.blue_pen);
75 |
76 | mPagePainter = new Paint();
77 | mPagePainter.setAntiAlias(true);
78 | mPagePainter.setColor(getResources().getColor(R.color.page_colour));
79 |
80 | mGridPainter = new Paint();
81 | mGridPainter.setColor(getResources().getColor(R.color.grid_colour));
82 | mGridPainter.setStyle(Style.STROKE);
83 |
84 | mPenPainter = new Paint();
85 | mPenPainter.setColor(penColour);
86 |
87 | int penSizeIdentifier = getResources().getIdentifier(penSizePref,
88 | "dimen",
89 | getContext().getPackageName());
90 |
91 | Log.d(TAG, penSizePref+"-> got IDENT:"+penSizeIdentifier);
92 |
93 | float penSize = getResources().getDimension(R.dimen.pen_medium);
94 | if (penSizeIdentifier != 0) {
95 | penSize = getResources().getDimension(penSizeIdentifier);
96 | }
97 |
98 | mPenPainter.setStrokeWidth(penSize);
99 | mPenPainter.setStyle(Style.STROKE);
100 |
101 | float mEraserWidth = getResources().getDimension(R.dimen.eraser_size);
102 |
103 | mEraserPainter = new Paint();
104 | mEraserPainter.setColor(Color.TRANSPARENT);
105 | mEraserPainter.setStrokeWidth(mEraserWidth);
106 | mEraserPainter.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
107 |
108 | mLastSaveTime = (new Date()).getTime();
109 | }
110 |
111 | public String getCurrentPenColour() {
112 | return "TODO";
113 | }
114 |
115 | public void setCurrentPenColour(String penColourname) {
116 | String colourIdentName = penColourname.toLowerCase().replace(" ", "_");
117 | int colourIdentifier = getResources().getIdentifier(colourIdentName,
118 | "color",
119 | getContext().getPackageName());
120 |
121 | Log.d(TAG, colourIdentName+"-> got IDENT:"+colourIdentifier);
122 | if (colourIdentifier != 0) {
123 | int penColour = getResources().getColor(colourIdentifier);
124 | Editor ed = PreferenceManager.getDefaultSharedPreferences(getContext()).edit();
125 | ed.putInt("penColour", new Integer(penColour));
126 | ed.commit();
127 | mPenPainter.setColor(penColour);
128 | } else {
129 | Log.d(TAG, "INVALID colourname:"+ colourIdentName+"-> got IDENT:"+colourIdentifier);
130 | }
131 | }
132 |
133 | @Override
134 | protected void onDraw(Canvas canvas) {
135 | if (mBackgroundCanvas != null) {
136 | canvas.drawBitmap(mBackgroundBitmap, 0, 0, null);
137 | } else {
138 | Log.e(TAG, "NO BACKGROUND BITMAP!");
139 | }
140 | if (mBitmap != null) {
141 | canvas.drawBitmap(mBitmap, 0, 0, null);
142 | } else {
143 | Log.e(TAG, "NO BITMAP!");
144 | }
145 | Date now = new Date();
146 | if (now.getTime() > (mLastSaveTime + SAVE_DELAY) ) {
147 | saveCurrentBitMap(((SKNotes)getContext()).getCurrentFileName());
148 | mLastSaveTime = now.getTime();
149 | }
150 | }
151 |
152 | @Override
153 | public boolean onTouchEvent(MotionEvent event) {
154 |
155 | float mCurX;
156 | float mCurY;
157 |
158 | ArrayList eraserpoints = new ArrayList();
159 |
160 | int action = event.getAction();
161 | if (action != MotionEvent.ACTION_UP
162 | && action != MotionEvent.ACTION_CANCEL) {
163 | //Log.d(TAG, "PEN UP");
164 | }
165 | if (action == MotionEvent.ACTION_DOWN) {
166 | //start recording points
167 | mCurrentPath = new Path();
168 | mCurrentPath.moveTo(event.getX(), event.getY());
169 | }
170 | if (action == MotionEvent.ACTION_MOVE) {
171 | //start recording points
172 | int N = event.getHistorySize();
173 | int P = event.getPointerCount();
174 | for (int i = 0; i < N; i++) {
175 | for (int j = 0; j < P; j++) {
176 | mCurX = event.getHistoricalX(j, i);
177 | mCurY = event.getHistoricalY(j, i);
178 |
179 | if (mEraserMode == true) {
180 | if (eraserpoints != null) {
181 | eraserpoints.add(new Point(Math.round(mCurX), Math.round(mCurY)));
182 | } else {
183 | Log.e(TAG, "no eraserpoints array defined, skipping adding erase point");
184 | }
185 | } else {
186 | if (mCurrentPath != null) {
187 | mCurrentPath.lineTo(mCurX, mCurY);
188 | } else {
189 | Log.e(TAG, "NO PATH TO ADD POINT"+mCurX+","+mCurY);
190 | }
191 | }
192 | }
193 | }
194 |
195 | if (mCurrentPath != null) {
196 | mCurrentPath.lineTo(event.getX(), event.getY());
197 | mCanvas.drawPath(mCurrentPath, (mEraserMode == true ? mEraserPainter : mPenPainter));
198 | invalidate();
199 | } else {
200 | Log.e(TAG, "Missing CurrentPath object");
201 | }
202 | }
203 | mUnsaved = true;
204 | return true;
205 | }
206 |
207 |
208 |
209 | public boolean onTouch(View v, MotionEvent event) {
210 | return false;
211 | }
212 |
213 | @Override
214 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
215 |
216 | Log.i(TAG, "Size changed w:" + w + " h:" + h);
217 |
218 | createNewDrawingCanvasAndBitMap(w,h);
219 |
220 | drawPageGridOrLines(w,h);
221 | loadBitMap(((SKNotes)getContext()).getCurrentFileName());
222 | }
223 |
224 | public void clear() {
225 | int curW = mBitmap != null ? mBitmap.getWidth() : 0;
226 | int curH = mBitmap != null ? mBitmap.getHeight() : 0;
227 |
228 | createNewDrawingCanvasAndBitMap(curW, curH);
229 | drawPageGridOrLines(curW, curH);
230 | invalidate();
231 | }
232 |
233 | private void createNewDrawingCanvasAndBitMap(int w, int h) {
234 | Bitmap newBitmap = Bitmap.createBitmap(w, h,
235 | Bitmap.Config.ARGB_8888);
236 | Canvas newCanvas = new Canvas();
237 | newCanvas.setBitmap(newBitmap);
238 |
239 | if (mBitmap != null) {
240 | mBitmap.recycle(); //tell android to clear up prev bitmap
241 | }
242 |
243 | mBitmap = newBitmap;
244 | mCanvas = newCanvas;
245 | }
246 |
247 | private void drawPageGridOrLines(int w, int h) {
248 |
249 | Resources res = getResources();
250 | int xpos = Math.round(res.getDimension(R.dimen.grid_size));
251 | int ypos = Math.round(res.getDimension(R.dimen.grid_size));
252 |
253 | Bitmap newBackgroundBitmap = Bitmap.createBitmap(w, h,
254 | Bitmap.Config.RGB_565);
255 | Canvas newBackgroundCanvas = new Canvas();
256 | newBackgroundCanvas.setBitmap(newBackgroundBitmap);
257 |
258 |
259 | if (mBackgroundBitmap != null) {
260 | newBackgroundCanvas.drawBitmap(mBackgroundBitmap, 0, 0, null);
261 | }
262 | mBackgroundBitmap = newBackgroundBitmap;
263 | mBackgroundCanvas = newBackgroundCanvas;
264 |
265 | // make the entire canvas page colour
266 | mBackgroundCanvas.drawPaint(mPagePainter);
267 |
268 | // Draw Background Grid
269 | Display display = ((Activity)getContext()).getWindowManager().getDefaultDisplay();
270 | width = display.getWidth();// start
271 | height = display.getHeight();// end
272 |
273 | SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getContext());
274 | String pageBackgroundPref = prefs.getString("pageBackgroundPref", "Grid");
275 |
276 | //lines only
277 | if (!pageBackgroundPref.equals("None")) {
278 | for (int i = 0; i < height; i += ypos) {
279 | mBackgroundCanvas.drawLine(0, i, width, i, mGridPainter);
280 | }
281 | }
282 |
283 | if (pageBackgroundPref.equals("Grid")) {
284 | // add for square graph paper:
285 | for (int i = 0; i < width; i += xpos) {
286 | mBackgroundCanvas.drawLine(i, 0, i, height, mGridPainter);
287 | }
288 | }
289 | }
290 |
291 |
292 |
293 | public void saveCurrentBitMap(String filename) {
294 | if (!mUnsaved) {
295 | return; //do nothing if no unsaved changes pending
296 | }
297 | try {
298 | FileOutputStream out = ((Activity)getContext()).openFileOutput(filename,
299 | Context.MODE_PRIVATE);
300 | mBitmap.compress(Bitmap.CompressFormat.PNG, 99, out); //note PNG lossless
301 | mUnsaved = false;
302 | Log.i(TAG, "saved page:"+filename);
303 | } catch (Exception e) {
304 | e.printStackTrace();
305 | }
306 | }
307 |
308 | public void loadBitMap(String filename) {
309 | Bitmap loadedBM = BitmapFactory.decodeFile(((Activity)getContext()).getFilesDir()+File.separator+filename);
310 | if (loadedBM != null) {
311 | Log.i(TAG, "decoded:"
312 | + loadedBM.getHeight());
313 | mCanvas.drawBitmap(loadedBM, 0, 0, null);
314 | mUnsaved = false;
315 | invalidate();
316 | } else {
317 | Log.e(TAG, "bitmap file not found!");
318 | }
319 | }
320 |
321 |
322 | public void onClick(View v) {
323 | //handle button clicks for pen/pencolour/eraser
324 | int selectedBg = getResources().getColor(R.color.selected_button_background);
325 | int unselectedBg = getResources().getColor(R.color.unselected_button_background);
326 |
327 | switch(v.getId()) {
328 | case R.id.eraserButton:
329 | mEraserMode = true;
330 | v.setBackgroundColor(selectedBg);
331 | ((View)v.getParent()).findViewById(R.id.penButton).setBackgroundColor(unselectedBg);
332 | break;
333 |
334 | case R.id.penButton:
335 | mEraserMode = false;
336 | v.setBackgroundColor(selectedBg);
337 | ((View)v.getParent()).findViewById(R.id.eraserButton).setBackgroundColor(unselectedBg);
338 | break;
339 |
340 | case R.id.penColourButton:
341 |
342 | break;
343 | }
344 | }
345 |
346 | public void nullBitmaps() {
347 | mCanvas = null;
348 | if (mBitmap != null) {
349 | mBitmap.recycle();
350 | }
351 | mBitmap = null;
352 | mBackgroundCanvas = null;
353 | if (mBackgroundBitmap != null) {
354 | mBackgroundBitmap.recycle();
355 | }
356 | mBackgroundBitmap = null;
357 | }
358 |
359 | }
360 |
--------------------------------------------------------------------------------