android 仿ios等待框
2018-04-03 12:17:22 shaoyezhangliwei 阅读数 1007

编制时间这一栏 就是选择年月,不是点击弹框选择时间  ,直接在这个面板上就可以选择时间,减少交互步骤,增强用户体验。


整理了一个DEMO,希望可以帮助到大家。如果有其他问题欢迎大家加入我的qq群讨论交流:

开发一群:454430053开发二群:537532956
2018-01-03 01:27:37 qq_38729449 阅读数 134

办法有点笨,如有好的建议希望提出来

public class BasePopup {

    protected Activity activity;
    protected PopupWindow mPopupWindow;
    protected View popupWindowView;
    View popupView;

    /**
     * 初始化PopupWindow
     *
     * @return
     */
    public View initPopuptWindowView(int resources) {
        popupView = activity.getLayoutInflater().inflate(
                resources, null, false);
        mPopupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable(activity
                .getResources(), (Bitmap) null));
        mPopupWindow.getContentView().setFocusableInTouchMode(true);
        mPopupWindow.getContentView().setFocusable(true);
        mPopupWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_MENU
                        && event.getRepeatCount() == 0
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (mPopupWindow != null && mPopupWindow.isShowing()) {
                        mPopupWindow.dismiss();
                    }
                    return true;
                }
                return false;
            }
        });

        popupView.setTag(mPopupWindow);
        mPopupWindow.setAnimationStyle(R.style.shareDialog);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = activity.getWindow()
                        .getAttributes();
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                lp.alpha = 1.0f;
                activity.getWindow().setAttributes(lp);
            }
        });
        return popupView;
    }

    public void show() {
        show(Gravity.BOTTOM);
    }

    public void dismiss(){
        if(mPopupWindow != null){
            mPopupWindow.dismiss();
        }
    };

    public boolean isShowing() {
        if (mPopupWindow.isShowing()) {
            return true;
        }
        return false;
    }
    public void show(int gravity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 0.7f; // 0.0-1.0
        activity.getWindow().setAttributes(lp);
//        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mPopupWindow.showAtLocation(activity.getWindow().getDecorView(),
                Gravity.BOTTOM, 0, 0);
        translateBottomIn(popupView);
    }


    public void translateBottomIn(View view) {
        view.clearAnimation();
        view.setAnimation(AnimationUtils.loadAnimation(activity, com.smart.androidutils.R.anim.abc_slide_in_bottom));
        view.setVisibility(View.VISIBLE);
    }

    public void translateBottomOut(View view) {
        view.clearAnimation();
        view.setAnimation(AnimationUtils.loadAnimation(activity, com.smart.androidutils.R.anim.bottom_out));
        view.setVisibility(View.GONE);
    }
}
这个才是主类
public class LinePopup extends BasePopup implements View.OnClickListener {

    private TextView dialogLineOne;
    private TextView dialogLineTwo;
    private TextView dialogLineThree;
    private TextView dialogLineFour;
    private TextView cancelButton;
    private View pop_line_one;
    private View pop_line_two;
    private View pop_line_three;

    private ILineDialogClickListener callback;

    public void setOnDialogClickListener(ILineDialogClickListener callback) {
        this.callback = callback;
    }

    public LinePopup(Activity activity) {
        this.activity = activity;
        if (popupWindowView == null || popupWindowView.getTag() == null) {
            popupWindowView = initPopuptWindowView(R.layout.dialog_line);
            dialogLineOne = (TextView) popupWindowView.findViewById(R.id.dialog_line_one);
            dialogLineTwo = (TextView) popupWindowView.findViewById(R.id.dialog_line_two);
            dialogLineThree = (TextView) popupWindowView.findViewById(R.id.dialog_line_three);
            dialogLineFour = (TextView) popupWindowView.findViewById(R.id.dialog_line_four);
            pop_line_one = popupWindowView.findViewById(R.id.pop_line_one);
            pop_line_two = popupWindowView.findViewById(R.id.pop_line_two);
            pop_line_three = popupWindowView.findViewById(R.id.pop_line_three);
            cancelButton = (TextView) popupWindowView.findViewById(R.id.btn_cancel);

            dialogLineOne.setOnClickListener(this);
            dialogLineTwo.setOnClickListener(this);
            dialogLineThree.setOnClickListener(this);
            dialogLineFour.setOnClickListener(this);
            cancelButton.setOnClickListener(this);
        }
    }

