3

I have a code that get the coordinate of image view, and i want to draw a line when the user touch 2 times in the imageview. The imageview has a bitmap from drawable. for more detail this is my code :

public class MainActivity extends Activity implements OnTouchListener {
   ImageView tampil;
   Matrix matrix = new Matrix();
   Matrix savedMatrix = new Matrix();
   private static final String TAG = "Touch";
   static final int NONE = 0;
   static final int DRAG = 1;
   static final int ZOOM = 2;
   int mode = NONE;

   // Remember some things for zooming
   PointF start = new PointF();
   PointF mid = new PointF();
   float oldDist = 1f;
   float x1, y1, x2, y2;
   Bitmap bmp;
   int i = 1;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tampil = (ImageView)findViewById(R.id.imageView1);
    tampil.setOnTouchListener(this);
    Drawable d = getResources().getDrawable(R.drawable.enigma);
    bmp = ((BitmapDrawable)d).getBitmap();
    tampil.setImageBitmap(bmp);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    //ImageView view = (ImageView) v;

      // Handle touch events here...
      switch (event.getAction() & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
         savedMatrix.set(matrix);
         start.set(event.getX(), event.getY());
         Log.d(TAG, "mode=DRAG");
         mode = DRAG;
         Log.i(TAG, "("+String.valueOf((int)event.getX())+","+String.valueOf((int)event.getY())+")");
         if (i==1){
             x1 = event.getX();
             y1 = event.getY();
             i = 2;
             Log.i(TAG, "coordinate x1 : "+String.valueOf(x1)+" y1 : "+String.valueOf(y1));
         } else if (i==2){
             x2 = event.getX();
             y2 = event.getY();
             i = 3;
             Log.i(TAG, "coordinate x2 : "+String.valueOf(x2)+" y2 : "+String.valueOf(y2));
             onDraw();
         } 
        break;
      }
    return true;
}

public void onDraw(){
    Bitmap tes = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.RGB_565);
    tes = bmp;
    Canvas cvs = new Canvas(tes);
    Paint pnt = new Paint();
    pnt.setColor(Color.RED);
    Log.i(TAG, "draw line");
    cvs.drawLine(x1, y1, x2, y2, pnt);
    i=1;
    tampil.setImageBitmap(tes);
}
}

but when the user touch 2 time, the aplication has not draw the line.. anyone can give a solution

this is the log cat say :

10-17 12:08:21.082: I/ActivityManager(70): Displayed com.example.tesontouch/.MainActivity: +3s139ms
  10-17 12:08:24.103: D/Touch(796): mode=DRAG
  10-17 12:08:24.103: I/Touch(796): (113,145)
  10-17 12:08:24.123: I/Touch(796): coordinate x1 : 113.26723 y1 : 145.2766
  10-17 12:08:26.243: D/dalvikvm(264): GC_EXPLICIT freed 8K, 55% free 2595K/5703K, external 1625K/2137K, paused 88ms
  10-17 12:08:26.323: D/Touch(796): mode=DRAG
  10-17 12:08:26.323: I/Touch(796): (306,284)
  10-17 12:08:26.343: I/Touch(796): coordinate x2 : 306.67017 y2 : 284.45056
  10-17 12:08:26.423: D/dalvikvm(796): GC_EXTERNAL_ALLOC freed 4K, 53% free 2559K/5379K, external 3064K/4342K, paused 81ms
  10-17 12:08:26.473: I/Touch(796): draw line
  10-17 12:08:31.413: D/dalvikvm(473): GC_EXPLICIT freed 6K, 52% free 2697K/5511K, external 3502K/4752K, paused 183ms
Kara
  • 6,115
  • 16
  • 50
  • 57
adi.zean
  • 1,085
  • 3
  • 12
  • 15

1 Answers1

7

this is the correct working code :)

package com.example.stackdrawline;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class StackActivity extends Activity implements OnTouchListener {
ImageView tampil;
   Matrix matrix = new Matrix();
   Matrix savedMatrix = new Matrix();
   private static final String TAG = "Touch";
   static final int NONE = 0;
   static final int DRAG = 1;
   static final int ZOOM = 2;
   int mode = NONE;

   // Remember some things for zooming
   PointF start = new PointF();
   PointF mid = new PointF();
   float oldDist = 1f;
   float x1, y1, x2, y2;
   Bitmap bmp;
   int i = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stack);
    tampil = (ImageView)findViewById(R.id.imageView1);
    tampil.setOnTouchListener(this);
    Drawable d = getResources().getDrawable(R.drawable. enigma);
    bmp = ((BitmapDrawable)d).getBitmap();
    tampil.setImageBitmap(bmp);

}

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
         savedMatrix.set(matrix);
         start.set(event.getX(), event.getY());
         Log.d(TAG, "mode=DRAG");
         mode = DRAG;
         Log.i(TAG, "("+String.valueOf((int)event.getX())+","+String.valueOf((int)event.getY())+")");
         if (i==1){
             x1 = event.getX();
             y1 = event.getY();
             i = 2;
             Log.i(TAG, "coordinate x1 : "+String.valueOf(x1)+" y1 : "+String.valueOf(y1));
         } else if (i==2){
             x2 = event.getX();
             y2 = event.getY();
             i = 3;
             Log.i(TAG, "coordinate x2 : "+String.valueOf(x2)+" y2 : "+String.valueOf(y2));
             onDraw();
         } 
        break;
      }
    return true;
}
public void onDraw(){
    bmp = Bitmap.createBitmap(tampil.getWidth(), tampil.getHeight(), Config.ARGB_8888);
    Canvas c = new Canvas(bmp);
    tampil.draw(c);

    Paint pnt = new Paint();
    pnt.setColor(Color.RED);
    c.drawLine(x1, y1, x2, y2, pnt);
    tampil.setImageBitmap(bmp);
    i = 1;
}

}
Amit Hooda
  • 2,133
  • 3
  • 23
  • 37
  • whoaaa.... thanks Amit Hooda, you realy help me.. :D but one question, why the size of bmp must same as the ImageView tampil?? – adi.zean Oct 18 '12 at 00:27
  • 1
    because on we wnt the image to be of same size as it was earlier, we dont need to resize original image,you can try hardcoding values of tampil.getwidth and tampil.getHeight and check wat happens :) – Amit Hooda Oct 18 '12 at 05:53
  • Thinking of how can I do it for the same with ArrowLine – LOG_TAG Oct 10 '14 at 04:49
  • @AmitHooda can you help me in drawing the arrowline http://stackoverflow.com/questions/26292752/how-to-draw-arrowline-in-android-graphics-path-with-correct-configerations using Path – LOG_TAG Oct 10 '14 at 06:17