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 ­čÖé

Hello friends,

This is the example for how to use Timer and Timertask() in Android development.

when you want to generate some task repeatedly after some time interval so for that need u can use these.

This example also contains Alpha animation Sample. Images start showing with alpha animation.

See whole code below.

Alpha Animation & Timer

Source Code:

ImagePlayActivity.Java…………………………………………………………………………………………………………………….

package com.example.ImagePlay;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

//import com.example.ImagePlay.*;
import com.example.ImagePlay.*;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.AbsoluteLayout.LayoutParams;

public class ImagePlayActivity extends Activity {
    /** Called when the activity is first created. */
    Button btn = null;
    AbsoluteLayout container = null;
    int counter = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        container = (AbsoluteLayout)findViewById(R.id.absLayout);
        ImageView toAdd = new ImageView(this);
       // generate();
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                timerMethod();
            }
        }, 0,1000);
        
        
     
    }
    private void timerMethod()
    {
    this.runOnUiThread(generate);
    }
    private Runnable generate= new Runnable() {
        
        @Override
        public void run() {
            // TODO Auto-generated method stub
            Log.i(“MARKER”,”******************************************”);
            //int numViews = container.getChildCount();    
            
            ImageView toAdd = new ImageView(ImagePlayActivity.this);        
            Drawable imgContent = ImagePlayActivity.this.getResources().getDrawable(R.drawable.icon);
            toAdd.setImageDrawable(imgContent);
            toAdd.setTag(“img”+counter++);
            
            Random rndGen = new Random();
            LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,rndGen.nextInt(300),rndGen.nextInt(300));
            toAdd.setLayoutParams(lp);
            toAdd.setBackgroundColor(Color.TRANSPARENT);
            
            AlphaAnimation anim = new AlphaAnimation(0, 1);
            anim.setDuration(1000);
            container.addView(toAdd);
            //container.invalidate();
            toAdd.startAnimation(anim);
            
            
        
        }
    };

        
}

main.Xml……………………………………………………………………………………………………………………………………………..

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
android:id=”@+id/LinLayout”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical”
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
>

<AbsoluteLayout
android:id=”@+id/absLayout”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
>
</AbsoluteLayout>
</LinearLayout>
———————————-end—————————————————————-