通用系列
对话框
Dialog
使用方法
private void showInputPasswordDialog() {
LouDialog.getInstance(mContext,
R.layout.dialog_input_password,
R.style.dialog_theme)
.setCancelable(false)
.setDimAmount(0.3f)
.setWindowAnimations(R.style.dialog_anim)
.putClickListenerToViewArray(new DialogClickListener() {
@Override
public void click(View v) {
switch(v.getId()){
case R.id.tv_dialog_ok:
break;
case R.id.tv_dialog_cancel:
break;
}
}
}, R.id.tv_dialog_ok, R.id.tv_dialog_cancel)
.show();
}
<!-- 对话框主题 -->
<style name="dialog_theme" parent="@android:Theme.Holo.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/dialog_bg_shape</item>
<item name="android:windowIsFloating">true</item>
<item name="android:textColor">@color/main_font</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
<!-- 对话框进入和退出动画 -->
<style name="dialog_anim" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/in_slide_right</item>
<item name="android:windowExitAnimation">@anim/out_slide_right</item>
</style>
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
public class LouDialog {
private SparseArray<View> mViews;
private Dialog mDialog;
public interface DialogClickListener {
public void click(View v);
}
private LouDialog(Context context, int layoutId, int themeId) {
mDialog = new Dialog(context, themeId);
mDialog.setContentView(layoutId);
mViews = new SparseArray<View>();
}
/**
*
* @param context
* @param layoutId
* @param themeId
* 如果使用默认主题,输入0即可;
* @return
*/
public static LouDialog getInstance(Context context, int layoutId, int themeId) {
return new LouDialog(context, layoutId, themeId);
}
public LouDialog setDimAmount(float amount) {
if (mDialog != null) {
mDialog.getWindow().setDimAmount(amount);
}
return this;
}
public LouDialog setCancelable(boolean able) {
if (mDialog != null) {
mDialog.setCancelable(able);
}
return this;
}
/**
* 设置弹框位置以及弹框透明度;
*
* @param gravity
* @param x 单位:px;
* @param y 单位:px;
* @return
*/
public LouDialog setPositionAndAlpha(int gravity, int x, int y, float alpha) {
if (mDialog != null) {
Window window = mDialog.getWindow();
window.setGravity(gravity);
WindowManager.LayoutParams lp = window.getAttributes();
lp.x = x;
lp.y = y;
lp.alpha = alpha;
window.setAttributes(lp);
}
return this;
}
/**
* 设置进入和退出动画;
* @param styleId
* @return
*/
public LouDialog setWindowAnimations(int styleId){
if (mDialog != null) {
mDialog.getWindow().setWindowAnimations(styleId);;
}
return this;
}
/**
* 显示对话框
*/
public void show() {
if (!isShowing()) {
mDialog.show();
}
}
public Dialog getDialog() {
return mDialog;
}
/**
* 判断是否正在显示
*
* @return
*/
public boolean isShowing() {
boolean isShowing = false;
if (mDialog != null && mDialog.isShowing()) {
isShowing = true;
}
return isShowing;
}
/**
* 隐藏对话框
*/
public void dismiss() {
if (mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
}
}
/**
* 获取具体的某个视图;
*
* @param viewId
* @return
*/
@SuppressWarnings("unchecked")
public <T extends View> T getView(int viewId) {
View view = mViews.get(viewId);
if (view == null) {
view = mDialog.findViewById(viewId);
mViews.put(viewId, view);
}
return (T) view;
}
public LouDialog putText(int viewId, String text) {
View v = getView(viewId);
if (v instanceof TextView) {
((TextView) v).setText(text);
}
return this;
}
public LouDialog putImg(int viewId, int resId) {
View v = getView(viewId);
if (v instanceof ImageView) {
((ImageView) v).setImageResource(resId);
}
return this;
}
public LouDialog putImg(int viewId, Bitmap bitmap) {
View v = getView(viewId);
if (v instanceof ImageView) {
((ImageView) v).setImageBitmap(bitmap);
}
return this;
}
public LouDialog putImg(int viewId, Drawable drawable) {
View v = getView(viewId);
if (v instanceof ImageView) {
((ImageView) v).setImageDrawable(drawable);
}
return this;
}
/**
* 添加点击事件;
*
* @param viewId
* @param listener
* @param hideAfterClick
* @return
*/
public LouDialog putClickListener(final int viewId, final DialogClickListener listener,
final boolean hideAfterClick) {
View v = getView(viewId);
clickEffectWithBgByAlpha(new OnClickListener() {
@Override
public void onClick(View v) {
listener.click(v);
if (hideAfterClick) {
dismiss();
}
}
}, v);
return this;
}
/**
* 默认点击后隐藏对话框;
*
* @param viewId
* @param listener
* @return
*/
public LouDialog putClickListener(final int viewId, final DialogClickListener listener) {
return putClickListener(viewId, listener, true);
}
public LouDialog putClickListenerToViewArray(final DialogClickListener listener, int ...viewIds) {
for(int viewId: viewIds){
putClickListener(viewId, listener, true);
}
return this;
}
// --------------------工具类
private static void clickEffectWithBgByAlpha(OnClickListener clickListener, View... views) {
if (clickListener == null) {
return;
}
// 逐个处理每个view
for (final View v : views) {
if (v == null) {
continue;
}
// 设置监听
v.setOnClickListener(clickListener);
// 根据背景是否为空,来处理点击后的效果:不为空则设置透明,为空则设置默认颜色;
final boolean bgIsNull = v.getBackground() == null;
// 设置触摸效果
v.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
doDown(view, bgIsNull);
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_CANCEL:
doReset(view, bgIsNull);
break;
case MotionEvent.ACTION_UP:
doReset(view, bgIsNull);
break;
}
return false;
}
// 按下时
private void doDown(View view, boolean bgIsNull) {
view.animate().cancel();
view.setAlpha(0.4f);
if (bgIsNull) {
view.setBackgroundColor(0x88ffffff);
} else {
view.getBackground().setAlpha(202);
}
}
// 恢复视图
private void doReset(View view, boolean bgIsNull) {
view.animate().alpha(1f).setDuration(100).start();
if (bgIsNull) {
view.setBackground(null);
} else {
view.getBackground().setAlpha(255);
}
}
});
}
}
// ~~~~~~~~~~~~~~
}