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

前端之家收集整理的这篇文章主要介绍了android-如何在用户每次触摸屏幕时创建图像 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

但是现在我想要以下内容

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

Sample Screenshot of what I want to do

activity_main.xml

@H_404_17@<?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>

主要活动

@H_404_17@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; } } }
最佳答案
像这样做

@H_404_17@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.f2er.com/android/531383.html

猜你在找的Android相关文章