2011-04-11 74 views
1

如何将图像从一种布局拖动到另一种布局。我附上我迄今为止所做的。我在一个视图中有两个布局。我可以在初始布局上移动图像。但问题是我不能将图像移动到不同的布局。请帮我找到解决这个问题的方法。如何将图像从一种布局拖动到另一种布局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1"> 
     <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:src="@drawable/icon" 
     android:scaleType="matrix" 
     android:layout_weight="1"> 
     </ImageView> 

     <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:src="@drawable/icon" 
     android:scaleType="matrix" 
     android:layout_weight="1" 
     android:background="#00aa00" 
     > 
     </ImageView> 


      </LinearLayout> 

    </LinearLayout> 

和主文件是

package com.example.multitouch; 
import android.app.Activity; 
import android.graphics.Matrix; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.ImageView; 

public class multitouch extends Activity implements OnTouchListener{ 

    /** Called when the activity is first created. */ 
    Matrix matrix = new Matrix() ; 
    Matrix eventmatrix = new Matrix(); 
    static float centerX,centerY; 
    final static int NONE = 0; 
    final static int DRAG = 1; 
    int touchState=NONE; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ImageView view = (ImageView) findViewById(R.id.imageView); 
     view.setOnTouchListener(this);  
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     // TODO Auto-generated method stub 
     ImageView view = (ImageView) v; 
     final int action = event.getAction(); 


     switch(action){ 
     case MotionEvent.ACTION_DOWN: 
      touchState= DRAG; 
      centerX= event.getX(); 
      centerY = event.getY();   
      eventmatrix.set(matrix);     
     break;  
     case MotionEvent.ACTION_MOVE: 
      if (touchState == DRAG) { 
      matrix.set(eventmatrix); 
      matrix.setTranslate(event.getX()-centerX,event.getY()-centerY); 
      }   
      view.setImageMatrix(matrix); 
      //view.setImageMatrix(eventmatrix); 
     break; 
     case MotionEvent.ACTION_UP: 
      //touchState=NONE; 
      break; 

     }  
     return true; 
    } 
} 

请给我一个答复

回答

0

你怎么能移动到另一个布局,如果它不是其子?我会尝试将您的根布局封装到RelativeLayout并将图像放在那里,以便您可以随意移动它。

事情是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    <ImageView/> 
    <LinearLayout> 
     <LinearLayout/> 
    </LinearLayout> 
</RelativeLayout> 
+0

@ user639183Thanks你reply.if你不介意,你能解释一下。我是新手,为什么问这个问题。 – Anish 2011-04-11 15:31:49

+0

你的形象是在一些布局内,它不能通过移动摆脱它。因此,您需要将图像置于涵盖所有屏幕的全局布局中。我会更新我的答案。 – ernazm 2011-04-11 15:37:42