Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
What I want to do is draw a line that will follow my finger on android phone...How can i do this? Please help
I work in Eclipse.
Posted
Updated 19-Jun-18 18:28pm
Comments
ridoy 19-Aug-12 13:02pm    
This is not code giving place..you need to show us what you have tried or what problem you face?

Draw line on finger touch
There are many application in Android you can use to draw something on screen. This is a simple application which will draw straight line on screen between touch you have started and the last point of your touch.

This application will use Bitmap, Canvas, and Paint class.

Bitmap class covers some common techniques for processing and loading Bitmap objects in a way that keeps your user interface (UI) components responsive and avoids exceeding your application memory limit.

Canvas class holds the DRAW calls. To draw something, you need four basic components: A bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Circle), and a paint (to describe the colours and styles for the drawing).

Paint class holds the style and colour information about how to draw geometries, text and bitmaps.

1. Design Screen:

activity_touch_draw.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />
 
</RelativeLayout>


An ImageView will be used as drawing board and your finger will work as pencil. A straight line will drawn from starting touch to ending point where user pull up finger.

TouchDraw.java
package app.test;
 
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
 
public class Test extends Activity implements OnTouchListener {
  ImageView imageView;
  Bitmap bitmap;
  Canvas canvas;
  Paint paint;
  float downx = 0, downy = 0, upx = 0, upy = 0;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 
    imageView = (ImageView) this.findViewById(R.id.ImageView);
 
    Display currentDisplay = getWindowManager().getDefaultDisplay();
    float dw = currentDisplay.getWidth();
    float dh = currentDisplay.getHeight();
 
    bitmap = Bitmap.createBitmap((int) dw, (int) dh,
        Bitmap.Config.ARGB_8888);
    canvas = new Canvas(bitmap);
    paint = new Paint();
    paint.setColor(Color.GREEN);
    imageView.setImageBitmap(bitmap);
 
    imageView.setOnTouchListener(this);
  }
 
  public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
      downx = event.getX();
      downy = event.getY();
      break;
    case MotionEvent.ACTION_MOVE:
      break;
    case MotionEvent.ACTION_UP:
      upx = event.getX();
      upy = event.getY();
      canvas.drawLine(downx, downy, upx, upy, paint);
      imageView.invalidate();
      break;
    case MotionEvent.ACTION_CANCEL:
      break;<a href=""></a>[<a href="" target="_blank">^</a>]
    default:
      break;
    }
    return true;
  }
}



First of all you'll need to know what is width and height of your screen. You'll need Display class object to get detail about screen. Display class provides information about the display size and density.

Display currentDisplay = getWindowManager().getDefaultDisplay();
float dw = currentDisplay.getWidth();
float dh = currentDisplay.getHeight();


currentDisplay object will provider display width and height.
bitmap object will of provided width and height.
canvas object will used to class draw functions. Your actual line will draw on canvas.
paint object will actually a line on the canvas.

Finally, user will draw on screen line by touching ImageView object. So, I have set OnTouchListener on ImageView object.

OnTouchListener is an interface definition for a callback to be invoked when a touch event is dispatched to the view.

onTouch method is called when a touch event is dispatched to a view. This allows a listeners to get chance to respond before the target view.
public boolean onTouch(View v, MotionEvent event)

v is object of View class from where touch is dispatched.
event is object of MotionEvent containing full information about event


int action = event.getAction();


getAction() return the kind of action being performed. During entire process of drawing a line on touch many actions will be called like.

ACTION_DOWN
ACTION_MOVE
ACTION_CANCEL
ACTION_UP

So, I need to draw a line starting from action called ACTION_DOWN up to the action ACTION_UP. I have taken starting X & Y co-ordinate where ACTION_DOWN action called and again I have retrieved ending X & Y co-ordinate when ACTION_UP action called. Finally, I have used drawLine() method of Canvas class to draw straight line.
 
Share this answer
 
Comments
Maciej Los 20-Jun-18 4:44am    
6 years too late!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900