how can I keep image on same position after dragging?
This is the MainActivity.java
I have three button in main_activity.xml Add table and add chair button.
Each button call specific method eg. addImage(R.drawable.table2);
In that method it's create image view and add that image view in arraylist of imageview type.
On that imageview which is in relative layout I have added code on onclick method for moving that table and chair image but My concern when I am adding new image from button click all that previews imageview parameter will be lost.I want to keep every image view on same position after draging whether I click add new table or add new chair
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/84720.html
上一篇: Android:初始化新的AsyncTask(VerifyError)时,应用程序崩溃
下一篇: 如何在拖动后将图像保持在同一位置?