Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8720385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:01:45+00:00 2026-06-13T07:01:45+00:00

I have a code that get the coordinate of image view, and i want

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:01:46+00:00Added an answer on June 13, 2026 at 7:01 am

    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;
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that gets the current logged in user. userID = request.session.get(_auth_user_id)
I have code which will draw a graph that scales if the user attempts
I have a code that should get a LinearLayout (with more LinearLayout's inside), and
so i have code that reads from an AVAsset using kCVPixelFormat_32BGRA. I get the
I have some code that at one part will get executed a lot, and
I have following code that does not work: I never get to goToFoodDetail .
I have a piece of code (below) that can get the text of an
I have some code in method C that will get executed based upon the
Trying to get comfortable with jQuery and I have encountered some sample code that
I have a piece of code of my application that's I get the error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.