每次用户触摸屏幕时如何创建图像

问题描述 投票:3回答:1

在我的ImageViews有两个RelativeLayout。我可以通过触摸并在屏幕上的任何位置拖动图像来移动图像。图像位于drawable文件夹中

但现在我想要以下几点:

  1. 用户在屏幕上触摸的地方应该显示另一个相同的图像。
  2. 在拖动图像时继续显示图像(不删除前一个图像)。

Sample Screenshot of what I want to do

activity_main.xml中

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

    <ImageView
        android:id="@+id/backgroundIv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:scaleType="fitXY"
        android:src="@drawable/windows_xp_desktop" />

    <ImageView
        android:id="@+id/foregroundIv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:scaleType="fitXY"
        android:src="@drawable/win_xp_error" />

</RelativeLayout>

主Activity.java

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    ImageView backgroundIv, foregroundIv;
    RelativeLayout relativeLayout;

    private ViewGroup rootLayout;
    private int xDelta;
    private int yDelta;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Hiding Title bar of this activity screen */
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        //Making this activity, full screen */
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        backgroundIv = findViewById(R.id.backgroundIv);
        foregroundIv = findViewById(R.id.foregroundIv);
        relativeLayout = findViewById(R.id.relativeLayout);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(350, 200);
        foregroundIv.setLayoutParams(layoutParams);
        foregroundIv.setOnTouchListener(new ChoiceTouchListener());
    }

    private class ChoiceTouchListener implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            final int X = (int) event.getRawX();
            final int Y = (int) event.getRawY();


            switch (event.getAction() & MotionEvent.ACTION_MASK) {

                case MotionEvent.ACTION_DOWN:
                    RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                    xDelta = X - lParams.leftMargin;
                    yDelta = Y - lParams.topMargin;
                    break;

                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                    layoutParams.leftMargin = X - xDelta;
                    layoutParams.topMargin = Y - yDelta;
                    layoutParams.rightMargin = -250;
                    layoutParams.bottomMargin = -250;
                    v.setLayoutParams(layoutParams);
                    break;

            }
            relativeLayout.invalidate();
            return true;
        }
    }

}
android ontouchlistener
1个回答
2
投票

像这样做

public class MainActivity extends AppCompatActivity {

    ImageView backgroundIv, foregroundIv;
    RelativeLayout relativeLayout;

    private ViewGroup rootLayout;
    private int xDelta;
    private int yDelta;
    private ChoiceTouchListener listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        foregroundIv = findViewById(R.id.foregroundIv);
        relativeLayout = findViewById(R.id.relativeLayout);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(350, 200);
        foregroundIv.setLayoutParams(layoutParams);
        listener = new ChoiceTouchListener();
        foregroundIv.setOnTouchListener(listener);
    }

    private void addNewImage(int X, int Y) {
        ImageView img = new ImageView(this);
        img.setImageResource(R.drawable.mcblauhp);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        img.setOnTouchListener(listener);
        relativeLayout.addView(img);
        RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) img.getLayoutParams();
        lParams.leftMargin = X;
        lParams.topMargin = Y;
        img.setLayoutParams(lParams);
    }

    private class ChoiceTouchListener implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            final int X = (int) event.getRawX();
            final int Y = (int) event.getRawY();


            switch (event.getAction() & MotionEvent.ACTION_MASK) {

                case MotionEvent.ACTION_DOWN:
                    RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                    xDelta = X - lParams.leftMargin;
                    yDelta = Y - lParams.topMargin;
                    //addNewImage(lParams.leftMargin, lParams.topMargin);
                    //v.bringToFront(); // this is to ensure that the moved image is always on top
                    break;

                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                    addNewImage(layoutParams.leftMargin, layoutParams.topMargin);
                    v.bringToFront();
                    layoutParams.leftMargin = X - xDelta;
                    layoutParams.topMargin = Y - yDelta;
                    layoutParams.rightMargin = -250;
                    layoutParams.bottomMargin = -250;
                    v.setLayoutParams(layoutParams);
                    break;

            }
            relativeLayout.invalidate();
            return true;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.