2013-07-21 175 views
-1

林對位圖繪畫提出申請,並對其進行了詢問之前,所以像herehere離奇觸摸座標

我用他們我的應用程序內,似乎沒有工作了好幾次,這裏的代碼我有使用:現在

public class EditActivity extends Activity implements OnTouchListener, 
     OnClickListener { 
    GestureDetector gd; 
    View.OnTouchListener gl; 
    RelativeLayout parent; 
    ImageView im; 
    Bitmap bmp; 
    Bitmap alteredBitmap; 
    Canvas canvas; 
    Paint paint; 
    Matrix matrix; 
    Rect imageBounds; 
    Path mPath; 
    int dw; 
    int dh; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_edit); 
     parent = (RelativeLayout) findViewById(R.id.rl); 


     matrix = new Matrix(); 
     parent.setBackgroundColor(Color.BLACK); 
     gd = new GestureDetector(this, new MyGestureDetector()); 
     gl = new View.OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 

       float x = event.getX(); 
       float y = event.getY(); 

       int action = event.getAction(); 

        switch (action) { 

        case MotionEvent.ACTION_DOWN: 

         mPath.moveTo(x,y); 
         im.invalidate(); 
         break; 
        case MotionEvent.ACTION_MOVE: 
         mPath.lineTo(x,y); 
         im.invalidate(); 
         break; 
        case MotionEvent.ACTION_UP: 
         // canvas.drawPath(mPath, paint); 
         mPath.reset(); 
         im.invalidate(); 
         break; 
        case MotionEvent.ACTION_CANCEL: 
         break; 
        default: 
         break; 
        } 
        return gd.onTouchEvent(event); 
       } else { 
        return gd.onTouchEvent(event); 
       } 
      } 
     }; 

     im = (ImageView) findViewById(R.id.ImageView); 

     Uri imageFileUri = Uri.parse(getIntent().getStringExtra("uri")); 
     Display currentDisplay = getWindowManager().getDefaultDisplay(); 

     dw = currentDisplay.getWidth(); 

     dh = currentDisplay.getHeight(); 

     try { 
      // Load up the image's dimensions not the image itself 
      BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 

      bmpFactoryOptions.inJustDecodeBounds = false; 
      bmp = BitmapFactory.decodeStream(getContentResolver() 
        .openInputStream(imageFileUri), null, bmpFactoryOptions); 

      alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), 
        bmp.getHeight(), bmp.getConfig()); 

      mPath = new Path(); 
      canvas = new Canvas(alteredBitmap); 
      paint = new Paint(Paint.SUBPIXEL_TEXT_FLAG); 
      paint.setFlags(Paint.DEV_KERN_TEXT_FLAG); 
      paint.setStyle(Paint.Style.FILL); 
      paint.setColor(Color.WHITE); 
      paint.setFilterBitmap(true); 
      paint.setDither(true); 
      paint.setAntiAlias(true); 
      paint.setTextSize(40f); 

      paint.setTypeface(getTypeFaceFromFile("Roboto-Light")); 

      canvas.drawBitmap(bmp, matrix, paint); 
      canvas.save(); 
      im.setImageBitmap(alteredBitmap); 
      im.setOnClickListener(this); 
      im.setOnTouchListener(gl); 

     } catch (FileNotFoundException e) { 
      Log.v("ERROR", e.toString()); 
     } 

    } 

    class MyGestureDetector extends SimpleOnGestureListener { 

     @Override 
     public boolean onDown(MotionEvent event) { 

      return super.onDown(event); 
     } 

     @Override 
     public boolean onSingleTapConfirmed(MotionEvent e) { 

      return super.onSingleTapConfirmed(e); 
     } 

     @Override 
     public boolean onDoubleTap(MotionEvent e) { 

      return super.onDoubleTap(e); 
     } 

     @Override 
     public void onLongPress(MotionEvent e) { 

      super.onLongPress(e); 
     } 

     @Override 
     public boolean onDoubleTapEvent(MotionEvent e) { 

      return super.onDoubleTapEvent(e); 
     } 

    } 

} 

