how to add move image on touch event android?

This class has three Buttons Add table, Add chair and reset add table will add tablein second layout add chair will add chair image in second layout.Both button works but I want to move chair and table ( imageView ) in screen.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ArrayList<String> mSelecetdItems = new ArrayList<>();
    public static final String SELECETD_ITEMS = "SELECETD_ITEMS";

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

    public  void addtable(View v){
        this.mSelecetdItems.add("table");
        showSecondActivity();
    }
    public  void addchair(View v){
        this.mSelecetdItems.add("chair");
        showSecondActivity();
    }

    private void showSecondActivity() {
        Intent i = new Intent(this,Second.class);
        i.putStringArrayListExtra(SELECETD_ITEMS,this.mSelecetdItems);
        startActivity(i);
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putStringArrayList(SELECETD_ITEMS , mSelecetdItems);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        this.mSelecetdItems = savedInstanceState.getStringArrayList(SELECETD_ITEMS);
    }
}

This class has three Buttons Add table, Add chair and reset add table will add tablein second layout add chair will add chair image in second layout. I want to move table and chair in layout image.

Second.java

public class Second extends AppCompatActivity {

    ImageView iv;
    ImageView iv2;
    private int xDelta;
    private int yDelta;
    LinearLayoutCompat hall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Toolbar toolbar = (Toolbar) findViewById(R.id.second_toolbar);
        setSupportActionBar(toolbar);
        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);

        hall = (LinearLayoutCompat) findViewById(R.id.hall_layout);
        Intent i = getIntent();

        ArrayList<String> selecetdItems = i.getStringArrayListExtra(MainActivity.SELECETD_ITEMS);
        for (String selecetdItem : selecetdItems) {
            if (selecetdItem.equals("table")) {
                addImageView(R.drawable.table, hall);
            } else if (selecetdItem.equals("chair")) {
                addImageView2(R.drawable.chair2, hall);
            }
        }
    }

    private void addImageView(int imageRes, LinearLayoutCompat hall) {
       iv = new ImageView(this);
        iv.setImageResource(imageRes);
        iv.setLayoutParams(new android.view.ViewGroup.LayoutParams(250, 250));
        iv.setMaxHeight(180);
        iv.setMaxWidth(120);
        iv.setPadding(15,15,0,0);
        hall.addView(iv);
    }

    private void addImageView2(int imageRes, LinearLayoutCompat hall) {
         iv2 = new ImageView(this);
        iv2.setImageResource(imageRes);
        iv2.setLayoutParams(new android.view.ViewGroup.LayoutParams(150, 150));
        iv2.setMaxHeight(30);
        iv2.setMaxWidth(30);

        iv2.setPadding(25,0,0,0);
        hall.addView(iv2);
    }
}

activity_main.xml This layout file has ConstraintLayout in Root.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.andiroot.restaurantbook.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="#ff1e8622"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
      >
    </android.support.v7.widget.Toolbar>

    <RelativeLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="64dp">

        <Button
            android:id="@+id/btn_addtbl"
            style="@style/Widget.AppCompat.Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="76dp"
            android:text="Add Table"
            android:onClick="addtable"/>

        <Button
            android:id="@+id/btn_addchairs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="addchair"
            android:layout_marginTop="27dp"
            android:text="Add Chair"
            android:layout_below="@+id/btn_addtbl"
            android:layout_alignEnd="@+id/btn_addtbl" />

        <Button
            android:id="@+id/btn_reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/btn_addchairs"
            android:layout_below="@+id/btn_addchairs"
            android:layout_marginTop="51dp"
            android:text="Reset" />
    </RelativeLayout>



</android.support.constraint.ConstraintLayout>

activity_second.xml This layout file has LinearLayoutCompat in Root

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/hall_layout"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.andiroot.restaurantbook.Second">

    <android.support.v7.widget.Toolbar
        android:id="@+id/second_toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="#ff1e8622"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">



    </android.support.v7.widget.Toolbar>
<!--
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/hall_layout2">
    </RelativeLayout>
-->
</android.support.v7.widget.LinearLayoutCompat>


</pre>

尝试这个

    ImageView dishaIMG;
    RelativeLayout cropperLayout;
    float dX, dY;
    float windowWidth, windowHeight;

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    windowHeight = displayMetrics.heightPixels;
    windowWidth = displayMetrics.widthPixels;

    cropperLayout = (RelativeLayout) findViewById(R.id.activity_main2);

    dishaIMG = (ImageView) findViewById(R.id.img_disha);


        dishaIMG.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent motionEvent) {

            int width = dishaIMG.getLayoutParams().width;
            int height = dishaIMG.getLayoutParams().height;

            switch (motionEvent.getAction()) {

                case MotionEvent.ACTION_DOWN:

                    dX = v.getX() - motionEvent.getRawX();
                    dY = v.getY() - motionEvent.getRawY();

                    case MotionEvent.ACTION_MOVE:

                    if (width == windowWidth && height == windowHeight) {


                    } else {
                        v.animate()
                                .x(motionEvent.getRawX() + dX)
                                .y(motionEvent.getRawY() + dY)
                                .setDuration(0)
                                .start();

                        if (motionEvent.getRawX() + dX + width > windowWidth) {
                            v.animate()
                                    .x(windowWidth - width)
                                    .setDuration(0)
                                    .start();
                        }
                        if (motionEvent.getRawX() + dX < 0) {
                            v.animate()
                                    .x(0)
                                    .setDuration(0)
                                    .start();
                        }
                        if (motionEvent.getRawY() + dY + height > windowHeight) {
                            v.animate()
                                    .y(windowHeight - height)
                                    .setDuration(0)
                                    .start();
                        }
                        if (motionEvent.getRawY() + dY < 0) {
                            v.animate()
                                    .y(0)
                                    .setDuration(0)
                                    .start();
                        }

                    }
            }
            return true;
        }
    });
链接地址: http://www.djcxy.com/p/96152.html

上一篇: 优化解决Sudoku的回溯算法

下一篇: 如何在触摸事件android上添加移动图像?