2012-06-14 53 views
3

我正在處理項目用戶將圖像在一個位置移動到屏幕上的另一個位置。我寫了一個示例代碼來移動圖像,但這裏的問題是如果我移動一個圖像,相鄰圖像也開始移動。以下是示例代碼。任何一個想法。Android將圖像拖放到屏幕上?

Main.java

public class MainActivity extends Activity { 
    int windowwidth; 
    int windowheight;  
    ImageView ima1,ima2; 

    private android.widget.RelativeLayout.LayoutParams layoutParams ; 
    // private android.widget.RelativeLayout.LayoutParams layoutParams ; 
    //private android.widget.RelativeLayout.LayoutParams layoutParams ;   

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

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

     System.out.println("width" +windowwidth); 
     System.out.println("height" +windowheight);    

     ima1 = (ImageView)findViewById(R.id.imageview1); 
     ima1.setOnTouchListener(new View.OnTouchListener() { 

public boolean onTouch(View v, MotionEvent event) { 
     layoutParams = (RelativeLayout.LayoutParams) ima1.getLayoutParams(); 

     switch(event.getAction())     
      { 
       case MotionEvent.ACTION_DOWN:       
        break;  

       case MotionEvent.ACTION_MOVE: 
        int x_cord = (int) event.getRawX(); 
        int y_cord = (int) event.getRawY(); 

       System.out.println("value of x" +x_cord); 
       System.out.println("value of y" +y_cord);   

        if (x_cord > windowwidth) { 
         x_cord = windowwidth; 
         } 
        if (y_cord > windowheight) { 
         y_cord = windowheight; 
         } 
      layoutParams.leftMargin = x_cord-25; 
      layoutParams.topMargin = y_cord-25; 
      // layoutParams.rightMargin = x_cord-25; 
      // layoutParams.bottomMargin = y_cord-25; 
      ima1.setLayoutParams(layoutParams); 
        break; 
       default: break; 
       } 
       return true; 
      } 
     }); 

     ima2 = (ImageView)findViewById(R.id.imageview2); 
     ima2.setOnTouchListener(new View.OnTouchListener() {   

    public boolean onTouch(View v, MotionEvent event) { 
     layoutParams = (RelativeLayout.LayoutParams) ima2.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(); 

         System.out.println("value of x1" +x_cord); 
        System.out.println("value of y1" +y_cord);        

         if (x_cord > windowwidth) { 
          x_cord = windowwidth; 
         } 
         if (y_cord > windowheight) { 
          y_cord = windowheight; 
         } 
         layoutParams.leftMargin = x_cord - 25; 
         layoutParams.topMargin = y_cord - 75; 
         ima2.setLayoutParams(layoutParams); 
         break; 
        default: break; 
       } 
       return true; 
      } 
     }); 
     } 
    } 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    <ImageView 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:id="@+id/imageview1" 
    android:src="@drawable/image1" />  
<ImageView 
    android:layout_width="100sp" 
    android:layout_height="100sp" 
    android:id="@+id/imageview2" 
    android:src="@drawable/image2" />    
</RelativeLayout> 
+0

我這裏是http://polamreddyn.blogspot.in/2013/02/android-drag-and-drop-images-in-one.html – NagarjunaReddy

回答

8

將以下代碼寫入您的活動文件。

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


tv1 = (ImageView)findViewById(R.id.image); 
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 = (ImageView)findViewById(R.id.image1); 
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; 
    } 
}); 

XML文件: -

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ImageView android:layout_width="50sp" android:layout_height="50sp" 
     android:id="@+id/image" android:src="@drawable/image"> 
    </ImageView> 
    <ImageView android:layout_y="30dip" android:layout_x="118dip" 
     android:layout_width="50sp" android:layout_height="50sp" android:id="@+id/image1" 
     android:src="@drawable/image1"> 
    </ImageView> 
</RelativeLayout> 
+0

@Why我們正在使用textview我只想imageview .... – NagarjunaReddy

+0

請參閱我編輯的答案,並使用imageview而不是textview。 –

+0

使用一個只移動一個圖像,並移動到一些角落的圖像大小減少,第二個圖像不可能移動... – NagarjunaReddy

2

