Android, overlapping imageViews - android

I am creating programatically ImageViews and dragging them in the same layout.
I would like to disable overlapping for this ImageView. Is there a setting to be applied to disable overlap when I create every ImageView programatically or something else ?
public class MainActivity extends ActionBarActivity {
private int offset_x = 0;
private int offset_y = 0;
RelativeLayout canvas;
LinearLayout buttonslayout;
boolean isselected = false;
View selected_item = null;
int test1,test2;
Button save,newdoor,newwindow,rotate;
TextView xcoord,ycoord;
//static final int SNAP_GRID_INTERVAL = 43;
static final int SNAP_GRID_INTERVAL = 44;
static final int SNAP_GRID_INTERVAL2 = 25;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
canvas = (RelativeLayout) findViewById(R.id.container2);
ViewGroup vg = (ViewGroup)findViewById(R.id.container3);
//buttonslayout = (LinearLayout) findViewById(R.id.container3);
save = (Button) findViewById(R.id.savebtn);
newdoor = (Button) findViewById(R.id.btnnewdoor);
newwindow = (Button) findViewById(R.id.btnnewwindow);
xcoord = (TextView) findViewById(R.id.xcoord);
ycoord = (TextView) findViewById(R.id.ycoord);
rotate = (Button) findViewById(R.id.rotate);
newwindow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final int lungime = 66;
final int inaltime = 20;
final int []lastcoords = new int[2];
final int canvasWidth = canvas.getWidth();
final int canvasHeight = canvas.getHeight();
RelativeLayout mylayout = (RelativeLayout) findViewById(R.id.container2);
final View canvas = ((View) v.getParent());
final int []coordwall = new int[2];
mylayout.getLocationInWindow(coordwall);
final ImageView image = new ImageView(getBaseContext());
image.setBackgroundResource(R.drawable.element_window);
mylayout.addView(image);
image.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
offset_x = (int)event.getX();
offset_y = (int)event.getY();
selected_item = v;
isselected = true;
xcoord.setText("X: "+lastcoords[0]);
ycoord.setText("Y: "+lastcoords[1]);
Log.i("test","Isselected = "+isselected);
break;
default:
break;
}
return false;
}
});
rotate.setOnClickListener(new View.OnClickListener() {
int grad=0;
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(grad!=1)
{
image.setRotation(90);
grad=1;
}
else
{
image.setRotation(180);
grad=0;
}
}
});
mylayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
MarginLayoutParams marginParams = new MarginLayoutParams(v.getLayoutParams());
switch(event.getAction())
{
case MotionEvent.ACTION_UP:
isselected = false;
Log.i("test","Isselected = "+isselected);
break;
case MotionEvent.ACTION_MOVE:
if(isselected == true)
{
View canvas = ((View) v.getParent());
final int width = v.getWidth() / 2, height = v.getHeight() / 2;
int x = (int)event.getX() - offset_x;
int y = (int)event.getY() - offset_y;
int w = getWindowManager().getDefaultDisplay().getWidth() - 100;
int h = getWindowManager().getDefaultDisplay().getHeight() - 100;
int rawX = (int) event.getRawX(), rawY = (int) event.getRawY();
int x_coord = (int) event.getRawX() - coordwall[0] - width;
int y_coord = (int) event.getRawY() - coordwall[1] - height;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
if (rawY < coordwall[1] + inaltime / 2)
lp.topMargin = 0;
else
if (rawY >= coordwall[1] + canvasHeight - inaltime )
lp.topMargin = canvasHeight - inaltime*2;
else
lp.topMargin = y / SNAP_GRID_INTERVAL * SNAP_GRID_INTERVAL;
if(rawX < coordwall[0] + lungime / 2)
lp.leftMargin = 0;
else
if(rawX >= coordwall[0] + canvasWidth - lungime)
{
lp.leftMargin = canvasWidth - lungime*2;
Log.i("arry","leftmargin = " + lp.leftMargin);
}
else
lp.leftMargin = x / SNAP_GRID_INTERVAL * SNAP_GRID_INTERVAL;
lastcoords[0] = lp.leftMargin;
lastcoords[1] = lp.topMargin;
xcoord.setText("X: "+lp.leftMargin);
ycoord.setText("Y: "+lp.topMargin);
lp.setMargins(lp.leftMargin, lp.topMargin, 0, 0);
selected_item.setLayoutParams(lp);
}
break;
default:
break;
}
return true;
}
});
}
});
And the xml:
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="horizontal"
tools:context="com.example.draganddrop.MainActivity"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:id="#+id/container3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#848484"
android:orientation="vertical" >
<Button
android:id="#+id/savebtn"
style="#drawable/new_wall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/new_wall"
android:textSize="13sp" />
<Button
android:id="#+id/btnnewwindow"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/new_window"
android:textSize="13sp" />
<Button
android:id="#+id/btnnewdoor"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/new_door"
android:textSize="14sp" />
<TextView
android:id="#+id/xcoord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="X: " />
<TextView
android:id="#+id/ycoord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Y: " />
<Button
android:id="#+id/rotate"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate" />
</LinearLayout>
<RelativeLayout
android:id="#+id/container2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" >
</RelativeLayout>
</LinearLayout>

