I am able to drag Image but when I drag image my image size keep changing. Image size is not fixed. please let me know where I am wrong. When I drag image it keeps changing size of perticular image. There something about layout params but I am not able to fix it.
Xml of my drag area.
<LinearLayout
android:id="@+id/vg1"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:gravity="center"
android:weightSum="1"
android:layout_alignTop="@+id/scrollView1"
android:layout_toLeftOf="@+id/scrollView2"
android:layout_toRightOf="@+id/scrollView1"
android:gravity="right"
android:background="@drawable/shape" >
<ImageView
android:id="@+id/dragimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:src="@drawable/log_off_button" />
</LinearLayout>
My Drag Image code:
OnTouchListener dragt = new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(v.getLayoutParams());
v.setLayoutParams(par);
switch(v.getId())
{//What is being touched
case R.id.dragimg:
{//Which action is being taken
switch(event.getAction())
{
case MotionEvent.ACTION_MOVE:
{
par.topMargin = (int)event.getRawY() + windowheight - (vg.getHeight()/2);
par.leftMargin = (int) ((int)event.getRawX() +windowwidth - (vg.getWidth()));
v.setLayoutParams(par);
break;
}//inner case MOVE
case MotionEvent.ACTION_UP:
{
par.topMargin = (int)event.getRawY()+ windowheight - (vg.getHeight()/2);
par.leftMargin = (int) ((int)event.getRawX() + windowwidth - (vg.getWidth()));
v.setLayoutParams(par);
break;
}//inner case UP
case MotionEvent.ACTION_DOWN:
{
// par.height = 60;
// par.width = 60;
v.setLayoutParams(par);
break;
}
}
break;
}
}
return true;
}
};
Please let me know where I am wrong or better way to do this. There will be only 1 image for drag and drop.