[英]drag and drop (checkmark if I drag the right image) android
如果我将countryTypical图像拖放到imageCountry [n9]中,则应用程序将选中标记图像设置在正确的countryTypical图像上。 如果拖动正确的countryTypical图像,并且拖动错误的countryTypical图像,也会发生这种情况。 我想更正错误。 非常感谢...
String[] countryNames = {
"Italy", "France", "Spain", "Germany", "Belgium", "Portugal", "Switzerland", "Great Britain"
};
int[] countryImages = {
R.drawable.italy,
R.drawable.france,
R.drawable.spain,...
};
int[] countryFlags = {
R.drawable.italian_flag,
R.drawable.french_flag,
R.drawable.spanish_flag,....
};
在我的OnCreate中:
countryTypical1.setImageResource(countryFlags[n1]);
countryTypical2.setImageResource(countryFlags[n2]);
countryTypical3.setImageResource(countryFlags[n3]);
countryTypical4.setImageResource(countryFlags[n4]);
countryTypical5.setImageResource(countryFlags[n5]);
countryTypical6.setImageResource(countryFlags[n6]);
countryTypical7.setImageResource(countryFlags[n7]);
countryTypical8.setImageResource(countryFlags[n8]);
countryTypical1.setOnTouchListener(dragClick);
countryTypical2.setOnTouchListener(dragClick);
countryTypical3.setOnTouchListener(dragClick);
countryTypical4.setOnTouchListener(dragClick);
countryTypical5.setOnTouchListener(dragClick);
countryTypical6.setOnTouchListener(dragClick);
countryTypical7.setOnTouchListener(dragClick);
countryTypical8.setOnTouchListener(dragClick);
imageCountry.setOnDragListener(DropListener);
View.OnTouchListener dragClick = new View.OnTouchListener(){
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
DragShadow dragShadow = new DragShadow(view);
ClipData dragData = ClipData.newPlainText("", "");
view.startDrag(dragData, dragShadow, view,0);
return true;
}
return true;
}
};
private class DragShadow extends View.DragShadowBuilder{
public DragShadow(View view) {
super(view);
}
@Override
public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
View view = getView();
int height = view.getHeight();
int width = view.getWidth();
shadowSize.set(width, height);
shadowTouchPoint.set(width, height);
}
@Override
public void onDrawShadow(Canvas canvas) {
getView().draw(canvas);
}
}
View.OnDragListener DropListener = new View.OnDragListener() {
@Override
public boolean onDrag(View view, DragEvent event) {
assignDropTrue(countryTypical1, event, n1);
assignDropTrue(countryTypical2, event, n2);
assignDropTrue(countryTypical3, event, n3);
assignDropTrue(countryTypical4, event, n4);
assignDropTrue(countryTypical5, event, n5);
assignDropTrue(countryTypical6, event, n6);
assignDropTrue(countryTypical7, event, n7);
assignDropTrue(countryTypical8, event, n8);
return true;
}
};
private void assignDropTrue(ImageView countryTypical, DragEvent event, int n1){
if (n9 == n1){
int dragEvent = event.getAction();
switch (dragEvent) {
case DragEvent.ACTION_DROP:
countryTypical.setImageResource(R.drawable.vcheckmark);
}}
}
我写了一个示例,目的只是为了让您对我提出的逻辑有一个想法(也就是未经测试)
View.OnDragListener DropListener = new View.OnDragListener() {
@Override
public boolean onDrag(View view, DragEvent event) {
int action = event.getAction();
View draggedView = (View) event.getLocalState();
//view is the country where the flag was dropped, draggedView is the flag you dropped
//below we check if the correct flag is dropped in the correct country
switch (event.getAction()){
case DragEvent.ACTION_DROP:
for (int i; i< countryImages.length(); i++){
if (draggedView.getId() == countryFlags[i] && view.getId()== countryImages[i])
draggedView.setImageResource(R.drawable.vcheckmark);
}
break;
default:
break;
}
return true;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.