It is not very clear what exactly You want to do, but if I am understand You the right way, You want to set some ImageViews to a layout programmatically? Why are Your Views overlapping? I think You have to set the layout Paramaters for Your view container:
//after onCreate in the activity, create the LinearLayout (for example)
//and set the LayoutParams, if You have no one declared in Your xml
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//then create the imageView, set the image inside and add it to the container
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.your_image);
imageView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
linearLayout.addView(imageView);
//set all as content
setContentView(linearLayout);
Have You done it in this way? If not, try it, also if You want to set multiple imageViews or some other Layout, You have to play a little bit with the LayoutParams.

Related

Draggable Button getting out of RelativeLayout whenever dragged

I am developing an application in which I am using Drag and Drop functionality.
In that I am generating buttons OnClick.The code is working fine but when I drag the button to the corners of the relativelayout,button gets out of the layout. I want it to stay inside of the layout.
Before Dragging the button
After Dragging the button (on top corner in this example)
As you can see that the button is getting out of the layout whenever I drag it towards the end/corner of the layout, I want it to stay in the layout intact, display full button. How can I do that?
Thanks in advance.!
MainActivity.java
public class MainActivity extends Activity implements OnTouchListener {
Button btnAddButton;
RelativeLayout rl1;
int i = 1;
private int _xDelta;
private int _yDelta;
ViewGroup _root;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAddButton = (Button) findViewById(R.id.btnAdd);
rl1 = (RelativeLayout) findViewById(R.id.relative_layout);
_root = (ViewGroup)findViewById(R.id.relative_layout);
btnAddButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
add(v);
}
});
}
public void add(View v) {
Button btn = new Button(MainActivity.this);
//btn.setId(i);
RelativeLayout.LayoutParams layoutParam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
int a=(int) (Math.random()*100);
// Toast.makeText(MainActivity.this, String.valueOf(Math.random()*100), 1).show();//double a=Math.random();
layoutParam.leftMargin = 30+a;
if (i > 1) {
layoutParam.addRule(RelativeLayout.BELOW, (i - 1));
}
btn.setText("Button" + i);
rl1.addView(btn, layoutParam);
btn.setOnTouchListener(this);
i++;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#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;
}
_root.invalidate();
return true;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="AddButton"
android:text="Button" />
<RelativeLayout
android:id="#+id/relative_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/btnAdd" >
</RelativeLayout>
</RelativeLayout>
I was able to tackle this by using these conditions:
if ((v.getY() < lay.getY())) {
v.setX(xVal);
v.setY(yVal);
} else if (v.getX() < lay.getX()) {
v.setX(xVal);
v.setY(yVal);
} else if (v.getX() + v.getWidth() > lay.getX() + lay.getWidth()) {
v.setX(xVal);
v.setY(yVal);
} else if (v.getY() + v.getHeight() > lay.getY() + lay.getHeight()) {
v.setX(xVal);
v.setY(yVal);
} else {
for (int i = 0; i <= viewIdList.size() - 1; i++) {
if (v.getId() != viewIdList.get(i).getId()) {
View v3 = viewIdList.get(i);
Rect rect1 = new Rect(v.getLeft(), v.getTop(),
v.getRight(), v.getBottom());
v.getHitRect(rect1);
Rect rect2 = new Rect(v3.getLeft(), v3.getTop(),
v3.getRight(), v3.getBottom());
v3.getHitRect(rect2);
if (Rect.intersects(rect1, rect2)) {
System.out.println("overlap");
v.setX(xVal);
v.setY(yVal);
}
where v is the view (ImageView in my case) and this code is written in MotionEvent.ACTION_UP of onTouchListener.

when move the image from bottom to up with animation,not getting the full image

when move the image from bottom to up with animation,not getting the full image,i am declaring imageview width wrap content.when i was moving the down its working perfectly,but when i image dragging up,the showing part comes with animation not getting the full image.
public class MainActivity extends Activity {
// mainLayout is the child of the HorizontalScrollView ...
private LinearLayout mainLayout;
// this is an array that holds the IDs of the drawables ...
private int[] images = { R.drawable.gg, R.drawable.gg, R.drawable.gg,
R.drawable.gg, R.drawable.gg, R.drawable.gg };
int status = 0;
int Measuredwidth = 0, MeasuredHeight = 0;
float x = 0;
boolean first = true;
ImageButton btn;
View cell = null;
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
PointF startPoint = new PointF();
static final int NONE = 0;
static final int DRAG = 1;
Bitmap icon;
int mode = NONE;
AnimationListener animx;
/** Called when the activity is first created. */
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
mainLayout = (LinearLayout) findViewById(R.id.linearLayout);
for (int i = 0; i < images.length; i++) {
cell = getLayoutInflater().inflate(R.layout.image, null);
final ImageView img = (ImageView) cell.findViewById(R.id.imageView);
img.setImageResource(images[i]);
// mainLayout.addView(cell);
// }
final LinearLayout ll = (LinearLayout) cell
.findViewById(R.id.lllayout);
Point size = new Point();
WindowManager w = getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(size);
MeasuredHeight = size.y;
Measuredwidth = size.x;
} else {
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
MeasuredHeight = d.getHeight();
}
// final AbsoluteLayout aalayout = (AbsoluteLayout) cell
// .findViewById(R.id.absLayout);
btn = (ImageButton) cell.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "button 1 pressed",
2000).show();
}
});
img.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(final View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
status = 0;
System.out.println("sdhsjdkklkl");
} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (first) {
x = event.getX();
first = false;
System.out.println("matrix: " + matrix);
savedMatrix.set(matrix);
mode = DRAG;
startPoint.set(img.getLeft(), event.getY());
} else {
startPoint.set(img.getLeft(), event.getY());
}
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
float dx = event.getX() - startPoint.x;
final float dy = event.getY() - startPoint.y;
float setheight = btn.getBottom() + 6;
if (dy > 0) {
System.out.println("saved matrix:" + savedMatrix);
matrix.set(savedMatrix);
System.out.println("y value: " + dy);
matrix.postTranslate(img.getLeft(), 80);
ll.setVisibility(View.VISIBLE);
img.setImageMatrix(matrix);
}
else if (dy < 0) {
int fromLoc[] = new int[2];
img.getLocationOnScreen(fromLoc);
final float startX = fromLoc[0];
final float startY = fromLoc[1];
int toLoc[] = new int[2];
img.getLocationOnScreen(toLoc);
final float destX = 0;
final float destY = img.getTop();
ll.setVisibility(View.GONE);
TranslateAnimation mAnimation = new TranslateAnimation(
0, 0, 0, -80);
mAnimation.setDuration(10000);
mAnimation.setRepeatCount(0);
mAnimation
.setInterpolator(new LinearInterpolator());
mAnimation.setFillAfter(true);
img.setAnimation(mAnimation);
}
}
return true;
}
});
System.out.println("bmgbgklb");
mainLayout.addView(cell);
}
}
}
my main xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dip" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
my sub xml is:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="2dip"
android:scaleType="matrix"
android:src="#drawable/dd" >
</ImageView>
<LinearLayout
android:id="#+id/lllayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="invisible" >
<ImageButton
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dip"
android:background="#drawable/orangearrow_over"
android:focusable="true" />
<ImageButton
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="#drawable/orangearrow_over" />
<ImageButton
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:background="#drawable/orangearrow_over" />
</LinearLayout>
</FrameLayout>

