Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在游戏中实现拖放?_Java_Android_Drag And Drop_Imageview - Fatal编程技术网

Java 如何在游戏中实现拖放?

Java 如何在游戏中实现拖放?,java,android,drag-and-drop,imageview,Java,Android,Drag And Drop,Imageview,我一直在寻找所有的软件和在线教程,但由于某些原因,我仍然无法让它工作。我想在游戏中实现拖放功能。活动内容如下: 我想能够拖放底部的4个形状。如果正确的形状合适,我希望形状与?改变成正确的形状。有人能告诉我怎么做吗 这是我的密码: public class SecondActivity extends AppCompatActivity { int n; ImageView shape1, shape2, shape3, shape4, guessShape; ImageButton exit

我一直在寻找所有的软件和在线教程,但由于某些原因,我仍然无法让它工作。我想在游戏中实现拖放功能。活动内容如下:

我想能够拖放底部的4个形状。如果正确的形状合适,我希望形状与?改变成正确的形状。有人能告诉我怎么做吗

这是我的密码:

public class SecondActivity extends AppCompatActivity {

int n;
ImageView shape1, shape2, shape3, shape4, guessShape;
ImageButton exit;
private android.widget.RelativeLayout.LayoutParams layoutParams;
Random rand = new Random();
ImageView[] shapes = new ImageView[4];


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    //declare each imageview
    shape1 = (ImageView) findViewById(R.id.shape1);
    shape2 = (ImageView) findViewById(R.id.shape2);
    shape3 = (ImageView) findViewById(R.id.shape3);
    shape4 = (ImageView) findViewById(R.id.shape4);
    guessShape = (ImageView) findViewById(R.id.guessShape);
    exit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
            System.exit(0);
        }
    });

    //add each imageView to the shapes[] array
    shapes[0] = shape1;
    shapes[1] = shape2;
    shapes[2] = shape3;
    shapes[3] = shape4;

    //store all the shapes in an array
    int[] images = new int[]{R.drawable.img_0, R.drawable.img_1, R.drawable.img_2, R.drawable.img_3, R.drawable.img_4,
            R.drawable.img_5, R.drawable.img_6, R.drawable.img_7, R.drawable.img_8, R.drawable.img_9, R.drawable.img_10,
            R.drawable.img_11, R.drawable.img_12, R.drawable.img_13, R.drawable.img_14, R.drawable.img_15, R.drawable.img_16,
            R.drawable.img_17};

    //store all the guessShapes in an array
    int[] outlines = new int[]{R.drawable.outline_0, R.drawable.outline_1, R.drawable.outline_2,
            R.drawable.outline_3, R.drawable.outline_4, R.drawable.outline_5, R.drawable.outline_6,
            R.drawable.outline_7, R.drawable.outline_8, R.drawable.outline_9, R.drawable.outline_10,
            R.drawable.outline_11, R.drawable.outline_12, R.drawable.outline_13, R.drawable.outline_14,
            R.drawable.outline_15, R.drawable.outline_16, R.drawable.outline_17};

    //generate 4 random images from the array's and ensure that they don't match each other
    ArrayList<Integer> list = new ArrayList<Integer>();
    for (int i = 0; i < 18; i++) {
        list.add(new Integer(i));
    }
    Collections.shuffle(list);
    int whichImg = (int) Math.round((Math.random() * 4));
    int img1 = list.get(0);
    int img2 = list.get(1);
    int img3 = list.get(2);
    int img4 = list.get(3);

    if (whichImg == 1) {
        whichImg = img1;
    } else if (whichImg == 2) {
        whichImg = img2;
    } else if (whichImg == 3) {
        whichImg = img3;
    } else {
        whichImg = img4;
    }

    int outlineID = outlines[whichImg];

    //set the shape in each imageview
    guessShape.setBackgroundResource(outlineID);
    shape1.setBackgroundResource(images[img1]);
    shape2.setBackgroundResource(images[img2]);
    shape3.setBackgroundResource(images[img3]);
    shape4.setBackgroundResource(images[img4]);

    //ensures that 1/4 shape has the guess shape correspondence
    final Object currentBackground = guessShape.getBackground().getConstantState();

    //for loop to have the guess shape and 1/4 shapes to match
    for (int i = 0; i < 18; i++) {
        if (currentBackground.equals(getResourceID("outline_" + i, "drawable", getApplicationContext()))) {
            int random = new Random().nextInt(shapes.length);
            shapes[random].setBackgroundResource(getResourceID("img_" + i, "drawable", getApplicationContext()));
        }

        //set tags for each view
        guessShape.setTag("gShape");
        shape1.setTag("S_1");
        shape2.setTag("S_2");
        shape3.setTag("S_3");
        shape4.setTag("S_4");

    }
}


//method to get the ID of an image in drawable folder
protected final static int getResourceID(final String resName, final String resType, final Context ctx)
{
    final int ResourceID =
            ctx.getResources().getIdentifier(resName, resType,
                    ctx.getApplicationInfo().packageName);
    if (ResourceID == 0)
    {
        throw new IllegalArgumentException
                (
                        "No resource string found with name " + resName
                );
    }
    else
    {
        return ResourceID;
    }
}

}

尽管你还不够清楚

试试这个,首先创建一个实现onTouchListener的类

然后定义一个拖动侦听器

class MyDragListener implements OnDragListener {
    Drawable enterShape = getResources().getDrawable(
            R.drawable.shape_droptarget);
    Drawable normalShape = getResources().getDrawable(R.drawable.shape);

    @Override
    public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
        // do nothing
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            v.setBackgroundDrawable(enterShape);
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            v.setBackgroundDrawable(normalShape);
            break;
        case DragEvent.ACTION_DROP:
            // Dropped, reassign View to ViewGroup
            View view = (View) event.getLocalState();
            ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            LinearLayout container = (LinearLayout) v;
            container.addView(view);
            view.setVisibility(View.VISIBLE);
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            v.setBackgroundDrawable(normalShape);
            default:
            break;
        }
        return true;
    }
}
现在只需使用这些行

findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());

这里有一些教程可能对您有所帮助


谢谢你的回答,我尝试过实现它,但我不赞成使用“startDrag”?与setBackgroundDrawable相同,它不赞成使用??对不起,我是新来安卓的,我不知道这意味着什么,有一条线穿过它,所以,你声明了两个可绘制的entershape和normalshape,但事实是,形状有?可以是大纲数组中我的代码中看到的18个形状,底部4个形状可以是图像数组中的任何形状。所以我不能真的声明一个图像忽略所有的线条,你只需要检查它是否工作,关于形状,如果你有一个图像数组,只使用图像上的逻辑并不重要view@AzIslam这里的开发者只帮你解决问题,他们并不能真正解决你的问题,这取决于问题有多大,如果问题很小,是的,开发人员会给出精确的解决方案,但对于冗长的问题,他们只给出步骤,我希望你能理解这一点
findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
findViewById(R.id.bottomleft).setOnDragListener(new MyDragListener());