Last active
August 29, 2015 14:14
-
-
Save bouchtaoui-dev/780a9a5d189d56a2a3f6 to your computer and use it in GitHub Desktop.
A simple class that makes a view blink, without the need of Thread or Timer objects. The rest is self explanatory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.graphics.drawable.AnimationDrawable; | |
import android.graphics.drawable.Drawable; | |
import android.util.AttributeSet; | |
import android.widget.ImageView; | |
/** | |
* | |
* @author Andaluz | |
* It's free like freedom, but with respect to each other :)' | |
*/ | |
public class BlinkImageView extends ImageView { | |
private final String TAG = "BlinkImageView"; | |
private AnimationDrawable mAniFrame; | |
private boolean isFirstTime; | |
private int timeOn; | |
private int timeOff; | |
private int resourceOn; | |
private int resourceOff; | |
public BlinkImageView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
isFirstTime = true; | |
timeOn = 500; | |
timeOff = 500; | |
resourceOn = -1; | |
resourceOff = -1; | |
} | |
public BlinkImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
isFirstTime = true; | |
timeOn = 500; | |
timeOff = 500; | |
resourceOn = -1; | |
resourceOff = -1; | |
} | |
public BlinkImageView(Context context) { | |
super(context); | |
isFirstTime = true; | |
timeOn = 500; | |
timeOff = 500; | |
resourceOn = -1; | |
resourceOff = -1; | |
} | |
public void setResources(int off, int on) { | |
resourceOff = off; | |
resourceOn = on; | |
} | |
public void setDurationTime(int off, int on) { | |
if (off != 0) | |
timeOff = off; | |
if(on!=0) | |
timeOn = on; | |
} | |
public void doBlink() { | |
if(isFirstTime) { | |
initializeAnimationDrawable(); | |
isFirstTime = false; | |
} else | |
mAniFrame.setVisible(false, true); // <-- a little hack, this will reset the animation. | |
mAniFrame.start(); | |
} | |
private void initializeAnimationDrawable() { | |
mAniFrame = new AnimationDrawable(); | |
mAniFrame.setOneShot(true); | |
mAniFrame.addFrame(getResources().getDrawable(resourceOn), timeOn); | |
mAniFrame.addFrame(getResources().getDrawable(resourceOff), timeOff); | |
setBackgroundDrawable((Drawable)mAniFrame); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment