我打算制作和应用程序,我可以将不同类型的球图像拖放到某种类型的场中。为此,我做了一个简单的例子,其中我有球的图像的例子,我想在场内拖动它的多个副本。这是example我一直在做一些研究,我发现要这样做,最好的方法是使用一个自定义视图对象作为字段。它将包含我拥有的所有图像及其在该字段中的位置的列表。
我错过了最后一步,我将图像放在自定义视图中,并将其添加到包含X和Y位置的图像列表中,这样我就可以稍后用所有的球恢复图像。
我该怎么办?这是我已有的代码:
布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="10dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LIST OF ITEMS TO DRAG"
android:textSize="25sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/ball"
android:layout_margin="10dp"
android:id="@+id/image_to_drag"
/>
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/ball2"
android:layout_margin="10dp"
android:id="@+id/image_to_drag2"
/>
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/ball3"
android:layout_margin="10dp"
android:id="@+id/image_to_drag3"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CUSTOMVIEW WHERE DROP"
android:textSize="25sp"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<com.dragandrop.canvas.canvas.Salon
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/salon_1"
android:background="#96e29b"
/>
</FrameLayout>
</LinearLayout>
CustomView类
public class Field extends View
{
Context context;
public Salon(Context context, AttributeSet attrs)
{
super(context,attrs);
this.context = context;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Resources res = getResources();
float left = 0;
float top = 0;
}
}
主要活动
public class MainActivity extends AppCompatActivity {
ImageView image;
ImageView image2;
ImageView image3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image_to_drag);
image2 = (ImageView) findViewById(R.id.image_to_drag2);
image3 = (ImageView) findViewById(R.id.image_to_drag3);
image.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData clipData = ClipData.newPlainText("","");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(image);
image.startDrag(clipData,shadowBuilder,image,0);
return false;
}
});
image2.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData clipData = ClipData.newPlainText("","");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(image2);
image2.startDrag(clipData,shadowBuilder,image2,0);
return false;
}
});
image3.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData clipData = ClipData.newPlainText("","");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(image3);
image3.startDrag(clipData,shadowBuilder,image3,0);
return false;
}
});
}
}