我目前正在发展这种观点。
我的目标 API 版本将是 API5。
左侧 A、B、C、D 指的是参考图像(这些是不可点击的) 向下显示的图像是“可拖动”的图像。它们被洗牌。现在用户必须拖动底部图像并将它们放在屏幕右侧相应的“可放置区域”中。
并且正在使用这种布局:
.xml 格式:
<LinearLayout id="@+id/container"
orientation="vertical">
<LinearLayout android:orientation="horizontal"
android:weight="1">
<ImageView android:src="@drawable/ic_launcher">
<ImageView android:src="#FFFFFF"
android"id="@+id/img">
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:weight="1">
<ImageView android:src="@drawable/ic_launcher">
<ImageView android:src="#FFFFFF"
android"id="@+id/img1">
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:weight="1">
<ImageView android:src="@drawable/ic_launcher">
<ImageView android:src="#FFFFFF"
android"id="@+id/img2">
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:weight="1">
<ImageView android:src="@drawable/ic_launcher">
<ImageView android:src="#FFFFFF"
android"id="@+id/img3">
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:weight="1">
<ImageView android:src="@drawable/ic_launcher"
android:id="@+id/imgDrop"
android:weight="1">
<ImageView android:src="@drawable/ic_launcher"
android:id="@+id/imgDrop1"
android:weight="1">
<ImageView android:src="@drawable/ic_launcher"
android:id="@+id/imgDrop2"
android:weight="1">
<ImageView android:src="@drawable/ic_launcher"
android:id="@+id/imgDrop3"
android:weight="1">
</LinearLayout>
对于 MainActivity ,我尝试使用以下代码从开发人员网站和 StackOverFlow 拖放 Single Image ,它正在工作:
MainActivity.java:
public class MainActivity extends Activity implements OnTouchListener {
private View selected_item = null;
private int offset_x = 0;
private int offset_y = 0;
Boolean touchFlag=false;
boolean dropFlag=false;
LayoutParams imageParams;
ImageView imageDrop,image1,image2;
int crashX,crashY;
Drawable dropDrawable,selectDrawable;
Rect dropRect,selectRect;
int topy,leftX,rightX,bottomY;
int dropArray[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewGroup container = (ViewGroup) findViewById(R.id.container);
imageDrop=(ImageView) findViewById(R.id.ImgDrop);
image1=(ImageView) findViewById(R.id.img);
image2=(ImageView) findViewById(R.id.img2);
container.setBackgroundColor(Color.GREEN);
container.setOnTouchListener(new View.OnTouchListener()
{
@SuppressWarnings("deprecation")
public boolean onTouch(View v, MotionEvent event)
{
if(touchFlag==true)
{
System.err.println("Display If Part ::->"+touchFlag);
switch (event.getActionMasked())
{
case MotionEvent.ACTION_DOWN :
topy=imageDrop.getTop();
leftX=imageDrop.getLeft();
rightX=imageDrop.getRight();
bottomY=imageDrop.getBottom();
System.err.println("Display Top-->"+topy);
System.err.println("Display Left-->"+leftX);
System.err.println("Display Right-->"+rightX);
System.err.println("Display Bottom-->"+bottomY);
//opRect.
break;
case MotionEvent.ACTION_MOVE:
crashX=(int) event.getX();
crashY=(int) event.getY();
System.err.println("Display Here X Value-->"+crashX);
System.err.println("Display Here Y Value-->"+crashY);
int x = (int) (event.getX() - offset_x);
int y = (int) (event.getY() - offset_y);
int w = getWindowManager().getDefaultDisplay().getWidth() - 50;
int h = getWindowManager().getDefaultDisplay().getHeight() - 10;
if (x > w)
x = w;
if (y > h)
y = h;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(new ViewGroup.MarginLayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
lp.setMargins(x, y, 0, 0);
//Drop Image Here
if(crashX > leftX && crashX < rightX && crashY > topy && crashY < bottomY )
{
Drawable temp=selected_item.getBackground();
imageDrop.setBackgroundDrawable(temp);
imageDrop.bringToFront();
dropFlag=true;
selected_item.setVisibility(View.INVISIBLE);
}
//Drop Image Here
selected_item.setLayoutParams(lp);
break;
case MotionEvent.ACTION_UP:
//
touchFlag=false;
if(dropFlag==true)
{
dropFlag=false;
}
else
{
selected_item.setLayoutParams(imageParams);
}
break;
default:
break;
}
}else
{
System.err.println("Display Else Part ::->"+touchFlag);
}
return true;
}
});
image1.setOnTouchListener(this);
image2.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
touchFlag=true;
offset_x = (int) event.getX();
offset_y = (int) event.getY();
selected_item = v;
imageParams=v.getLayoutParams();
break;
case MotionEvent.ACTION_UP:
selected_item=null;
touchFlag=false;
break;
default:
break;
}
return false;
} }
我的问题是如何实现我需要的逻辑方式,如上图所示。
任何类型的建议或参考都是非常可观的。谢谢。