    public void setLineOneText(@StringRes int re) {
        dialogLineOne.setText(re);
        pop_line_one.setVisibility(View.GONE);
        pop_line_two.setVisibility(View.GONE);
        pop_line_three.setVisibility(View.GONE);
        dialogLineTwo.setVisibility(View.GONE);
        dialogLineThree.setVisibility(View.GONE);
        dialogLineFour.setVisibility(View.GONE);
    }

    public void setLineTwoText(@StringRes int re) {
        dialogLineTwo.setVisibility(View.VISIBLE);
        pop_line_one.setVisibility(View.VISIBLE);
        pop_line_two.setVisibility(View.GONE);
        pop_line_three.setVisibility(View.GONE);
        dialogLineThree.setVisibility(View.GONE);
        dialogLineFour.setVisibility(View.GONE);
        dialogLineTwo.setText(re);
    }


    public void setLineThreeText(@StringRes int re) {
        dialogLineThree.setVisibility(View.VISIBLE);
        pop_line_two.setVisibility(View.VISIBLE);
        pop_line_three.setVisibility(View.GONE);
        dialogLineFour.setVisibility(View.GONE);
        dialogLineThree.setText(re);
    }


    public void setLineFourText(@StringRes int re) {
        dialogLineFour.setVisibility(View.VISIBLE);
        pop_line_three.setVisibility(View.VISIBLE);
        dialogLineFour.setText(re);
    }

    public void setLineOneText(CharSequence re) {
        dialogLineOne.setText(re);
        pop_line_one.setVisibility(View.GONE);
        pop_line_two.setVisibility(View.GONE);
        pop_line_three.setVisibility(View.GONE);
        dialogLineTwo.setVisibility(View.GONE);
        dialogLineThree.setVisibility(View.GONE);
        dialogLineFour.setVisibility(View.GONE);
    }

    public void setLineTwoText(CharSequence re) {
        dialogLineTwo.setVisibility(View.VISIBLE);
        pop_line_one.setVisibility(View.VISIBLE);
        pop_line_two.setVisibility(View.GONE);
        pop_line_three.setVisibility(View.GONE);
        dialogLineThree.setVisibility(View.GONE);
        dialogLineFour.setVisibility(View.GONE);
        dialogLineTwo.setText(re);
    }


    public void setLineThreeText(CharSequence re) {
        dialogLineThree.setVisibility(View.VISIBLE);
        pop_line_two.setVisibility(View.VISIBLE);
        pop_line_three.setVisibility(View.GONE);
        dialogLineFour.setVisibility(View.GONE);
        dialogLineThree.setText(re);
    }


    public void setLineFourText(CharSequence re) {
        dialogLineFour.setVisibility(View.VISIBLE);
        pop_line_three.setVisibility(View.VISIBLE);
        dialogLineFour.setText(re);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_line_one:
                mPopupWindow.dismiss();
                if (callback != null) {
                    callback.onLineOneClick();
                }
                break;
            case R.id.dialog_line_two:
                mPopupWindow.dismiss();
                if (callback != null) {
                    callback.onLineTowClick();
                }
                break;
            case R.id.dialog_line_three:
                mPopupWindow.dismiss();
                if (callback != null) {
                    callback.onLineThreeClick();
                }
                break;
            case R.id.dialog_line_four:
                mPopupWindow.dismiss();
                if (callback != null) {
                    callback.onLineFourClick();
                }
                break;
            case R.id.btn_cancel:
                mPopupWindow.dismiss();
                break;
            default:
                break;
        }
    }

    public static abstract class ILineDialogClickListener {
        public abstract void onLineOneClick();

        public void onLineTowClick(){};

        public void onLineThreeClick(){};

        public void onLineFourClick(){};
    }

}
。XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/live_padding_left">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/live_pop_window"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/dialog_line_one"
            style="@style/popLayout"/>

        <View
            android:id="@+id/pop_line_one"
            style="@style/dividing_long_line" />

        <TextView
            android:id="@+id/dialog_line_two"
            style="@style/popLayout" />

        <View
            android:id="@+id/pop_line_two"
            style="@style/dividing_long_line" />

        <TextView
            android:id="@+id/dialog_line_three"
            style="@style/popLayout"/>

        <View
            android:id="@+id/pop_line_three"
            style="@style/dividing_long_line" />

        <TextView
            android:id="@+id/dialog_line_four"
            style="@style/popLayout"/>


    </LinearLayout>

    <TextView
        android:id="@+id/btn_cancel"
        android:layout_width="match_parent"
        android:layout_height="@dimen/live_text_width"
        android:layout_marginTop="10dp"
        android:background="@drawable/live_grey_corner_bg"
        android:gravity="center"
        android:textSize="18sp"
        android:text="@string/btn_cancel"
        android:textColor="@color/tv_settings_logout" />
