如何将图像从一个布局拖动到另一个布局

时间:2011-04-11 15:14:35

标签: android

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

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

请给我回复

2 个答案:

答案 0 :(得分:1)

它可能变得非常复杂,但请检查一下: http://blahti.wordpress.com/2011/02/10/moving-views-part-3/

答案 1 :(得分:0)

如果它不是它的孩子,如何将它移动到另一个布局?我会尝试将您的根布局包装到RelativeLayout并将图像放在那里,这样您就可以将它移动到任何您想要的位置。

这样的事情:

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