Reputation: 403
I have an ImageView with a background image. The ImageView is sized programmatically as a percentage of the screen. The view is in a RelativeLayout and it is at the center of it. What I want is to make it drawable. I want to be able to draw in it with my finger (just like we were drawing nonsense in paint as we were kids). Also when I'm moving my finger I want the path to be drawn immediately. Can you give me an example of how to do it?
Upvotes: 0
Views: 2314
Reputation: 10876
Just take GestureOverlayView and set your image as background of this OverlayView.
<android.gesture.GestureOverlayView
android:id="@+id/signaturePad"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:background="@color/white"
android:eventsInterceptionEnabled="true"
android:fadeEnabled="false"
android:gestureColor="@color/black"
android:gestureStrokeLengthThreshold="0.1"
android:gestureStrokeType="multiple"
android:orientation="vertical" >
</android.gesture.GestureOverlayView>
and function to save image from GestureOverlayView.
try {
GestureOverlayView gestureView = (GestureOverlayView) findViewById(R.id.signaturePad);
gestureView.setDrawingCacheEnabled(true);
Bitmap bm = Bitmap.createBitmap(gestureView.getDrawingCache());
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "signature.png");
f.createNewFile();
FileOutputStream os = new FileOutputStream(f);
os = new FileOutputStream(f);
//compress to specified format (PNG), quality - which is ignored for PNG, and out stream
bm.compress(Bitmap.CompressFormat.PNG, 100, os);
os.close();
} catch (Exception e) {
Log.v("Gestures", e.getMessage());
e.printStackTrace();
}
follow this Example
Upvotes: 1
Reputation: 1326
You would have to override the ImageView's onTouchEvent
and onDraw
calls. Get the user's touch position in onTouchEvent
and draw to the ImageView
's canvas
.
See example here -
paint canvas android
Upvotes: 0
Reputation: 46
There are some very good references to drawing apps, found with a small amount of Googling:
Simple App to get a good wet finger / easy to implement: http://v4all123.blogspot.com/2013/11/simple-drawing-example-in-android.html
More Complicated, but very thorough explanation: http://code.tutsplus.com/tutorials/android-sdk-create-a-drawing-app-touch-interaction--mobile-19202
Neither are using a ImageView, but I'm sure you could accommodate switching View Types
Upvotes: 1