我正在创建一个照片编辑器(android)。基本功能是我想在图像上重叠图像。
例如: 我有一个男孩的脸作为主要形象。我有4个图像可以拖动它。像小胡子,帽子,球和星。
我的问题是当我将图像拖到特定区域(在嘴上)时,它会转到左角。
这是我的XML布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.61"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="3.21"
android:id="@+id/imgstage">
<ImageView
android:id="@+id/imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tap" />
</FrameLayout>
<LinearLayout
android:layout_width="70dp"
android:layout_height="fill_parent"
android:background="@drawable/sidebg"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.10" >
<ImageView
android:id="@+id/imageBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.10"
android:contentDescription="Block 1"
android:src="@drawable/blueblock" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.10" >
<ImageView
android:id="@+id/imageOrange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.10"
android:layout_margin="5dp"
android:contentDescription="Block 2"
android:src="@drawable/orangeblock" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.10" >
<ImageView
android:id="@+id/imageGreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.10"
android:layout_margin="5dp"
android:contentDescription="Block 3"
android:src="@drawable/greenblock" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.10" >
<ImageView
android:id="@+id/imageRed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.10"
android:layout_margin="5dp"
android:contentDescription="Block 4"
android:src="@drawable/redblock" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
MainAcivity.java
public class MainActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView mainImage = (ImageView) findViewById(R.id.imgView);
mainImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startLoadImageActivity();
}
});
findViewById(R.id.imageBlue).setOnTouchListener(new MyTouchListener());
findViewById(R.id.imageOrange).setOnTouchListener(new MyTouchListener());
findViewById(R.id.imageRed).setOnTouchListener(new MyTouchListener());
findViewById(R.id.imageGreen).setOnTouchListener(new MyTouchListener());
findViewById(R.id.imageBlue).setOnDragListener(new MyDragListener());
findViewById(R.id.imageOrange).setOnDragListener(new MyDragListener());
findViewById(R.id.imageRed).setOnDragListener(new MyDragListener());
findViewById(R.id.imageGreen).setOnDragListener(new MyDragListener());
findViewById(R.id.imgstage).setOnDragListener(new MyDragListener());
}
@SuppressLint("NewApi")
private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
}
class MyDragListener implements OnDragListener {
public boolean onDrag(View v, DragEvent e) {
if (e.getAction()==DragEvent.ACTION_DROP) {
View view = (View) e.getLocalState();
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
FrameLayout to = (FrameLayout) v;
to.addView(view);
view.setVisibility(View.VISIBLE);
}
return true;
}
}
我不知道问题出现在布局或代码中。