Blinking Santa Handling alpha feature

December 16th, 2011 by aabhaanjan

In this article we are going to learn how to make an blinking image on touch or click.
There are following steps we need to follow to make Blinking Santa.

Step 1: Create a Project using Motodev. The options will look like this.
1

Step 2: in your res/drwable folder put the image santa.png

Step 3. Open the layout/Main.xml and put following codes

[xml]


[/xml]

Step 4. Open the MainActivity.java and put following codes
[xml]

package com.blinkingsanta;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.animation.Animation;

import android.view.animation.Animation.AnimationListener;

import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener, AnimationListener {

private ImageView _santaImage;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

_santaImage = (ImageView) findViewById(R.id.image);

_santaImage.setOnClickListener(this);

}

@Override

public void onClick(View view) {

if (view.getId() == R.id.image) {

Animation animation;

if (_santaImage.getVisibility() == View.VISIBLE) {

animation = new VisibilityClass(3, true);
}

else
{

animation = new VisibilityClass(3, false);

}

animation.setDuration(1000L);

animation.setAnimationListener(this);
_santaImage.startAnimation(animation);

}

}

@Override

public void onAnimationEnd(Animation animation) {

_santaImage.setVisibility(View.VISIBLE);

}

@Override

public void onAnimationRepeat(Animation animation) {

}

@Override

public void onAnimationStart(Animation animation) {

}

}

[/xml]

Step 5. Create a new class VisibilityClass.java and put following codes

[xml]

package com.blinkingsanta;

import android.util.FloatMath;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class VisibilityClass extends Animation {
private int _totalNumber;
private boolean _isAnimFinished;

public VisibilityClass(int _totalNumber, boolean _isAnimFinished) {
this._totalNumber = _totalNumber;
this._isAnimFinished = _isAnimFinished;
}

@Override
protected void applyTransformation(float _interpolatedTime, Transformation _trans) {
float period = _interpolatedTime * _totalNumber * 4.12f + (_isAnimFinished ? 4.12f / 2 : 0);
_trans.setAlpha(Math.abs(FloatMath.cos(period)));
}

}

[/xml]

Step 6. Run the app and Click on Santa . It should look like this 🙂

2

Tags: , , , , , , , , , , , , ,