Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

two image dragging

Tags:

android

I am making a module in which I have two images whenever i touch one image it should follow the finger or mouse(in emulator) on drag and if it comes over the other image then they change their positions where the first image was on first touch(ACTION_DOWN) . i have written the following code in which the views are moving but when i drag the first image second is also dragged. Further would like to have idea on how to change the positions.

.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/vg"
   >

<ImageView   
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
       />
 <ImageView   
    android:id="@+id/img1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
</LinearLayout>

activity file

public class MainActivity extends Activity {
    private View selected_item = null;
    private int offset_x = 0;
    private int offset_y = 0;
    Canvas can;
    Paint paint;
    ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ViewGroup vg = (ViewGroup)findViewById(R.id.vg);
    vg.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                            switch(event.getActionMasked())
                            {

                                    case MotionEvent.ACTION_MOVE:
                                        if(selected_item == img) {
                                            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;
                    if(x > w)
                        x = w;
                    if(y > h)
                        y = h;
                                     LinearLayout.LayoutParams lp = new  LinearLayout.LayoutParams(
                                    new ViewGroup.MarginLayoutParams(
                                                    100,
                                                    100));
                                     lp.setMargins(x, y, 0, 0);

                                            selected_item.setLayoutParams(lp); 
                                        }
                                            break;
                                    default:
                                            break;
                            }
                            return true;
                    }
  });
   img = (ImageView)findViewById(R.id.img);

   //timerDelayRemoveView(500, img);

   BitmapDrawable drawable = (BitmapDrawable)getResources().getDrawable(R.drawable.imagesl_02); 
   Bitmap bitmap = drawable.getBitmap();
   Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
   img.setImageBitmap(scaledBitmap);
   LinearLayout.LayoutParams lp0 = new LinearLayout.LayoutParams(100, 100);
   lp0.leftMargin = 0;
   lp0.topMargin = 0;
   img.setLayoutParams(lp0);
   //vg.addView(img, lp1);
  // vg.addView(img, 1);
    img.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                            switch(event.getActionMasked())
                            {
                                    case MotionEvent.ACTION_DOWN:
                                            offset_x = (int)event.getX();
                                            offset_y = (int)event.getY();
                                            selected_item = v;
                          Toast.makeText(MainActivity.this, "down",Toast.LENGTH_SHORT).show();
                                            break;

                            default: break;        
                            }

                            return false;
                    }
            });

    ImageView img1 = (ImageView)findViewById(R.id.img1);
    BitmapDrawable drawable1 = (BitmapDrawable)getResources().getDrawable(R.drawable.realimage); 
    Bitmap bitmap1 = drawable1.getBitmap();
    Bitmap scaledBitmap1 = Bitmap.createScaledBitmap(bitmap1, 100, 100, true);
    img1.setImageBitmap(scaledBitmap1);
    LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(100, 100);
    lp1.leftMargin = 100;
    lp1.topMargin = 100;
    img1.setLayoutParams(lp1);

    //img.setImageBitmap(scaledBitmap1);

    img1.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                            switch(event.getActionMasked())
                            {
                                    case MotionEvent.ACTION_DOWN:
                                           // offset_x = (int)event.getX();
                                           // offset_y = (int)event.getY();
                                            selected_item = v;
                                            break;
                                    default:
                                            break;
                            }

                            return false;
                    }
            }); 

}
  }
like image 203
karan421 Avatar asked Aug 10 '12 06:08

karan421


2 Answers

The second Image is getting dragged because you have the two in a LinearLayout. The second image's layout parameters are dependent on the first, meaning that as you adjust them to the right, the second image is bound to the right edge of the first.

If I were approaching this problem (Assuming I couldn't use the dragging API introduced in Honeycomb), I would first put everything in a RelativeLayout with 2 ImageViews. When you "pick up" one of the images, you adjust its layout parameters as you move it around, and then when you drop it you adjust the LayoutParams of both to meet your desired layout.

like image 85
JRaymond Avatar answered Sep 25 '22 07:09

JRaymond


findViewById(R.drawable.realimage) should be findViewById(R.id.realimage).

I think you should use id instead of drawable.

like image 22
Braj Avatar answered Sep 24 '22 07:09

Braj