,我通過觸摸屏幕上繪製的路徑,它只是繪製自己的其他地方,也只發生在使用Android本身已縮放圖像,而不是它足夠小圖片適合不結垢,請查看一下,幫我

編輯: 我已經遵循了這一answer,這是實現後我的代碼,但現在世界上沒有路徑....

 gl = new View.OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 

       float x = event.getX(); 
       float y = event.getY(); 


       float scaledImageOffsetX = x - imageBounds.left; 
       float scaledImageOffsetY = y - imageBounds.top; 

       float originalImageOffsetX = scaledImageOffsetX * widthRatio; 
       float originalImageOffsetY = scaledImageOffsetY * heightRatio; 
       int action = event.getAction(); 

        switch (action) { 

        case MotionEvent.ACTION_DOWN: 

         mPath.moveTo(originalImageOffsetX,originalImageOffsetY); 
         im.invalidate(); 
         break; 
        case MotionEvent.ACTION_MOVE: 
         mPath.lineTo(originalImageOffsetX,originalImageOffsetY); 
         im.invalidate(); 
         break; 
        case MotionEvent.ACTION_UP: 
         // canvas.drawPath(mPath, paint); 
         mPath.reset(); 
         im.invalidate(); 
         break; 

        default: 
         break; 
        } 
        return true; 
       } 

      } 
     }; 

     im = (ImageView) findViewById(R.id.ImageView); 
     Uri imageFileUri = Uri.parse(getIntent().getStringExtra("uri")); 
     Display currentDisplay = getWindowManager().getDefaultDisplay(); 
     dw = currentDisplay.getWidth(); 
     dh = currentDisplay.getHeight(); 

     try { 

      BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 

      bmpFactoryOptions.inJustDecodeBounds = false; 
      bmp = BitmapFactory.decodeStream(getContentResolver() 
        .openInputStream(imageFileUri), null, bmpFactoryOptions); 

      alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), 
        bmp.getHeight(), bmp.getConfig()); 

      mPath = new Path(); 
      canvas = new Canvas(alteredBitmap); 
      paint = new Paint(Paint.SUBPIXEL_TEXT_FLAG); 
      paint.setFlags(Paint.DEV_KERN_TEXT_FLAG); 
      paint.setStyle(Paint.Style.FILL); 
      paint.setColor(Color.WHITE); 
      paint.setFilterBitmap(true); 
      paint.setDither(true); 
      paint.setAntiAlias(true); 
      paint.setTextSize(40f); 

      paint.setTypeface(getTypeFaceFromFile("Roboto-Light")); 

      canvas.drawBitmap(bmp, matrix, paint); 
      canvas.save(); 
      im.setImageBitmap(alteredBitmap); 
      Drawable drawable = im.getDrawable(); 
      imageBounds = drawable.getBounds(); 

      float intrinsicHeight = drawable.getIntrinsicHeight(); 
      float intrinsicWidth = drawable.getIntrinsicWidth();; 

      float scaledHeight = imageBounds.height(); 
      float scaledWidth = imageBounds.width(); 

      heightRatio = intrinsicHeight/scaledHeight; 
      widthRatio = intrinsicWidth/scaledWidth; 
      im.setOnClickListener(this); 
      im.setOnTouchListener(gl); 

     } catch (FileNotFoundException e) { 
      Log.v("ERROR", e.toString()); 
     } 

    } 

} 

感謝, 截拳道

+0

您需要作爲第一個答案說明這個問題縮放座標:http://stackoverflow.com/questions/4933612/how-to-convert-coordinates-of-the-image-view-to-the-coordinates-of-該位圖 – ethan

+0

我跟着它,我認爲這與我的問題第一行還可以,但觸及 – Jeet

+0

工作,同時,在這個問題的答案,設置originalImageOffsetX和originalImageOffsetY顯示什麼?你的問題是幾乎可以肯定的需要轉換或縮放通過的TouchEvent賦予與圖像上的座標對準座標引起的。 – Jeet

回答

-1

好吧,我有通過自己修補找到了答案,我希望它對別人有用

這是我遵循的步驟

1)計算原始的寬度和縮放寬度 2)找到縮放比例 3)乘以縮放比例,以本次活動的X和Y座標,你可以得到理想的座標...