when i was dragging down the image,the button is not working

In my xml on button i want to declare image,when i was dragging down the image the button clicking was not working,it was in background,it does come the forground.
How can it be possible to use button? I want to display toast meassage when i click on button.
public class MainActivity extends Activity {
/** Called when the activity is first created. */
ImageView img = null;
AbsoluteLayout aLayout;
int status = 0;
int Measuredwidth = 0, MeasuredHeight = 0;
float x = 0;
boolean first = true;
Bitmap icon;
Button b;
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aLayout = (AbsoluteLayout) findViewById(R.id.absLayout);
img = (ImageView) findViewById(R.id.imageView);
icon = BitmapFactory.decodeResource(getResources(), R.drawable.gg);
b = (Button) findViewById(R.id.btnFavorites);
Point size = new Point();
WindowManager w = getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(size);
MeasuredHeight = size.y;
Measuredwidth = size.x;
} else {
Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
MeasuredHeight = d.getHeight();
}
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
b.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "activity closed",
Toast.LENGTH_LONG).show();
}
});
aLayout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP) {
status = 0;
// img.setBackgroundColor(Color.TRANSPARENT);
} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (first) {
x = event.getX();
first = false;
}
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
//
}
if ((event.getX() < (Measuredwidth - icon.getWidth() / 2) && event
.getX() > (icon.getWidth() / 2))
&& (event.getY() < MeasuredHeight
- (icon.getHeight() + 60) && event.getY() > icon
.getHeight() / 2)) {
#SuppressWarnings("deprecation")
// (int) event.getX()-img.getWidth()/2
LayoutParams lp = new AbsoluteLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, (int) 0, (int) event
.getY() - img.getHeight() / 2);
img.setLayoutParams(lp);
}
return true;
}
});
}
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/btnFavorites"
android:layout_width="match_parent"
android:layout_height="45dip"
android:drawablePadding="10dp"
android:paddingLeft="10dp"
android:layout_marginTop="1dip"
android:gravity="left|center_vertical"
android:background="#242D41"
android:drawableLeft="#drawable/ic_launcher"
android:includeFontPadding="false"
android:textColor="#ffffff"
android:textSize="15dip"
android:textStyle="bold" />
<AbsoluteLayout
android:id="#+id/absLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="1dip" >
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/gg"
android:scaleType="matrix" >
</ImageView>
</AbsoluteLayout>
</FrameLayout>
if you place image on button , Button will lose control so , you can give drawable (image) to button using background or drawable left or right top bottom ...or else you can implement on click listener even on image view..
Update..
<Button
android:id="#+id/btnFavorites"
android:layout_width="match_parent"
android:layout_height="45dip"
android:drawablePadding="10dp"
android:paddingLeft="10dp"
android:layout_marginTop="1dip"
android:gravity="left|center_vertical"
android:background="#242D41"
android:drawableLeft="#drawable/favorite_slide"
android:includeFontPadding="false"
android:text="#string/titleFavourites"
android:textColor="#ffffff"
android:textSize="15dip"
android:textStyle="bold" />
in the code i have given drawableleft, same yo can give for right top left and even padding also possible , and apply your logic for touch

