Android studio SweetAlert for Android

找到个开源项目。github:https://github.com/pedant/sweet-alert-dialog

效果图:https://raw.githubusercontent.com/pedant/sweet-alert-dialog/master/change_type.gif


build.gradle

 compile 'cn.pedant.sweetalert:library:1.3'

colors.xml

<?

xml version="1.0" encoding="utf-8"?

> <resources> <color name="alertDialog_Progress_Color">#ff4c72dc</color> <color name="alertDialog_Progress_hintColor">#40dcd8d5</color> </resources>

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ProgressWheel">
        <attr name="progressIndeterminate" format="boolean" />
        <attr name="barColor" format="color" />
        <attr name="rimColor" format="color" />
        <attr name="rimWidth" format="dimension" />
        <attr name="spinSpeed" format="float" />
        <attr name="barSpinCycleTime" format="integer" />
        <attr name="circleRadius" format="dimension" />
        <attr name="fillRadius" format="boolean" />
        <attr name="barWidth" format="dimension" />
    </declare-styleable>
    <!-- From: file:/Users/nicolas/AndroidProjects/materialish-progress/library/src/main/res/values/strings.xml -->
    <eat-comment />
    <string name="app_name">Library</string>
</resources>

測试界面:


測试类 MainActivity

import android.graphics.Color;
import android.view.View;
import android.widget.Button;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by Administrator on 2015/7/1.
 */
public class MainActivity extends BasicActivity {

    @ViewInject(R.id.sweetAlertDialog)
    private Button btSweetAlertDialog;

    @ViewInject(R.id.basicMessage)
    private Button btBasicMessage;

    //...

    @Override
    public int getLayoutID() {
        return R.layout.activity_main;
    }

