微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我的RelativeLayout中有两个ImageView.我可以通过触摸并将其拖动到屏幕上的任何位置来移动图像.
图像在可绘制文件夹中

但是现在我想要以下内容

>用户在屏幕上触摸过的任何地方都应显示相同的内容
图片.
>拖动时继续显示图像(不删除一个)
图片.

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>

主要活动

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;
        }
    }

}
最佳答案
像这样做

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;
        }
    }
}

原文地址:https://www.jb51.cc/android/531383.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