-1

Possible Duplicate:
how to drag an image by touching in android?

how to moving bitmap left and righ with drag finger with may destination point boundary

ilustration

B--------o1-----------o2-----------o3

B = Bitmap

o1 = my destination point one

o2 = my destination point two

o3 = my destination point three

if bitmap drag to righ then bitmap on my destination point one

---------B---------o2-----------o3

if bitmap on my destination point one drag to right then bitmap change to my destination point two

----------o1--------B-------------o3

if bitmap on my destination point two drag to left then bitmap back to my destination point one

----------B--------o2-------------o3

and so on..

how to implement my idea to android code?

please give me sample code because i am newbie on android.

Community
  • 1
  • 1
kembang
  • 9
  • 2

2 Answers2

0

XML layout:

<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"
        android:layout_marginLeft="10px"
        android:src="@drawable/image" />

</LinearLayout>

DragActivity:

public class DragActivity extends Activity {

    private View selected_item = null;
    private int offset_x = 0;
    private int offset_y = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drag);
    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:
                                            int x = (int)event.getX() - offset_x;
                                            int y = (int)event.getY() - offset_y;
                    int w = getWindowManager().getDefaultDisplay().getWidth()- 70;
                    int h = getWindowManager().getDefaultDisplay().getHeight()- 70;
                    if(x > w)
                        x = w;
                    if(y > h)
                        y = h;
                                     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                                    new ViewGroup.MarginLayoutParams(
                                                    LinearLayout.LayoutParams.WRAP_CONTENT,
                                                    LinearLayout.LayoutParams.WRAP_CONTENT));
                                     lp.setMargins(x, y, 0, 0);
                                            selected_item.setLayoutParams(lp);
                                            break;
                                    default:
                                            break;
                            }
                            return true;
                    }
});
   ImageView img = (ImageView)findViewById(R.id.img);
    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;
                                            break;
                                    default:
                                            break;
                            }

                            return false;
                    }
            });
}

}
kittu88
  • 2,451
  • 5
  • 40
  • 80
0

Why don't you try this tutorial By use of onTouchEvent will give you whatever you want as per that example tutorial link. Sample code -

@Override
public boolean onTouchEvent(MotionEvent event) {
    mX = (int) event.getX() - mBitmap.getWidth() / 2;
    mY = (int) event.getY() - mBitmap.getHeight() / 2;
    return super.onTouchEvent(event);
}
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173