Android 自定义环形进度条、环形刷新Doalog、水平进度、性别选择与Title

导读:本篇文章讲解 Android 自定义环形进度条、环形刷新Doalog、水平进度、性别选择与Title,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

一、自定义环形进度条

效果图:

Android 自定义环形进度条、环形刷新Doalog、水平进度、性别选择与Title

1.创建自定义CircleProgressView 类

package com.ruidde.pictureselectordemo.units;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

public class CircleProgressView extends View {
    private static final String TAG = "CircleProgressBar";
    private int mMaxProgress = 100;
    private int mProgress = 30;
    private final int mCircleLineStrokeWidth = 4;
    private final int mTxtStrokeWidth = 2;
    // 画圆所在的距形区域
    private final RectF mRectF;
    private final Paint mPaint;
    private final Context mContext;
    private String mTxtHint1;
    private String mTxtHint2;

    /**
     * AttributeSet 是接收xml中定义的属性信息,这不一定是自定义布局,不是自定义布局也有该属性,要不xml中定义的属性信息就无法接收了。
     * 比如
     * <p>
     * <TextView android:layout_width="fill_parent"
     * android:layout_height="wrap_content" android:text="@string/hello" />
     * 这几行红,layout_width,layout_height,text都可以在AttributeSet 中接收到。
     */
    public CircleProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mRectF = new RectF();
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = this.getWidth();
        int height = this.getHeight();

        if (width != height) {
            int min = Math.min(width, height);
            width = min;
            height = min;
        }

        // 设置画笔相关属性
        mPaint.setAntiAlias(true);
        /**
         * 整个圆圈画笔颜色
         * */
        mPaint.setColor(Color.rgb(0xe9, 0xe9, 0xe9));
        canvas.drawColor(Color.TRANSPARENT);
        mPaint.setStrokeWidth(mCircleLineStrokeWidth); //圆圈画笔的宽度
        mPaint.setStyle(Style.STROKE);
        // 位置
        mRectF.left = mCircleLineStrokeWidth / 2; // 左上角x
        mRectF.top = mCircleLineStrokeWidth / 2; // 左上角y
        mRectF.right = width - mCircleLineStrokeWidth / 2; // 左下角x
        mRectF.bottom = height - mCircleLineStrokeWidth / 2; // 右下角y
        canvas.drawArc(mRectF, -90, 360, false, mPaint);
        /**
         * 绘制圆圈,进度条背景
         * */
        mPaint.setColor(Color.RED);
        canvas.drawArc(mRectF, -90, ((float) mProgress / mMaxProgress) * 360,
                false, mPaint);

        // 绘制进度文案显示
        mPaint.setStrokeWidth(mTxtStrokeWidth); //字体画笔的宽度
        mPaint.setColor(Color.BLACK);    //调节文案的字体颜色
        String text = mProgress + "";  //文案的显示
        int textHeight = height / 4;
        mPaint.setTextSize(textHeight);
        int textWidth = (int) mPaint.measureText(text, 0, text.length());
        mPaint.setStyle(Style.FILL);
        canvas.drawText(text, width / 2 - textWidth / 2, height / 2    //画笔在画布上画的位置(height / 2+ textHeight / 2)中间
                + textHeight / 2, mPaint);

        if (!TextUtils.isEmpty(mTxtHint1)) {
            mPaint.setStrokeWidth(mTxtStrokeWidth);
            text = mTxtHint1;
            textHeight = height / 8;
            mPaint.setTextSize(textHeight);
            mPaint.setColor(Color.rgb(0x99, 0x99, 0x99));
            textWidth = (int) mPaint.measureText(text, 0, text.length());
            mPaint.setStyle(Style.FILL);
            canvas.drawText(text, width / 2 - textWidth / 2, height / 4  //画笔在画布上画的位置(height / 4+ textHeight / 2)中间与上面的中间
                    + textHeight / 2, mPaint);
        }

        if (!TextUtils.isEmpty(mTxtHint2)) {
            mPaint.setStrokeWidth(mTxtStrokeWidth);
            text = mTxtHint2;
            textHeight = height / 8;
            mPaint.setTextSize(textHeight);
            textWidth = (int) mPaint.measureText(text, 0, text.length());
            mPaint.setStyle(Style.FILL);
            canvas.drawText(text, width / 2 - textWidth / 2, 3 * height / 4 //画笔在画布上画的位置(3*height / 4+ textHeight / 2)中间与下面的中间
                    + textHeight / 2, mPaint);
        }
    }

    public int getMaxProgress() {
        return mMaxProgress;
    }

    public void setMaxProgress(int maxProgress) {
        this.mMaxProgress = maxProgress;
    }

    public void setProgress(int progress) {
        this.mProgress = progress;
        this.invalidate();
    }

    public void setProgressNotInUiThread(int progress) {
        this.mProgress = progress;
        this.postInvalidate();
    }

    public String getmTxtHint1() {
        return mTxtHint1;
    }

    public void setmTxtHint1(String mTxtHint1) {
        this.mTxtHint1 = mTxtHint1;
    }

    public String getmTxtHint2() {
        return mTxtHint2;
    }

    public void setmTxtHint2(String mTxtHint2) {
        this.mTxtHint2 = mTxtHint2;
    }
}

2.XML布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.ruidde.pictureselectordemo.units.CircleProgressView
        android:id="@+id/circleProgressbar"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true" />

</RelativeLayout>

3.Activity中使用

