1

オンタッチを実装したときに2つの画像ビューがありますが、レイアウト全体を移動できるのは1つのビューだけでしたが、他の画像ビューは固定位置に保持されていました。他の画像ビューをある場所から別の場所に移動するにはどうすればよいですか?複数のビューの移動のためのコードを配置しません 助けてください plz.. friends

4

1 に答える 1

2

私はずっと前にこのコードを書いています。

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DragAndDropActivity extends Activity {
    public FrameLayout board;

    int dropZone1_X, dropZone2_X, dropZone3_X, dropZone1_Y, dropZone2_Y,
            dropZone3_Y, movingCoordinateLeft = 0, movingCoordinateTop = 0;

    int windowHeight, windowWidth, defaultMargin = 150;
    ImageView answerOption1, answerOption2, answerOption3, dropZone1,
            dropZone2, dropZone3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        board = new FrameLayout(this);
        setContentView(R.layout.layout);

        /*
         * set id's of view objects
         */
        setIds();

        /*
         * set on touch listener
         */
        setOnTouchListener();
        /*
         * get window dimensions
         */
        getWindowDimensions();

    }

    private void setOnTouchListener() {
        // TODO Auto-generated method stub
        answerOption1.setOnTouchListener(dragt);
        answerOption2.setOnTouchListener(dragt);
        answerOption3.setOnTouchListener(dragt);

    }

    private void setIds() {
        // TODO Auto-generated method stub

        board = (FrameLayout) findViewById(R.id.Board);
        // ids for answer options
        answerOption1 = (ImageView) findViewById(R.id.answer_option_1);
        answerOption2 = (ImageView) findViewById(R.id.answer_option_2);
        answerOption3 = (ImageView) findViewById(R.id.answer_option_3);

        // ids for drop zones
        dropZone1 = (ImageView) findViewById(R.id.frame1);
        dropZone2 = (ImageView) findViewById(R.id.frame2);
        dropZone3 = (ImageView) findViewById(R.id.frame3);

    }

    /*
     * 
     * Get default view dimensions at run time
     */

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {

            System.out.println("Method--onWindowFocusChanged");

            System.out.println("\n\nFirst drop zone dimensions");
            System.out.println("left margin-->" + dropZone1.getLeft());
            System.out.println("top margin-->" + dropZone1.getTop());

            System.out.println("\n\nSecond drop zone dimensions");
            System.out.println("left margin-->" + dropZone2.getLeft());
            System.out.println("top margin-->" + dropZone2.getTop());

            System.out.println("\n\nThird drop zone dimensions");
            System.out.println("left margin-->" + dropZone3.getLeft());
            System.out.println("top margin-->" + dropZone3.getTop());

        }
    }

    private void getWindowDimensions() {
        // TODO Auto-generated method stub
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        windowHeight = metrics.heightPixels;
        System.out.println("window height" + windowHeight);
        windowWidth = metrics.widthPixels;
        System.out.println("window width" + windowWidth);

    }

    // onCreate
    OnTouchListener dragt = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            FrameLayout.LayoutParams par = (LayoutParams) v.getLayoutParams();
            switch (v.getId()) {// What is being touched
            /***
             * 
             * Answer option 1
             * 
             * ***/
            case R.id.answer_option_1: {
                // Which action is being taken
                switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE: {
                    par.topMargin = (int) event.getRawY()
                            - (v.getHeight() + 22);
                    par.leftMargin = (int) event.getRawX()
                            - (v.getWidth() / 2 + 150);

                    movingCoordinateLeft = (int) event.getRawX()
                            - (v.getWidth() / 2 + 0);
                    movingCoordinateTop = par.topMargin;

                    System.out.println("Answer 1 --- left"
                            + movingCoordinateLeft + "---top"
                            + movingCoordinateTop);

                    v.setLayoutParams(par);

                    break;
                }// inner case MOVE
                case MotionEvent.ACTION_UP: {
                    par.height = 40;
                    par.width = 40;
                    /*
                     * par.topMargin = (int) event.getRawY() - (v.getHeight() +
                     * 15); par.leftMargin = (int) event.getRawX() -
                     * (v.getWidth() / 2 + 90);
                     */

                    if (windowHeight < 460) {
                        par.topMargin = 109;
                        par.leftMargin = 0;
                        par.height = 22;
                        par.width = 105;

                    } else {
                        par.topMargin = defaultMargin;
                        par.leftMargin = 0;
                    }

                    // check if co-ordinates matched and drop answer in drop
                    // zone
                    if ((movingCoordinateLeft > 10 && movingCoordinateLeft < 80)
                            && (movingCoordinateTop > 10 && movingCoordinateTop < 100)) {

                        System.out.println("left " + movingCoordinateLeft
                                + "top  " + movingCoordinateTop);

                        dropZone1.setImageDrawable(getResources().getDrawable(
                                R.drawable.duck));
                        answerOption1.setVisibility(View.INVISIBLE);

                    }

                    v.setLayoutParams(par);
                    break;
                }// inner case UP
                case MotionEvent.ACTION_DOWN: {

                    System.out.println("left" + event.getRawX());
                    System.out.println("top" + event.getRawY());

                    if (windowHeight < 460) {

                        par.height = 40;
                        par.width = 40;

                    } else {
                        par.height = 40;
                        par.width = 40;
                    }

                    v.setLayoutParams(par);
                    break;
                }// inner case UP
                }// inner switch
                break;
            }// case pawn

            /***
             * 
             * Answer option 2
             * 
             * ***/

            case R.id.answer_option_2: {// Which action is being taken
                switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE: {

                    par.topMargin = (int) event.getRawY()
                            - (v.getHeight() + 22);
                    par.leftMargin = (int) event.getRawX()
                            - (v.getWidth() / 2 + 150);

                    movingCoordinateLeft = (int) event.getRawX()
                            - (v.getWidth() / 2 + 0);
                    movingCoordinateTop = par.topMargin;

                    v.setLayoutParams(par);

                    break;
                }// inner case MOVE
                case MotionEvent.ACTION_UP: {
                    par.height = 40;
                    par.width = 40;
                    /*
                     * par.topMargin = (int) event.getRawY() - (v.getHeight() +
                     * 15); par.leftMargin = (int) event.getRawX() -
                     * (v.getWidth() / 2 + 90);
                     */

                    if (windowHeight < 460) {
                        par.topMargin = 150;
                        par.leftMargin = 0;
                        par.height = 40;
                        par.width = 40;

                    } else {
                        par.topMargin = 200;
                        par.leftMargin = 0;
                    }

                    // check if co-ordinates matched and drop answer in drop
                    // zone
                    if ((movingCoordinateLeft > 120 && movingCoordinateLeft < 200)
                            && (movingCoordinateTop > 10 && movingCoordinateTop < 100)) {

                        System.out.println("left " + movingCoordinateLeft
                                + "top  " + movingCoordinateTop);

                        dropZone2.setImageDrawable(getResources().getDrawable(
                                R.drawable.hen));
                        answerOption2.setVisibility(View.INVISIBLE);
                    }

                    v.setLayoutParams(par);

                    break;
                }// inner case UP
                case MotionEvent.ACTION_DOWN: {

                    if (windowHeight < 460) {

                        par.height = 40;
                        par.width = 40;

                    } else {
                        par.height = 40;
                        par.width = 40;
                    }

                    v.setLayoutParams(par);
                    break;
                }// inner case UP
                }// inner switch
                break;
            }// case pawn2

            /***
             * 
             * Answer option 3
             * 
             * ***/

            case R.id.answer_option_3: {// Which action is being taken
                switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE: {

                    par.topMargin = (int) event.getRawY()
                            - (v.getHeight() + 22);
                    par.leftMargin = (int) event.getRawX()
                            - (v.getWidth() / 2 + 150);

                    movingCoordinateLeft = (int) event.getRawX()
                            - (v.getWidth() / 2 + 0);
                    movingCoordinateTop = par.topMargin;

                    v.setLayoutParams(par);

                    break;
                }// inner case MOVE
                case MotionEvent.ACTION_UP: {
                    par.height = 40;
                    par.width = 40;
                    /*
                     * par.topMargin = (int) event.getRawY() - (v.getHeight() +
                     * 15); par.leftMargin = (int) event.getRawX() -
                     * (v.getWidth() / 2 + 90);
                     */

                    if (windowHeight < 460) {
                        par.topMargin = 191;
                        par.leftMargin = 0;
                        par.height = 40;
                        par.width = 40;

                    } else {
                        par.topMargin = 250;
                        par.leftMargin = 0;
                    }

                    // check if co-ordinates matched and drop answer in drop
                    // zone
                    if ((movingCoordinateLeft > 220 && movingCoordinateLeft < 310)
                            && (movingCoordinateTop > 10 && movingCoordinateTop < 100)) {

                        System.out.println("left " + movingCoordinateLeft
                                + "top  " + movingCoordinateTop);

                        dropZone3.setImageDrawable(getResources().getDrawable(
                                R.drawable.queen));
                        answerOption3.setVisibility(View.INVISIBLE);
                    }
                    v.setLayoutParams(par);

                    break;
                }// inner case UP
                case MotionEvent.ACTION_DOWN: {
                    System.out.println("down");
                    if (windowHeight < 460) {

                        par.height = 40;
                        par.width = 40;

                    } else {
                        par.height = 40;
                        par.width = 40;
                    }
                    v.setLayoutParams(par);
                    break;
                }// inner case UP
                }// inner switch
                break;
            }// case pawn2

            }// switch
            return true;
        }// onTouch

    };// dragt

}

およびlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal" >

        <ImageView
            android:id="@+id/frame3"
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:layout_alignParentRight="true"
            android:src="@drawable/drag_drop_button" />

        <ImageView
            android:id="@+id/frame2"
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:layout_toLeftOf="@+id/frame3"
            android:src="@drawable/drag_drop_button" />

        <ImageView
            android:id="@+id/frame1"
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:layout_toLeftOf="@+id/frame2"
            android:src="@drawable/drag_drop_button" />
    </RelativeLayout>

    <FrameLayout
        android:id="@+id/Board"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center" >

        <ImageView
            android:id="@+id/answer_option_1"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="150dp"
            android:background="@drawable/duck" >
        </ImageView>

        <ImageView
            android:id="@+id/answer_option_2"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_below="@+id/answer_option_1"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="200dp"
            android:background="@drawable/hen" >
        </ImageView>

        <ImageView
            android:id="@+id/answer_option_3"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="250dp"
            android:background="@drawable/queen" >
        </ImageView>
    </FrameLayout>

</RelativeLayout>
于 2013-10-22T11:12:38.023 に答える