2013-08-17 83 views
1

我可以拖動圖像,但當拖動圖像時,圖像大小不斷變化。圖像大小不固定。請讓我知道我錯了。當我拖動圖像時,它會不斷改變特定圖像的大小。有關於佈局參數的東西,但我無法修復它。在修正大小布局中拖放圖像Android

Xml我的拖動區域。

<LinearLayout 
      android:id="@+id/vg1" 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:gravity="center" 
    android:weightSum="1" 
    android:layout_alignTop="@+id/scrollView1" 
    android:layout_toLeftOf="@+id/scrollView2" 
    android:layout_toRightOf="@+id/scrollView1" 
    android:gravity="right" 
    android:background="@drawable/shape" > 


    <ImageView 
     android:id="@+id/dragimg" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="15dp" 
     android:layout_marginTop="15dp" 
     android:src="@drawable/log_off_button" /> 

</LinearLayout> 

我將圖片代碼:

 OnTouchListener dragt = new OnTouchListener() 
    { 
    @Override  
    public boolean onTouch(View v, MotionEvent event) 
    { 

     LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(v.getLayoutParams()); 
     v.setLayoutParams(par); 
    switch(v.getId()) 
    {//What is being touched          
    case R.id.dragimg:  
    {//Which action is being taken  
    switch(event.getAction())   
    { 
    case MotionEvent.ACTION_MOVE:   
    {  
     par.topMargin = (int)event.getRawY() + windowheight - (vg.getHeight()/2);  
     par.leftMargin = (int) ((int)event.getRawX() +windowwidth - (vg.getWidth())); 

    v.setLayoutParams(par); 
    break; 
    }//inner case MOVE 
    case MotionEvent.ACTION_UP:      
    { 

     par.topMargin = (int)event.getRawY()+ windowheight - (vg.getHeight()/2); 
     par.leftMargin = (int) ((int)event.getRawX() + windowwidth - (vg.getWidth())); 
    v.setLayoutParams(par); 
    break; 
    }//inner case UP 
    case MotionEvent.ACTION_DOWN: 
    { 
// par.height = 60; 
// par.width = 60; 
    v.setLayoutParams(par); 
    break;   
    } 
    } 
    break; 
    } 
    } 
    return true;   
    } 
    }; 

請讓我知道我錯了,或者更好的方式來做到這一點。將只有1個圖像進行拖放。

回答

0

我有同樣的問題,這是我找到了解決辦法:

private View.OnTouchListener onTouchListener() { 
     return new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View view, MotionEvent event) { 

       final int x = (int) event.getRawX(); 
       final int y = (int) event.getRawY(); 

       switch (event.getAction() & MotionEvent.ACTION_MASK) { 

        case MotionEvent.ACTION_DOWN: 
         RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) 
           view.getLayoutParams(); 

         xDelta = x - lParams.leftMargin; 
         yDelta = y - lParams.topMargin; 
         break; 

        case MotionEvent.ACTION_UP: 

         break; 

        case MotionEvent.ACTION_MOVE: 
         RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view 
           .getLayoutParams(); 
         layoutParams.leftMargin = x - xDelta; 
         layoutParams.topMargin = y - yDelta; 
         layoutParams.rightMargin = -250; 
         layoutParams.bottomMargin = -250; 
         view.setLayoutParams(layoutParams); 
         break; 
       } 
       image_layout.invalidate(); 
       return true; 
      } 
     }; 
    } 

添加這兩條線是最重要的:

layoutParams.rightMargin = -250; 
layoutParams.bottomMargin = -250; 

這將使的ImageView無法改變它的大小,當它走向右上角或右下角。

希望它能幫助你。

+0

你好,我想拖動圖像的面積修復大小我怎麼做? –