Repository URL to install this package:
package com.vipera.splashscreen;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Display;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.vipera.de.utility.logging.impl.DELoggerFactory;
import org.apache.cordova.CordovaInterface;
import org.slf4j.Logger;
/**
* Created by enrico on 23/02/17.
*/
public class BaseSplashScreen {
private static final Logger LOGGER= DELoggerFactory.getLogger(BaseSplashScreen.class);
private final boolean destroyOnHide;
private int fadeDuration = 0;
private Dialog splashDialog;
private CordovaInterface cordova;
private int drawableId = 0;
private int backgroundColor = Color.BLACK;
private ImageView splashImageView;
public BaseSplashScreen(CordovaInterface cordova, int drawableId,boolean destroyOnHide){
LOGGER.debug("drawableId: {}, destroyOnHide: {}",drawableId,destroyOnHide);
this.cordova=cordova;
this.drawableId=drawableId;
this.destroyOnHide=destroyOnHide;
crateSplashDialog();
}
private void crateSplashDialog(){
LOGGER.debug("crateSplashDialog");
initImageView();
initDialog();
}
private void initDialog() {
splashDialog = new Dialog(cordova.getActivity(), android.R.style.Theme_Translucent_NoTitleBar);
if ((cordova.getActivity().getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN)
== WindowManager.LayoutParams.FLAG_FULLSCREEN) {
splashDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
splashDialog.setContentView(splashImageView);
splashDialog.setCancelable(false);
}
private void initImageView(){
Display display = cordova.getActivity().getWindowManager().getDefaultDisplay();
Context context = cordova.getActivity();
splashImageView = new ImageView(context);
splashImageView.setImageResource(drawableId);
ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
splashImageView.setLayoutParams(layoutParams);
splashImageView.setMinimumHeight(display.getHeight());
splashImageView.setMinimumWidth(display.getWidth());
splashImageView.setBackgroundColor(backgroundColor);
splashImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
public int getFadeDuration() {
return fadeDuration;
}
public void setFadeDuration(int fadeDuration) {
this.fadeDuration = fadeDuration;
}
public void show() {
if(splashDialog == null){
crateSplashDialog();
}
if (splashDialog.isShowing()) {
return;
}
splashDialog.show();
}
public void hide() {
if (splashDialog == null || !splashDialog.isShowing()) {
return;
}
Animation animation=getHideAnimation(getFadeDuration());
if(animation == null){
splashDialog.dismiss();
if(destroyOnHide){
destroy();
}
return;
}
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (splashDialog != null && splashDialog.isShowing()) {
splashDialog.dismiss();
if(destroyOnHide){
destroy();
}
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
splashImageView.setAnimation(animation);
splashImageView.startAnimation(animation);
}
public void destroy() {
LOGGER.debug("destroy");
splashDialog = null;
splashImageView = null;
}
public int getBackgroundColor() {
return backgroundColor;
}
public void setBackgroundColor(int backgroundColor) {
this.backgroundColor = backgroundColor;
}
public void setBackgroundColor(String backgroundColor) {
this.backgroundColor = Color.parseColor(backgroundColor);
}
public Animation getHideAnimation(int fadeSplashScreenDuration){
if(fadeSplashScreenDuration == 0 ){
return null;
}
AlphaAnimation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new DecelerateInterpolator());
fadeOut.setDuration(fadeSplashScreenDuration);
return fadeOut;
}
}