Android sample App : Drag And Drop Image using Touch

Posted: May 13, 2011 in Uncategorized

Hello,

Below code is for drag and drop image in any location on android screen phone….

these all done using TouchListner…

check it out.

Drag ANd Drop Image

Drag ANd Drop Image

 TouchActivity.java

package com.example.Touch;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.AbsoluteLayout.LayoutParams;

public class TouchActivity extends Activity{
/** Called when the activity is first created. */
ImageView img=null;
AbsoluteLayout aLayout;
int status=0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
aLayout= (AbsoluteLayout)findViewById(R.id.absLayout);

img=(ImageView)findViewById(R.id.imageView);

//sa.setFillAfter(true);

img.setOnTouchListener(new OnTouchListener() {

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

status=1;
Log.i(“ImageStatus”,””+status);
//img.setBackgroundColor(Color.WHITE);

return false;
}
});
aLayout.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.i(“touch”,””+event);

if(status==1) // any event from down and move
{
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,(int)event.getX()-img.getWidth()/2,(int)event.getY()-img.getHeight()/2);
img.setLayoutParams(lp);

}
if(event.getAction()==MotionEvent.ACTION_UP){
status=0;

img.setBackgroundColor(Color.TRANSPARENT);

}
return true;
}
});
}

}

——————————————————————————————————————————————-

Main.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:id=”@+id/LLayout”
>
<AbsoluteLayout
android:id=”@+id/absLayout”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<ImageView android:layout_height=”wrap_content” android:id=”@+id/imageView” android:layout_width=”wrap_content” android:src=”@drawable/icon”></ImageView>
</AbsoluteLayout>
</LinearLayout>

………………………………………………………………………..

Ask me if any query :)

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s