Как перетащить изображение из одного макета в другой макет. Я прилагаю то, что я сделал до сих пор. У меня два макета в одном представлении. Я могу перемещать изображение по его первоначальной компоновке. Но проблема в том, что я не могу переместить изображение на другой макет. Пожалуйста, помогите мне найти решение этой проблемы.
<?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;
}
}
Пожалуйста, дайте мне ответ
Это может стать очень сложным, но проверьте это: http://blahti.wordpress.com/2011/02/10/moving-views-part-3/
Как вы можете переместить его на другой макет, если он не является его дочерним? Я бы попытался обернуть ваш корневой макет в RelativeLayout
и поместить туда изображение, чтобы вы могли перемещать его туда, где хотите.
Что-то вроде этого:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ImageView/>
<LinearLayout>
<LinearLayout/>
</LinearLayout>
</RelativeLayout>