Created
September 27, 2018 23:02
-
-
Save bubbleguuum/a9430813854e2ba60e8607c53a759058 to your computer and use it in GitHub Desktop.
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
package com.bubblesoft.android.utils; | |
import android.app.Activity; | |
import android.graphics.drawable.Drawable; | |
import android.os.Handler; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.animation.AlphaAnimation; | |
import android.view.animation.Animation; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
public class IconToast { | |
private static final int ANIMATION_DURATION = 600; | |
private static final int HIDE_DELAY = 2000; | |
private View mContainer; | |
private ImageView mImageView; | |
private TextView mTextView; | |
private boolean mShowing; | |
private Handler mHandler; | |
private AlphaAnimation mFadeInAnimation; | |
private AlphaAnimation mFadeOutAnimation; | |
public IconToast(Activity activity, int layoutResId) { | |
ViewGroup container = (ViewGroup) activity.findViewById(android.R.id.content); | |
View v = activity.getLayoutInflater().inflate(layoutResId, container); | |
init(v); | |
} | |
public IconToast(View v) { | |
init(v); | |
} | |
private void init(View v) { | |
mContainer = v.findViewById(R.id.container); | |
mContainer.setVisibility(View.GONE); | |
mTextView = v.findViewById(R.id.text); | |
mImageView = v.findViewById(R.id.icon); | |
mFadeInAnimation = new AlphaAnimation(0.0f, 1.0f); | |
mFadeInAnimation.setDuration(ANIMATION_DURATION); | |
mFadeOutAnimation = new AlphaAnimation(1.0f, 0.0f); | |
mFadeOutAnimation.setDuration(ANIMATION_DURATION); | |
mFadeOutAnimation.setAnimationListener(new Animation.AnimationListener() { | |
@Override | |
public void onAnimationStart(Animation animation) { | |
} | |
@Override | |
public void onAnimationEnd(Animation animation) { | |
mContainer.setVisibility(View.GONE); | |
mShowing = false; | |
} | |
@Override | |
public void onAnimationRepeat(Animation animation) { | |
} | |
}); | |
mHandler = new Handler(); | |
} | |
public void show(Drawable drawable, String text) { | |
mContainer.setVisibility(View.VISIBLE); | |
mTextView.setVisibility(text == null ? View.GONE : View.VISIBLE); | |
mTextView.setText(text); | |
mImageView.setVisibility(drawable == null ? View.GONE : View.VISIBLE); | |
mImageView.setImageDrawable(drawable); | |
if(mShowing) { | |
mHandler.removeCallbacks(mHideRunnable); | |
} else { | |
mContainer.startAnimation(mFadeInAnimation); | |
} | |
mHandler.postDelayed(mHideRunnable, HIDE_DELAY); | |
mShowing = true; | |
} | |
private final Runnable mHideRunnable = new Runnable() { | |
@Override | |
public void run() { | |
mContainer.startAnimation(mFadeOutAnimation); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice. Thank you.
What is the licence on this? Can I use it in a "GPL v2 or later" project?
Can I change the package name, to avoid namespace fragmentation?