Pages

About

Pages - Menu

Ads 468x60px

Social Icons

Main Menu

Featured Posts

Tuesday, 8 July 2014

Android sample App : Drag And Drop Image using Touch

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>

0 comments:

Post a Comment

Followers