如何在拖动后将图像保持在同一位置?
这是MainActivity.java
我在main_activity.xml中有三个按钮添加表格并添加椅子按钮。
每个按钮调用特定方法,例如。 addImage(R.drawable.table2);
在该方法中,它创建图像视图并将图像视图添加到imageview类型的数组列表中。
在相对布局的imageview中,我添加了onclick方法移动桌子和椅子图像的代码,但是当我从按钮添加新图像时,我担心的是,所有预览的imageview参数都将丢失。我想保留每个图像视图在拖动之后在同一位置上是否点击添加新表格或添加新椅子
https://github.com/Imshyamal/RestoTableBook
package com.example.andiroot.restotablebook; import android.content.Intent; import android.os.Build; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.Display; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Toast; import java.util.ArrayList; import java.util.Random; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private ImageView img; private String msg = "resto_chairtab"; private Random rand = new Random(); RelativeLayout.LayoutParams layoutParams; String DEBUG_TAG; RelativeLayout rel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setListeners(); rel = (RelativeLayout)findViewById(R.id.second); } private void setListeners() { findViewById(R.id.btnMainScreen).setOnClickListener(this); findViewById(R.id.btnAddTable).setOnClickListener(this); findViewById(R.id.btnChair).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btnMainScreen: // startActivity(new Intent(getApplicationContext(),TableManager.class)); Globals.tablesAndChairs.clear(); clarall(); Toast.makeText(this, "Hall Cleared", Toast.LENGTH_SHORT).show(); break; case R.id.btnAddTable: addImage(R.drawable.table2); Toast.makeText(this, R.string.table_added, Toast.LENGTH_SHORT).show(); break; case R.id.btnChair: addImage2(R.drawable.chair2); Toast.makeText(this, R.string.child_added, Toast.LENGTH_SHORT).show(); break; } final int width,height; if(Build.VERSION.SDK_INT >= 13){ android.graphics.Point p = new android.graphics.Point(); this.getWindowManager().getDefaultDisplay().getSize(p); width = p.x; height = p.y; } else{ Display display = getWindowManager().getDefaultDisplay(); width = display.getWidth(); height = display.getHeight(); } rel.setBackgroundResource(R.drawable.shape); for(final ImageView imageView : Globals.tablesAndChairs){ layoutParams =(RelativeLayout.LayoutParams)imageView.getLayoutParams(); // To do convert oixels to dp layoutParams.leftMargin = 20; layoutParams.topMargin = 20; imageView.setLayoutParams(layoutParams); if(imageView.getParent() != null) ((ViewGroup)imageView.getParent()).removeView(imageView); rel.addView(imageView); imageView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int index = Integer.valueOf(v.getTag().toString()); layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams(); switch(event.getActionMasked()) { case MotionEvent.ACTION_DOWN: Log.d(DEBUG_TAG,"Action was DOWN"); break; case MotionEvent.ACTION_MOVE: //Move object to another place Log.d(DEBUG_TAG,"Action was MOVE"); int x_cord = (int) event.getRawX(); int y_cord = (int) event.getRawY(); if (x_cord > width) { x_cord = width; } if (y_cord > height) { y_cord = height; } layoutParams.leftMargin = x_cord - 50; layoutParams.topMargin = y_cord - 50; imageView.setLayoutParams(layoutParams); break; case (MotionEvent.ACTION_UP) : Log.d(DEBUG_TAG,"Action was UP"); break; case (MotionEvent.ACTION_CANCEL) : Log.d(DEBUG_TAG,"Action was CANCEL"); return true; case (MotionEvent.ACTION_OUTSIDE) : Log.d(DEBUG_TAG,"Movement occurred outside bounds " + "of current screen element"); return true; default: break; } return true; } }); } } private void clarall() { rel.removeAllViews(); } private void addImage(int image){ ImageView img = new ImageView(this); img.setImageResource(image); img.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); img.setAdjustViewBounds(true); img.setScaleType(ImageView.ScaleType.FIT_XY); img.setMaxHeight(300); img.setMaxWidth(300); img.setMinimumHeight(300); img.setMinimumWidth(300); img.setTag(new Random().nextInt()); Globals.tablesAndChairs.add(img); } private void addImage2(int image){ ImageView img = new ImageView(this); img.setImageResource(image); img.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); img.setAdjustViewBounds(true); img.setScaleType(ImageView.ScaleType.FIT_XY); img.setMaxHeight(100); img.setMaxWidth(100); img.setMinimumHeight(100); img.setMinimumWidth(100); img.setTag(new Random().nextInt()); Globals.tablesAndChairs.add(img); } }链接地址: http://www.djcxy.com/p/84719.html
上一篇: how can I keep image on same position after dragging?
下一篇: RelativeLayout inside Scrollview expands more than screen size