    @OnClick({R.id.sweetAlertDialog,R.id.basicMessage,R.id.titleUnder,R.id.error,R.id.warning,R.id.success,R.id.custom_icon,R.id.confirm_button,R.id.cancel_bindlistener,R.id.opup_confirming})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sweetAlertDialog:
                showSweetAlertDialog();
                break;
            case R.id.basicMessage:
                showBasicMessage();
                break;
            case R.id.titleUnder:
                showTitleUnder();
                break;
            case R.id.error:
                showError();
                break;
            case R.id.warning:
                showWarning();
                break;
            case R.id.success:
                showSuccess();
                break;
            case R.id.custom_icon:
                showCustmIcon();
                break;
            case R.id.confirm_button:
                showConfirming();
                break;
            case R.id.cancel_bindlistener:
                showCancelBind();
                break;
            case R.id.opup_confirming:
                showPupConfirming();
                break;
            default:
                break;
        }
    }

    private void showSweetAlertDialog() {
        final SweetAlertDialog pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
        pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.alertDialog_Progress_Color));
        pDialog.getProgressHelper().setBarWidth(5);
        pDialog.getProgressHelper().setCircleRadius(100);
        pDialog.getProgressHelper().setRimColor(getResources().getColor(R.color.alertDialog_Progress_hintColor));
        pDialog.getProgressHelper().setRimWidth(5);
        pDialog.setTitleText("载入中..");
        pDialog.setCancelable(false);
        pDialog.show();
        dismiss(pDialog);
    }

    private void showBasicMessage() {
        SweetAlertDialog pDialog=new SweetAlertDialog(this)
                .setTitleText("Here's a message!");
                pDialog.show();
    }

    private void showTitleUnder() {
        new SweetAlertDialog(this)
                .setTitleText("Here's a message!")
                .setContentText("It's pretty, isn't it?

") .show(); } private void showError(){ new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE) .setTitleText("Oops...") .setContentText("Something went wrong!") .show(); } private void showWarning(){ new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE) .setTitleText("Are you sure?") .setContentText("Won't be able to recover this file!") .setConfirmText("Yes,delete it!") .show(); } private void showSuccess(){ new SweetAlertDialog(this, SweetAlertDialog.SUCCESS_TYPE) .setTitleText("Good job!") .setContentText("You clicked the button!") .show(); } private void showCustmIcon(){ new SweetAlertDialog(this, SweetAlertDialog.CUSTOM_IMAGE_TYPE) .setTitleText("Sweet!") .setContentText("Here's a custom image.") .setCustomImage(R.drawable.ic_launcher) .show(); } private void showConfirming(){ new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE) .setTitleText("Are you sure?

") .setContentText("Won't be able to recover this file!") .setConfirmText("Yes,delete it!") .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() { @Override public void onClick(SweetAlertDialog sDialog) { sDialog.dismissWithAnimation(); } }) .show(); } private void showCancelBind(){ new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE) .setTitleText("Are you sure?") .setContentText("Won't be able to recover this file!") .setCancelText("No,cancel plx!") .setConfirmText("Yes,delete it!") .showCancelButton(true) .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() { @Override public void onClick(SweetAlertDialog sDialog) { sDialog.cancel(); } }) .show(); } private void showPupConfirming(){ new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE) .setTitleText("Are you sure?") .setContentText("Won't be able to recover this file!") .setConfirmText("Yes,delete it!") .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() { @Override public void onClick(final SweetAlertDialog pDialog) { /* sDialog .setTitleText("Deleted!") .setContentText("Your imaginary file has been deleted!") .setConfirmText("OK") .setConfirmClickListener(null) .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);*/ pDialog.changeAlertType(SweetAlertDialog.PROGRESS_TYPE); pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.alertDialog_Progress_Color)); pDialog.getProgressHelper().setBarWidth(5); pDialog.getProgressHelper().setCircleRadius(100); pDialog.getProgressHelper().setRimColor(getResources().getColor(R.color.alertDialog_Progress_hintColor)); pDialog.getProgressHelper().setRimWidth(5); pDialog.setTitleText("Please later..."); pDialog.setContentText(""); pDialog.setCancelable(false); mHandler.postDelayed(new Runnable() { @Override public void run() { pDialog.setTitleText("Deleted!") .setContentText("Your imaginary file has been deleted!") .setConfirmText("OK") .setConfirmClickListener(null) .changeAlertType(SweetAlertDialog.SUCCESS_TYPE); pDialog.show(); } }, 3000); } }) .show(); } public void dismiss(final SweetAlertDialog pDialog){ mHandler.postDelayed(new Runnable() { @Override public void run() { pDialog.dismiss(); } }, 5000); } }


 SweetAlertDialog

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Build.VERSION;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.Transformation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import cn.pedant.SweetAlert.OptAnimationLoader;
import cn.pedant.SweetAlert.ProgressHelper;
import cn.pedant.SweetAlert.SuccessTickView;
import cn.pedant.SweetAlert.R.anim;
import cn.pedant.SweetAlert.R.drawable;
import cn.pedant.SweetAlert.R.id;
import cn.pedant.SweetAlert.R.layout;
import cn.pedant.SweetAlert.R.style;
import com.pnikosis.materialishprogress.ProgressWheel;
import java.util.List;

public class SweetAlertDialog extends Dialog implements OnClickListener {
    private View mDialogView;
    private AnimationSet mModalInAnim;
    private AnimationSet mModalOutAnim;
    private Animation mOverlayOutAnim;
    private Animation mErrorInAnim;
    private AnimationSet mErrorXInAnim;
    private AnimationSet mSuccessLayoutAnimSet;
    private Animation mSuccessBowAnim;
    private TextView mTitleTextView;
    private TextView mContentTextView;
    private String mTitleText;
    private String mContentText;
    private boolean mShowCancel;
    private boolean mShowContent;
    private String mCancelText;
    private String mConfirmText;
    private int mAlertType;
    private FrameLayout mErrorFrame;
    private FrameLayout mSuccessFrame;
    private FrameLayout mProgressFrame;
    private SuccessTickView mSuccessTick;
    private ImageView mErrorX;
    private View mSuccessLeftMask;
    private View mSuccessRightMask;
    private Drawable mCustomImgDrawable;
    private ImageView mCustomImage;
    private Button mConfirmButton;
    private Button mCancelButton;
    private ProgressHelper mProgressHelper;
    private FrameLayout mWarningFrame;
    private SweetAlertDialog.OnSweetClickListener mCancelClickListener;
    private SweetAlertDialog.OnSweetClickListener mConfirmClickListener;
    private boolean mCloseFromCancel;
    public static final int NORMAL_TYPE = 0;
    public static final int ERROR_TYPE = 1;
    public static final int SUCCESS_TYPE = 2;
    public static final int WARNING_TYPE = 3;
    public static final int CUSTOM_IMAGE_TYPE = 4;
    public static final int PROGRESS_TYPE = 5;

    public SweetAlertDialog(Context context) {
        this(context, 0);
    }

    public SweetAlertDialog(Context context, int alertType) {
        super(context, style.alert_dialog);
        this.setCancelable(true);
        this.setCanceledOnTouchOutside(false);
        this.mProgressHelper = new ProgressHelper(context);
        this.mAlertType = alertType;
        this.mErrorInAnim = OptAnimationLoader.loadAnimation(this.getContext(), anim.error_frame_in);
        this.mErrorXInAnim = (AnimationSet)OptAnimationLoader.loadAnimation(this.getContext(), anim.error_x_in);
        if(VERSION.SDK_INT <= 10) {
            List childAnims = this.mErrorXInAnim.getAnimations();

            int idx;
            for(idx = 0; idx < childAnims.size() && !(childAnims.get(idx) instanceof AlphaAnimation); ++idx) {
                ;
            }

            if(idx < childAnims.size()) {
                childAnims.remove(idx);
            }
        }

        this.mSuccessBowAnim = OptAnimationLoader.loadAnimation(this.getContext(), anim.success_bow_roate);
        this.mSuccessLayoutAnimSet = (AnimationSet)OptAnimationLoader.loadAnimation(this.getContext(), anim.success_mask_layout);
        this.mModalInAnim = (AnimationSet)OptAnimationLoader.loadAnimation(this.getContext(), anim.modal_in);
        this.mModalOutAnim = (AnimationSet)OptAnimationLoader.loadAnimation(this.getContext(), anim.modal_out);
        this.mModalOutAnim.setAnimationListener(new AnimationListener() {
            public void onAnimationStart(Animation animation) {
            }

            public void onAnimationEnd(Animation animation) {
                SweetAlertDialog.this.mDialogView.setVisibility(8);
                SweetAlertDialog.this.mDialogView.post(new Runnable() {
                    public void run() {
                        if(SweetAlertDialog.this.mCloseFromCancel) {
                            SweetAlertDialog.super.cancel();
                        } else {
                            SweetAlertDialog.super.dismiss();
                        }

                    }
                });
            }

            public void onAnimationRepeat(Animation animation) {
            }
        });
        this.mOverlayOutAnim = new Animation() {
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                LayoutParams wlp = SweetAlertDialog.this.getWindow().getAttributes();
                wlp.alpha = 1.0F - interpolatedTime;
                SweetAlertDialog.this.getWindow().setAttributes(wlp);
            }
        };
        this.mOverlayOutAnim.setDuration(120L);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(layout.alert_dialog);
        this.mDialogView = this.getWindow().getDecorView().findViewById(16908290);
        this.mTitleTextView = (TextView)this.findViewById(id.title_text);
        this.mContentTextView = (TextView)this.findViewById(id.content_text);
        this.mErrorFrame = (FrameLayout)this.findViewById(id.error_frame);
        this.mErrorX = (ImageView)this.mErrorFrame.findViewById(id.error_x);
        this.mSuccessFrame = (FrameLayout)this.findViewById(id.success_frame);
        this.mProgressFrame = (FrameLayout)this.findViewById(id.progress_dialog);
        this.mSuccessTick = (SuccessTickView)this.mSuccessFrame.findViewById(id.success_tick);
        this.mSuccessLeftMask = this.mSuccessFrame.findViewById(id.mask_left);
        this.mSuccessRightMask = this.mSuccessFrame.findViewById(id.mask_right);
        this.mCustomImage = (ImageView)this.findViewById(id.custom_image);
        this.mWarningFrame = (FrameLayout)this.findViewById(id.warning_frame);
        this.mConfirmButton = (Button)this.findViewById(id.confirm_button);
        this.mCancelButton = (Button)this.findViewById(id.cancel_button);
        this.mProgressHelper.setProgressWheel((ProgressWheel)this.findViewById(id.progressWheel));
        this.mConfirmButton.setOnClickListener(this);
        this.mCancelButton.setOnClickListener(this);
        this.setTitleText(this.mTitleText);
        this.setContentText(this.mContentText);
        this.setCancelText(this.mCancelText);
        this.setConfirmText(this.mConfirmText);
        this.changeAlertType(this.mAlertType, true);
    }

    private void restore() {
        this.mCustomImage.setVisibility(8);
        this.mErrorFrame.setVisibility(8);
        this.mSuccessFrame.setVisibility(8);
        this.mWarningFrame.setVisibility(8);
        this.mProgressFrame.setVisibility(8);
        this.mConfirmButton.setVisibility(0);
        this.mConfirmButton.setBackgroundResource(drawable.blue_button_background);
        this.mErrorFrame.clearAnimation();
        this.mErrorX.clearAnimation();
        this.mSuccessTick.clearAnimation();
        this.mSuccessLeftMask.clearAnimation();
        this.mSuccessRightMask.clearAnimation();
    }

    private void playAnimation() {
        if(this.mAlertType == 1) {
            this.mErrorFrame.startAnimation(this.mErrorInAnim);
            this.mErrorX.startAnimation(this.mErrorXInAnim);
        } else if(this.mAlertType == 2) {
            this.mSuccessTick.startTickAnim();
            this.mSuccessRightMask.startAnimation(this.mSuccessBowAnim);
        }

    }

    private void changeAlertType(int alertType, boolean fromCreate) {
        this.mAlertType = alertType;
        if(this.mDialogView != null) {
            if(!fromCreate) {
                this.restore();
            }

            switch(this.mAlertType) {
            case 1:
                this.mErrorFrame.setVisibility(0);
                break;
            case 2:
                this.mSuccessFrame.setVisibility(0);
                this.mSuccessLeftMask.startAnimation((Animation)this.mSuccessLayoutAnimSet.getAnimations().get(0));
                this.mSuccessRightMask.startAnimation((Animation)this.mSuccessLayoutAnimSet.getAnimations().get(1));
                break;
            case 3:
                this.mConfirmButton.setBackgroundResource(drawable.red_button_background);
                this.mWarningFrame.setVisibility(0);
                break;
            case 4:
                this.setCustomImage(this.mCustomImgDrawable);
                break;
            case 5:
                this.mProgressFrame.setVisibility(0);
                this.mConfirmButton.setVisibility(8);
            }

            if(!fromCreate) {
                this.playAnimation();
            }
        }

    }

    public int getAlerType() {
        return this.mAlertType;
    }

    public void changeAlertType(int alertType) {
        this.changeAlertType(alertType, false);
    }

    public String getTitleText() {
        return this.mTitleText;
    }

    public SweetAlertDialog setTitleText(String text) {
        this.mTitleText = text;
        if(this.mTitleTextView != null && this.mTitleText != null) {
            this.mTitleTextView.setText(this.mTitleText);
        }

        return this;
    }

    public SweetAlertDialog setCustomImage(Drawable drawable) {
        this.mCustomImgDrawable = drawable;
        if(this.mCustomImage != null && this.mCustomImgDrawable != null) {
            this.mCustomImage.setVisibility(0);
            this.mCustomImage.setImageDrawable(this.mCustomImgDrawable);
        }

        return this;
    }

    public SweetAlertDialog setCustomImage(int resourceId) {
        return this.setCustomImage(this.getContext().getResources().getDrawable(resourceId));
    }

    public String getContentText() {
        return this.mContentText;
    }

    public SweetAlertDialog setContentText(String text) {
        this.mContentText = text;
        if(this.mContentTextView != null && this.mContentText != null) {
            this.showContentText(true);
            this.mContentTextView.setText(this.mContentText);
        }

        return this;
    }

    public boolean isShowCancelButton() {
        return this.mShowCancel;
    }

    public SweetAlertDialog showCancelButton(boolean isShow) {
        this.mShowCancel = isShow;
        if(this.mCancelButton != null) {
            this.mCancelButton.setVisibility(this.mShowCancel?

0:8); } return this; } public boolean isShowContentText() { return this.mShowContent; } public SweetAlertDialog showContentText(boolean isShow) { this.mShowContent = isShow; if(this.mContentTextView != null) { this.mContentTextView.setVisibility(this.mShowContent?0:8); } return this; } public String getCancelText() { return this.mCancelText; } public SweetAlertDialog setCancelText(String text) { this.mCancelText = text; if(this.mCancelButton != null && this.mCancelText != null) { this.showCancelButton(true); this.mCancelButton.setText(this.mCancelText); } return this; } public String getConfirmText() { return this.mConfirmText; } public SweetAlertDialog setConfirmText(String text) { this.mConfirmText = text; if(this.mConfirmButton != null && this.mConfirmText != null) { this.mConfirmButton.setText(this.mConfirmText); } return this; } public SweetAlertDialog setCancelClickListener(SweetAlertDialog.OnSweetClickListener listener) { this.mCancelClickListener = listener; return this; } public SweetAlertDialog setConfirmClickListener(SweetAlertDialog.OnSweetClickListener listener) { this.mConfirmClickListener = listener; return this; } protected void onStart() { this.mDialogView.startAnimation(this.mModalInAnim); this.playAnimation(); } public void cancel() { this.dismissWithAnimation(true); } public void dismissWithAnimation() { this.dismissWithAnimation(false); } private void dismissWithAnimation(boolean fromCancel) { this.mCloseFromCancel = fromCancel; this.mConfirmButton.startAnimation(this.mOverlayOutAnim); this.mDialogView.startAnimation(this.mModalOutAnim); } public void onClick(View v) { if(v.getId() == id.cancel_button) { if(this.mCancelClickListener != null) { this.mCancelClickListener.onClick(this); } else { this.dismissWithAnimation(); } } else if(v.getId() == id.confirm_button) { if(this.mConfirmClickListener != null) { this.mConfirmClickListener.onClick(this); } else { this.dismissWithAnimation(); } } } public ProgressHelper getProgressHelper() { return this.mProgressHelper; } public interface OnSweetClickListener { void onClick(SweetAlertDialog var1); } }


项目引用依赖:


进度条项目github:https://github.com/pnikosis/materialish-progress

demo项目地址:http://download.csdn.net/detail/anddroid_lanyan/8859017

新增:Eclipse Demo源代码地址 http://download.csdn.net/detail/anddroid_lanyan/8861939


posted @ 2016-01-27 11:32  blfshiye  阅读(1549)  评论(1编辑  收藏  举报