package com.powenko.Tutorial_Animation_Alpha;

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

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

public class Tutorial_Animation_AlphaActivity 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();
            Context context = getApplicationContext();
            ImageView toAdd = new ImageView(context);
            Drawable imgContent = context.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);

        }
    };

}
<!--?xml version="1.0" encoding="utf-8"?-->

By admin-powenko

Dr. Powen Ko is a teacher and CEO on LoopTek LLC, and like to teaching. if you need to class, please let PowenKo know, he will love to service and sharing. LoopTek web site is www.looptek.com

Leave a Reply