Use timer and Timertask() or Dynamic image generation WITH Alpha Animation in Android

Posted: May 12, 2011 in Uncategorized

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—————————————————————-

Advertisements
Comments
  1. Mr WordPress says:

    Hi, this is a comment.
    To delete a comment, just log in, and view the posts’ comments, there you will have the option to edit or delete them.

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