2012-06-09 42 views
0

我有兩個imageview,其中我通過使用setImageBitmap添加圖像,並且也將setOnTouchListener應用於它們。但這裏的問題是,當第一次添加第一個img時,它的移動通過觸摸但是當我添加第二個img時,第二個img移動但在此之後,我無法通過觸摸首先移動img。對不起,英語和先進的感謝。兩個imageview與單獨的onTouch事件

+0

我認爲你企圖觸摸移動圖像我說的對,並具有u在絕對佈局中使用或不先告訴我比或什麼ü已在使用基於我會回答 – Khan

+0

@khan我都ImageView的是在相對佈局,我想要移動兩個圖像取決於哪些img是由用戶觸摸 – Rohit13

+0

但一個闕是在哪個基地u移動你的圖像使用重繪圖像觸摸或設置佈局參數在觸摸移動 – Khan

回答

0

你必須給onTouchListener兩個圖像seperately作爲

ImageView first_image=new ImageView(this); 
    first_image.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
       if(event.getAction()==MotionEvent.ACTION_UP) 
       { 

       } 
       if(event.getAction()==MotionEvent.ACTION_DOWN) 
       { 

       } 
       if(event.getAction()==MotionEvent.ACTION_MOVE) 
       { 
        //Code for image moving 
       } 
      return false; 
     } 
    }); 


     ImageView second_image=new ImageView(this); 
    second_image.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
       if(event.getAction()==MotionEvent.ACTION_UP) 
       { 

       } 
       if(event.getAction()==MotionEvent.ACTION_DOWN) 
       { 

       } 
       if(event.getAction()==MotionEvent.ACTION_MOVE) 
       { 
        //code for image moving 
       } 
      return false; 
     } 
    }); 
+0

我已申請單獨的活動,但不能正常工作 – Rohit13

+0

可否請您發佈您的代碼,以便我們瞭解您所做的事情以及您的問題是什麼? – deepa

0

請嘗試爲下面的代碼,它可以幫助你,而不是使用TextView的ImageView的。

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; 
    } 
});