那是因爲你已經在LinearLayout把所有的東西,這意味着你不能把你想讓他們的項目,他們是總是一個接一個。您可以嘗試使用RelativeLayout代替。如果這不夠靈活,你應該看看Canvas

+0

IAM期運用RelativeLayout的,但我的問題不解決.. – NagarjunaReddy

+0

只是爲了澄清,你在「RelativeLayout的」有3張圖片,並想將它們分開在屏幕上移動,他們應該能夠相互重疊,對不對? – banzai86

+0

是佈局中的三個圖像分別將它們移動到屏幕上 – NagarjunaReddy

0

的原因是:你的屏幕上傳ACTION_MOVE太懶惰。

在正常情況下,動作此舉上傳非常頻繁,即使你的手指沒有在屏幕上移動。但某些手機屏幕並不那麼敏感。

您可以修改手機的閾值。它需要內核支持。

+0

親愛的這種發佈的答案是不允許我的意思是你發佈相同的答案與相同的內容是不允許的。 –

0

我把你的交替代碼管理中的RelativeLayout和隨機的地方多imageviews的自由。此外,我添加了獲取窗口大小的更好方法,因爲Display.getHeight()已棄用。

if(Build.VERSION.SDK_INT >= 13){ 
     android.graphics.Point p = new android.graphics.Point(); 
     this.getWindowManager().getDefaultDisplay().getSize(p); 
     width = p.x; 
     height = p.y; 
    } 
    else{ 
     Display display = getWindowManager().getDefaultDisplay(); 
     width = display.getWidth(); 
     height = display.getHeight(); 
    } 

    RelativeLayout rel = new RelativeLayout(this); 
    rel.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
    rel.setBackgroundResource(R.drawable.bg); 

    pic = new ImageView[10]; 
    layoutParams1 = new LayoutParams[10]; 

    for(int i = 0; i < 10; i++){ 
     pic[i] = new ImageView(this); 
     pic[i].setImageResource(R.drawable.img); 
     pic[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     pic[i].setAdjustViewBounds(true); 
     pic[i].setScaleType(ScaleType.FIT_XY); 
     pic[i].setMaxHeight(88); 
     pic[i].setMaxWidth(100); 
     pic[i].setMinimumHeight(88); 
     pic[i].setMinimumWidth(100); 
     pic[i].setTag(i); 
     int x = rand.nextInt(width); 
     while(x > width - 88){ 
      x = rand.nextInt(width); 
     } 
     int y = rand.nextInt(height); 
     while(y > height - 100){ 
      y = rand.nextInt(height); 
     } 
     layoutParams1[i] = (RelativeLayout.LayoutParams) pic[i].getLayoutParams(); 
     layoutParams1[i].leftMargin = x; 
     layoutParams1[i].topMargin = y; 
     pic[i].setLayoutParams(layoutParams1[i]); 
     pic[i].setOnTouchListener(new View.OnTouchListener() {   

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       int index = Integer.valueOf(v.getTag().toString()); 
       layoutParams1[index] = (RelativeLayout.LayoutParams) v.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 > width) { 
          x_cord = width; 
         } 
         if (y_cord > height) { 
          y_cord = height; 
         } 
         layoutParams1[index].leftMargin = x_cord - 44; 
         layoutParams1[index].topMargin = y_cord - 50; 
         pic[index].setLayoutParams(layoutParams1[index]); 
         break; 
        default: 
         break; 
       } 
       return true; 
      } 
     }); 

     rel.addView(pic[i]); 
    } 


    setContentView(rel); 
0

其實你可以通過編程方式宣告圖像避免這個問題。

int id = getResources().getIdentifier("image1", "drawable", getPackageName()); 
ImageView imageView1 = new ImageView(this); 
LinearLayout.LayoutParams vp = 
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT); 
imageView1.setLayoutParams(vp);   
imageView1.setImageResource(id);   
someLinearLayout.addView(imageView1); 

int id = getResources().getIdentifier("image2", "drawable", getPackageName()); 
ImageView imageView2 = new ImageView(this); 
LinearLayout.LayoutParams vp1 = 
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT); 
imageView2.setLayoutParams(vp1);   
imageView2.setImageResource(id);   
someLinearLayout.addView(imageView2); 

,並添加觸摸事件的增加imageviews