Adding multiple image views programmatically

I'm basically trying to achieve drag&drop feature..
What i'm trying is that i provides sequence of images on the screen, if i click on any image available in images row that will be added to the Mains screen. But i'm getting problem that when i add new view into the Main Screen then all the other views also moved to top left corner.
Can you please tell me what is the problem...? Or kindly suggest me a tutorial or link where i can find solution.... or how to achieve this ?
I'm using Framelayout, So that i also achieve images overlapping...
This is the class in which all code is working:
public class drag extends Activity implements OnClickListener, OnTouchListener
{
ImageView img1;
Button btn,btn2;
FrameLayout layout;
LayoutParams params;
ImageView im , im2, im3 ,im4;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (FrameLayout) findViewById(R.id.vg);
layout.setDrawingCacheEnabled(true);
im = (ImageView)findViewById(R.id.img1);
im.setDrawingCacheEnabled(true);
im.setOnTouchListener(this);
im.setOnClickListener(this);
btn = (Button)findViewById(R.id.btn1);
btn.setDrawingCacheEnabled(true);
btn.setOnClickListener(this);
btn.setOnTouchListener(this);
btn2 = (Button)findViewById(R.id.btn2);
btn2.setDrawingCacheEnabled(true);
btn2.setOnClickListener(this);
btn2.setOnTouchListener(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
im2 = new ImageView(drag.this);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
im2.setImageBitmap(bm);
im2.setOnTouchListener(drag.this);
im2.setOnClickListener(drag.this);
layout.addView(im2, params);
}
});
btn2.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
saveImage(bm);
}
});
}
public void saveImage(Bitmap myBitmap)
{
MediaStore.Images.Media.insertImage(getContentResolver(), myBitmap, "mmsImage" , "mmsimage");
}
int l, t, r, b;
int oldLeft, oldTop;
PointF p, curr;
#Override
public boolean onTouch(View view, MotionEvent me)
{
if (me.getAction() == MotionEvent.ACTION_DOWN)
{
//status = START_DRAGGING;
Log.i("status"," AAA dOWN");
img1 = new ImageView(this);
view.setDrawingCacheEnabled(true);
Bitmap mmsImage = Bitmap.createBitmap(view.getDrawingCache());
img1.setImageBitmap(mmsImage);
img1.setOnTouchListener(drag.this);
img1.setOnClickListener(drag.this);
oldLeft = (int)view.getLeft();
oldTop = (int)view.getTop();
p = new PointF(me.getRawX(), me.getRawY());
}
if (me.getAction() == MotionEvent.ACTION_MOVE)
{
Log.i("status"," AAA draging");
int xDiff = (int)(me.getRawX() - p.x);
int yDiff = (int)(me.getRawY() - p.y);
p.x = me.getRawX();
p.y = me.getRawY();
l = view.getLeft();
t = view.getTop();
r = view.getRight();
b = view.getBottom();
view.layout(l + xDiff, t + yDiff , r + xDiff, b + yDiff);
}
if (me.getAction() == MotionEvent.ACTION_UP)
{
Log.i("status"," AAA UP");
//captureUserMove(view);
}
return false;
}
}
Here is the XML :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/vg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</FrameLayout>
You set params, but don't specify how to position the added view. Try this:
In onCreate()
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.LEFT | Gravity.TOP; // you need this for margins to work
In the click listener:
// your x and y where you want the new view
params.leftMargin = x;
params.topMargin = y;
Putting
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
inside
#Override
public void onClick(View v)
{
}
will work.

