0

我有兩個imageview,其中我通過使用setImageBitmap添加圖像,並將setOnTouchListener應用到他們。但這裏的問題是,當第一次添加第一個img時,它的移動通過觸摸但是當我添加第二個img時,第二個img移動但在此之後,我無法通過觸摸首先移動img。對不起,英語和先進的感謝。這是我的代碼: -移動兩個圖像independentaly android

working_bitmap = BitmapFactory.decodeFile(file_location+"cropped_image.jpg"); 

    outBitmap=Bitmap.createBitmap(
    working_bitmap.getWidth(), 
    working_bitmap.getHeight(),Bitmap.Config.ARGB_8888);  
    Canvas canvas1 = new Canvas(outBitmap); 
    canvas1.drawBitmap(working_bitmap, new Matrix(), null); 
    if(downloaded_shirt_image != null) 
    { 
     int width = downloaded_shirt_image.getWidth(); 
    int height = downloaded_shirt_image.getHeight(); 
    int halfWidth = width/3; 
    int halfHeight = height/3; 
    //Half Scaled 
    Bitmap bmHalf = Bitmap.createScaledBitmap(downloaded_shirt_image, 
      halfWidth, halfHeight, false); 
    proimg =(ImageView)findViewById(R.id.pro_img); 
    proimg.setDrawingCacheEnabled(true); 
    proimg.setImageBitmap(bmHalf); 
    proimg.setVisibility(View.VISIBLE); 
    proimg.setOnTouchListener(new View.OnTouchListener() 
    { 
    public boolean onTouch(View v, MotionEvent event) { 
    ImageView view = (ImageView) v; 
    int rotation = 25; 
    // Dump touch event to log 
    dumpEvent(event); 
    // 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; 
     proimg.invalidate(); 
     break; 
     case MotionEvent.ACTION_MOVE: 
    if (mode == DRAG) { 
    // ... 
                          matrix.set(savedMatrix);             matrix.postTranslate(event.getX() - start.x, 
    event.getY() - start.y); 
    } 
    else if (mode == ZOOM) { 
    float newDist = spacing(event); 
    Log.d(TAG, "newDist=" + newDist); 
    if (newDist > 10f) { 
    matrix.set(savedMatrix); 
    float scale = newDist/oldDist; 
    matrix.postScale(scale, scale, mid.x, mid.y); 
    } 
    } 
    proimg.invalidate(); 
    break; 
    } 
    view.setImageMatrix(matrix); 
    return true; // indicate event was handled 
    } 
    }); 
    canvas1.drawBitmap(downloaded_shirt_image, 50, 192, null); 
    } 

    if(downloaded_pant_image != null) 
    { 
     int width = downloaded_pant_image.getWidth(); 
     int height = downloaded_pant_image.getHeight(); 
     int halfWidth = width/3; 
     int halfHeight = height/3; 
     //Half Scaled 
     Bitmap bmHalf = Bitmap.createScaledBitmap(downloaded_pant_image, 
         halfWidth, halfHeight, false); 
     pantimg =(ImageView)findViewById(R.id.pro_img_down); 
     pantimg.setDrawingCacheEnabled(true); 
     pantimg.setImageBitmap(bmHalf); 
     pantimg.setVisibility(View.VISIBLE); 
     pantimg.setOnTouchListener(new View.OnTouchListener() 
     { 
     public boolean onTouch(View v, MotionEvent event) { 
     ImageView view = (ImageView) v; 
     int rotation = 25; 
     // Dump touch event to log 
     dumpEvent(event); 
     // 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; 
     pantimg.invalidate(); 
     break; 
    case MotionEvent.ACTION_MOVE: 
     if (mode == DRAG) { 
     // ... 

    matrix.set(savedMatrix);            matrix.postTranslate(event.getX() - start.x, 
    event.getY() - start.y); 
    } 
    else if (mode == ZOOM) { 
    float newDist = spacing(event); 
    Log.d(TAG, "newDist=" + newDist); 
    if (newDist > 10f) { 
    matrix.set(savedMatrix); 
    float scale = newDist/oldDist; 
    matrix.postScale(scale, scale, mid.x, mid.y); 
    } 
    } 
    pantimg.invalidate(); 
    break; 
    } 
    view.setImageMatrix(matrix); 
    return true; // indicate event was handled 
    } 
    }        
    ); 
    canvas1.drawBitmap(downloaded_pant_image, 110, 565, null); 
    } 
+0

你好,如果你得到解決方案如何在孔屏幕上移動兩個圖像,請幫助我。 –

回答

0

請嘗試下面的代碼,並使用Imageview而不是Textview。

windowwidth = getWindowManager().getDefaultDisplay().getWidth(); 
windowheight = getWindowManager().getDefaultDisplay().getHeight(); 


tv1 = (TextView)findViewById(R.id.text_view1); 
tv1.setOnTouchListener(new View.OnTouchListener() {   

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     layoutParams1 = (RelativeLayout.LayoutParams) tv1.getLayoutParams(); 
     switch(event.getActionMasked()) 
     { 
      case MotionEvent.ACTION_DOWN: 
       break; 
      case MotionEvent.ACTION_MOVE: 
       int x_cord = (int) event.getRawX(); 
       int y_cord = (int) event.getRawY(); 
       if (x_cord > windowwidth) { 
        x_cord = windowwidth; 
       } 
       if (y_cord > windowheight) { 
        y_cord = windowheight; 
       } 
       layoutParams1.leftMargin = x_cord - 25; 
       layoutParams1.topMargin = y_cord - 75; 
       tv1.setLayoutParams(layoutParams1); 
       break; 
      default: 
       break; 
     } 
     return true; 
    } 
}); 

tv2 = (TextView)findViewById(R.id.text_view2); 
tv2.setTextColor(Color.MAGENTA); 
tv2.setOnTouchListener(new View.OnTouchListener() {   

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     layoutParams2 = (RelativeLayout.LayoutParams) tv2.getLayoutParams(); 
     switch(event.getActionMasked()) 
     { 
      case MotionEvent.ACTION_DOWN: 
       break; 
      case MotionEvent.ACTION_MOVE: 
       int x_cord = (int) event.getRawX(); 
       int y_cord = (int) event.getRawY(); 
       if (x_cord > windowwidth) { 
        x_cord = windowwidth; 
       } 
       if (y_cord > windowheight) { 
        y_cord = windowheight; 
       } 
       layoutParams2.leftMargin = x_cord - 25; 
       layoutParams2.topMargin = y_cord - 75; 
       tv2.setLayoutParams(layoutParams2); 
       break; 
      default: 
       break; 
     } 
     return true; 
    } 
}); 
+0

它是什麼類型的「layoutParams1」? – Rohit13

+0

聲明RelativeLayout.LayoutParams layoutParams1;和RelativeLayout.LayoutParams layoutParams2;在你的活動中。 –

+0

是兩個圖像獨立移動,但問題是我們無法在整個屏幕上移動圖像,而只是在tv2.setLayoutParams(layoutParams2)中定義的特定區域上移動;是任何方式整個屏幕上移動 – Rohit13