</LinearLayout>
stayle样式避免代码沉重,线自己写好了
<style name="popLayout">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">50dp</item>
    <item name="android:background">@drawable/live_grey_corner_bg</item>
    <item name="android:gravity">center</item>
    <item name="android:textColor">@color/tv_settings_logout</item>
    <item name="android:textSize">20sp</item>
</style>
这里我把弹出的样式也贴出来,怕有些同学懒得写
<style name="shareDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:background">@android:color/transparent</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:backgroundDimAmount">0.6</item>
</style>
到此基本结束,仿ios的弹框就算是完成了,只是这代码还可以封装下,懒得动了

2017-03-29 19:44:56 minwenping 阅读数 598

有点空 就写了个仿ios的加载等待dialog,没有图片只能将就一下啊,运行效果如下:
这里写图片描述

自定义dialog代码如下:

public class LoadingWaitDialog extends Dialog {

    private LayoutInflater inflater;
    private DisplayMetrics displayMetrics;
    private View view;
    private TextView tvTip;

    public LoadingWaitDialog(Context context) {
        //注意:一定要更该主题,不然在5.0上显示有问题
        super(context, R.style.chooseDialog);
        //获取渲染器
        inflater = LayoutInflater.from(context);
        //获取屏幕宽高信息
        displayMetrics = context.getResources().getDisplayMetrics();

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //渲染布局
        view = inflater.inflate(R.layout.layout_wait_dialog, null);
        tvTip = (TextView) view.findViewById(R.id.tv_tip);
        //下面的可以拓展动态更改progressbar的样式
        ContentLoadingProgressBar progressBar = (ContentLoadingProgressBar) view.findViewById(R.id.progress);
    }

    @Override
    public void show() {
        super.show();
        //设置dialog的宽高设置,注意:一定要放在show的后面,否则无效
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = (int) (displayMetrics.widthPixels * 0.5);
        params.height = (int) (displayMetrics.widthPixels * 0.4);
        params.gravity = Gravity.CENTER;
        getWindow().setAttributes(params);
        getWindow().setContentView(view);
    }

}

在mainActivity中代码如下:

  @Override
    protected void onResume() {
        super.onResume();
        LoadingWaitDialog dialog = new LoadingWaitDialog(this);
        dialog.show();
    }

值得注意的是要定义dialog的主题,比如去除titile和那一根线,dialog的主题代码如下:

  <style name="chooseDialog" parent="@android:style/Theme.Translucent">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>//无标题
        <item name="android:windowBackground">@drawable/bg_dialog</item>//背景
        <item name="android:windowIsFloating">true</item>//悬浮
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>
2017-02-09 10:17:09 u012572323 阅读数 1817

1.添加菊花图片


2.添加旋转动画

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/flower"
    android:pivotX="50.0%"
    android:pivotY="50.0%"
    android:fromDegrees="0"
    android:toDegrees="360">
</animated-rotate>


3.布局中配置Progressbar

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminateDrawable="@drawable/progress_flower"
    android:indeterminateBehavior="repeat"/>



底部弹出

博文 来自: Android_wm

仿IOS底部弹框

阅读数 26

没有更多推荐了,返回首页