Why moving of GroupView works bad?

I made two nested RelativeLayouts, filled one with some TextViews and made reaction on finger drag guesture.
But it works badly:
1) moving group leaves garbage traces as if background does not redraw
2) the column with leftMargin==800 does not draw
The XML layout code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/stator"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:id="#+id/mover"
android:background="#android:color/darker_gray"
>
</RelativeLayout>
</RelativeLayout>
The java code:
public class SymbolPadActivity extends Activity {
private RelativeLayout mover;
private RelativeLayout stator;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mover = (RelativeLayout) findViewById(R.id.mover);
RelativeLayout.LayoutParams labelParams;
TextView textView;
for(int leftMargin = 0; leftMargin<1500; leftMargin += 200) {
for(int topMargin=0; topMargin<800; topMargin += 80) {
// I can't omit these 3 lines
labelParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
labelParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
labelParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
labelParams.leftMargin = leftMargin;
labelParams.topMargin = topMargin;
textView = new TextView(this);
textView.setText("(" + leftMargin + "," + topMargin + ")");
mover.addView(textView, labelParams);
}
}
stator = (RelativeLayout) findViewById(R.id.stator);
stator.setOnTouchListener(new View.OnTouchListener() {
private int startleft, starttop;
private float startx, starty;
private boolean started;
#Override
public boolean onTouch(View v, MotionEvent event) {
if( event.getActionMasked() == MotionEvent.ACTION_DOWN ) {
started = true;
startx = event.getX();
starty = event.getY();
startleft = mover.getLeft();
starttop = mover.getTop();
return true;
}
else if( event.getActionMasked() == MotionEvent.ACTION_UP ) {
started = false;
startx = starty = 0;
return true;
}
else if( event.getActionMasked() == MotionEvent.ACTION_MOVE ) {
mover.setLeft( startleft + (int)(event.getX() - startx) );
mover.setTop( starttop + (int)(event.getY() - starty) );
return true;
}
else {
return false;
}
}
});
}
}
How to implement my goal correctly (in brief)?

Categories

Resources