package com.ruidde.pictureselectordemo;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.ruidde.pictureselectordemo.units.CircleProgressView;

public class CircleProgressActivity extends Activity {

    private CircleProgressView mCircleBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circle_progress);
        mCircleBar = findViewById(R.id.circleProgressbar);

        mCircleBar.setProgress(10);        //设置当前进度
        mCircleBar.setmTxtHint1("10%");   //设置当前进度tv上面 的显示文案
        mCircleBar.setmTxtHint2("刷牙得分"); //设置当前进度tv下面 的显示文案
        mCircleBar.setMaxProgress(100);   //设置总进度的大小
    }
}

二、自定义环形刷新Doalog

效果图:

Android 自定义环形进度条、环形刷新Doalog、水平进度、性别选择与Title

1.创建Dialog类

package com.ruidde.pictureselectordemo.units;

import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.ruidde.pictureselectordemo.R;


public class AlertDialogZ {
    private Context context;
    private android.app.AlertDialog ad;
    private TextView titleView;
    private TextView messageView;
    private LinearLayout buttonLayout;
    private Window window;

    public AlertDialogZ(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
        ad = new android.app.AlertDialog.Builder(context).create();
        ad.setCanceledOnTouchOutside(false);
        ad.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        window = ad.getWindow();
        window.setContentView(R.layout.alertdialog);
        titleView = (TextView) window.findViewById(R.id.title);
        messageView = (TextView) window.findViewById(R.id.message);
        buttonLayout = (LinearLayout) window.findViewById(R.id.buttonLayout);
    }

    public AlertDialogZ(Context context, String msg) {
        // TODO Auto-generated constructor stub
        this.context = context;
        ad = new android.app.AlertDialog.Builder(context).create();
        ad.setCanceledOnTouchOutside(false);
        try {
            ad.show();
        } catch (Exception e) {
            // TODO: handle exception
        }
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        window = ad.getWindow();
        window.setContentView(R.layout.progressdialog);
        messageView = (TextView) window.findViewById(R.id.dialog_textview);
        if (!TextUtils.isEmpty(msg)) {
            messageView.setText(msg);
        }
    }

    public AlertDialogZ(Context context, View view) {
        this.context = context;
        ad = new android.app.AlertDialog.Builder(context).create();
        ad.setCanceledOnTouchOutside(false);
        ad.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        window = ad.getWindow();
        window.setContentView(view);
    }

    public AlertDialogZ(Context context, int resId) {
        this.context = context;
        ad = new android.app.AlertDialog.Builder(context).create();
        ad.setCanceledOnTouchOutside(false);
        ad.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        window = ad.getWindow();
        window.setContentView(resId);
    }

    public void setTitle(int resId) {
        titleView.setText(resId);
    }

    public void setTitle(String title) {
        titleView.setText(title);
    }

    public void setMessage(int resId) {
        messageView.setText(resId);
    }

    public void setMessage(String message) {
        messageView.setText(message);
    }

    public void setOnCancelListener(OnCancelListener cListener) {
        ad.setOnCancelListener(cListener);
    }

    public void setOnDismissListener(OnDismissListener dListener) {
        ad.setOnDismissListener(dListener);
    }

    /**
     * 设置按钮
     *
     * @param text
     * @param listener
     */
    public void setPositiveButton(String text,
                                  final View.OnClickListener listener) {
        Button button = new Button(context);
        LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);//按钮的宽高设置
        button.setLayoutParams(params);
        button.setBackgroundResource(R.drawable.kuang_nor_img);
        button.setText(text);
        button.setTextColor(Color.BLACK);
        button.setGravity(Gravity.CENTER);
        button.setTextSize(18);
        button.setOnClickListener(listener);
        buttonLayout.addView(button);
    }

    /**
     * 设置按钮
     *
     * @param text
     * @param listener
     */
    public void setNegativeButton(String text,
                                  final View.OnClickListener listener) {
        Button button = new Button(context);
        LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); //按钮的宽高设置
        button.setLayoutParams(params);
        button.setBackgroundResource(R.drawable.kuang_nor_img);
        button.setText(text);
        button.setTextColor(Color.BLACK);
        button.setGravity(Gravity.CENTER);
        button.setTextSize(18);
        button.setOnClickListener(listener);
        if (buttonLayout.getChildCount() > 0) {
            params.setMargins(20, 0, 0, 0);
            button.setLayoutParams(params);
            buttonLayout.addView(button, 1);
        } else {
            button.setLayoutParams(params);
            buttonLayout.addView(button);
        }

    }

    public void setCancelable(boolean isCancelable) {
        if (ad != null) {
            ad.setCancelable(isCancelable);
        }
    }

    /**
     * 关闭对话框
     */
    public void dismiss() {
        if (ad != null && ad.isShowing()) {
            ad.dismiss();
        }
    }
}

2.xml中progressdialog

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00000000">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:background="@drawable/corner_round"
        android:layout_centerInParent="true">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="vertical" >

            <ProgressBar
                android:id="@+id/dialog_progressBar1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:indeterminateDuration="700"
                android:indeterminateDrawable="@drawable/progress_large"/>

            <TextView
                android:id="@+id/dialog_textview"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="10dip"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:text="正在请求数据..."
                android:textColor="#000000"
                android:textSize="16sp" />
        </LinearLayout>
    </RelativeLayout>

</RelativeLayout>

3.Activity中调用

AlertDialogZ mDialog = new AlertDialogZ(context, "正在加载数据...");

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/119208.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!