2011-04-11 5 views
1

Comment faire glisser une image d'une mise en page vers une autre mise en page. Je joins ce que j'ai fait jusqu'ici. J'ai deux dispositions dans une seule vue. Je peux déplacer l'image sur sa mise en page initiale. Mais le problème est que je ne peux pas déplacer l'image à une disposition différente. S'il vous plaît aidez-moi à trouver la solution à ce problème.Comment faire glisser une image d'une mise en forme vers une autre mise en page

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

et le fichier principal est

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

S'il vous plaît me donner une réponse

Répondre

0

Comment pouvez-vous passer à une autre disposition si ce n'est pas son enfant? Je vais essayer d'envelopper votre mise en page racine dans RelativeLayout et placer l'image là-bas, de sorte que vous pouvez le déplacer où vous voulez.

Quelque chose comme ceci:

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

@ user639183Thanks pour vous que vous ne vous dérange pas reply.if pouvez-vous l'expliquer. Je suis débutant pourquoi demander cela. – Anish

+0

Votre image est à l'intérieur d'une mise en page, elle ne peut pas en sortir en bougeant. Vous devez donc situer votre image dans la mise en page globale qui couvre tout l'écran. Je vais mettre à jour ma réponse. – ernazm

Questions connexes