美文网首页自定义控件自定义ViewAndroid知识
SquareProgressBar--一个方形的加载进度条

SquareProgressBar--一个方形的加载进度条

作者: 不识水的鱼 | 来源:发表于2016-09-20 21:55 被阅读598次

    SquareProgressBar一个方形的加载进度条,在github上面有原型,但是为了在中间添加一个进度的显示,又不能有图片,所以进行了一些微小的改动,去掉了中间的图片,完成功能
    github链接:
    android-square-progressbar



    下面贴上代码,主要是三个工具,两个类,一个布局
    第一个帮助类:
    package com.example.yukun.webapplication.squareprogressbar.utils;
    
    import android.content.Context;
    import android.util.TypedValue;
    
    public class CalculationUtil {
    
    public static int convertDpToPx(float dp, Context context) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                context.getResources().getDisplayMetrics());
    }
    }
    

    第二个帮助类:

    package com.example.yukun.webapplication.squareprogressbar.utils;
    
    import java.util.ArrayList;
    
    import android.R.color;
    
    public class ColourUtil {
    static ArrayList<Integer> colourArray = new ArrayList<Integer>();
    
    public static ArrayList<Integer> getColourArray() {
        colourArray.add(color.holo_blue_bright);
        colourArray.add(color.holo_blue_dark);
        colourArray.add(color.holo_blue_light);
        colourArray.add(color.holo_green_dark);
        colourArray.add(color.holo_green_light);
        colourArray.add(color.holo_orange_dark);
        colourArray.add(color.holo_orange_light);
        colourArray.add(color.holo_purple);
        colourArray.add(color.holo_red_dark);
        colourArray.add(color.holo_red_light);
        return colourArray;
    }
    }
    

    第三个帮助类:

    package com.example.yukun.webapplication.squareprogressbar.utils;
    
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Paint.Align;
    
    public class PercentStyle {
    private Align align;
    private float textSize;
    private boolean percentSign;
    private String customText = "%";
    private int textColor = Color.BLACK;
    
    public PercentStyle() {
        // do nothing
    }
    
    public PercentStyle(Align align, float textSize, boolean percentSign) {
        super();
        this.align = align;
        this.textSize = textSize;
        this.percentSign = percentSign;
    }
    
    public Align getAlign() {
        return align;
    }
    
    public void setAlign(Align align) {
        this.align = align;
    }
    
    public float getTextSize() {
        return textSize;
    }
    
    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }
    
    public boolean isPercentSign() {
        return percentSign;
    }
    
    public void setPercentSign(boolean percentSign) {
        this.percentSign = percentSign;
    }
    
    public String getCustomText() {
        return customText;
    }
    
    /**
     * With this you can set a custom text which should get displayed right
     * behind the number of the progress. Per default it displays a <i>%</i>.
     * 
     * @param customText
     *            The custom text you want to display.
     * @since 1.4.0
     */
    public void setCustomText(String customText) {
        this.customText = customText;
    }
    
    public int getTextColor() {
        return textColor;
    }
    
    /**
     * Set the color of the text that display the current progress. This will
     * also change the color of the text that normally represents a <i>%</i>.
     * 
     * @param textColor
     *            the color to set the text to.
     * @since 1.4.0
     */
    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }
    
    }
    

    第一个view类:

    package com.example.yukun.webapplication.squareprogressbar;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.Paint.Align;
    import android.graphics.Paint.Style;
    import android.graphics.Path;
    import android.util.AttributeSet;
    import android.view.View;
    
    import com.example.yukun.webapplication.squareprogressbar.utils.CalculationUtil;
    import com.example.yukun.webapplication.squareprogressbar.utils.PercentStyle;
    
    import java.text.DecimalFormat;
    
    
    public class SquareProgressView extends View {
    
    private double progress;
    private Paint progressBarPaint;
    private Paint outlinePaint;
    private Paint textPaint;
    
    private float widthInDp = 10;
    private float strokewidth = 0;
    private Canvas canvas;
    
    private boolean outline = false;
    private boolean startline = false;
    private boolean showProgress = false;
    private boolean centerline = false;
    
    private PercentStyle percentSettings = new PercentStyle(Align.CENTER, 150,
            true);
    private boolean clearOnHundred = false;
    private boolean isIndeterminate = false;
    private int indeterminate_count = 1;
    
    private float indeterminate_width = 20.0f;
    
    public SquareProgressView(Context context) {
        super(context);
        initializePaints(context);
    }
    
    public SquareProgressView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initializePaints(context);
    }
    
    public SquareProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initializePaints(context);
    }
    
    private void initializePaints(Context context) {
        progressBarPaint = new Paint();
        progressBarPaint.setColor(context.getResources().getColor(
                android.R.color.holo_green_dark));
        progressBarPaint.setStrokeWidth(CalculationUtil.convertDpToPx(
                widthInDp, getContext()));
        progressBarPaint.setAntiAlias(true);
        progressBarPaint.setStyle(Style.STROKE);
    
        outlinePaint = new Paint();
        outlinePaint.setColor(context.getResources().getColor(
                android.R.color.black));
        outlinePaint.setStrokeWidth(1);
        outlinePaint.setAntiAlias(true);
        outlinePaint.setStyle(Style.STROKE);
    
        textPaint = new Paint();
        textPaint.setColor(context.getResources().getColor(
                android.R.color.black));
        textPaint.setAntiAlias(true);
        textPaint.setStyle(Style.STROKE);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        this.canvas = canvas;
        super.onDraw(canvas);
        strokewidth = CalculationUtil.convertDpToPx(widthInDp, getContext());
        float scope = canvas.getWidth() + canvas.getHeight()
                + canvas.getHeight() + canvas.getWidth() - strokewidth;
    
        if (isOutline()) {
            drawOutline();
        }
    
        if (isStartline()) {
            drawStartline();
        }
    
        if (isShowProgress()) {
            drawPercent(percentSettings);
        }
    
        if (isCenterline()) {
            drawCenterline(strokewidth);
        }
    
        if ((isClearOnHundred() && progress == 100.0) || (progress <= 0.0)) {
            return;
        }
    
        if (isIndeterminate()) {
            Path path = new Path();
            DrawStop drawEnd = getDrawEnd((scope / 100) * Float.valueOf(String.valueOf(indeterminate_count)), canvas);
    
            if (drawEnd.place == Place.TOP) {
                path.moveTo(drawEnd.location - indeterminate_width - strokewidth, strokewidth / 2);
                path.lineTo(drawEnd.location, strokewidth / 2);
                canvas.drawPath(path, progressBarPaint);
            }
    
            if (drawEnd.place == Place.RIGHT) {
                path.moveTo(canvas.getWidth() - (strokewidth / 2), drawEnd.location - indeterminate_width);
                path.lineTo(canvas.getWidth() - (strokewidth / 2), strokewidth
                        + drawEnd.location);
                canvas.drawPath(path, progressBarPaint);
            }
    
            if (drawEnd.place == Place.BOTTOM) {
                path.moveTo(drawEnd.location - indeterminate_width - strokewidth,
                        canvas.getHeight() - (strokewidth / 2));
                path.lineTo(drawEnd.location, canvas.getHeight()
                        - (strokewidth / 2));
                canvas.drawPath(path, progressBarPaint);
            }
    
            if (drawEnd.place == Place.LEFT) {
                path.moveTo((strokewidth / 2), drawEnd.location - indeterminate_width
                        - strokewidth);
                path.lineTo((strokewidth / 2), drawEnd.location);
                canvas.drawPath(path, progressBarPaint);
            }
    
            indeterminate_count++;
            if (indeterminate_count > 100) {
                indeterminate_count = 0;
            }
            invalidate();
        } else {
            Path path = new Path();
            DrawStop drawEnd = getDrawEnd((scope / 100) * Float.valueOf(String.valueOf(progress)), canvas);
    
            if (drawEnd.place == Place.TOP) {
                if (drawEnd.location > (canvas.getWidth() / 2)) {
                    path.moveTo(canvas.getWidth() / 2, strokewidth / 2);
                    path.lineTo(drawEnd.location, strokewidth / 2);
                } else {
                    path.moveTo(canvas.getWidth() / 2, strokewidth / 2);
                    path.lineTo(canvas.getWidth() - (strokewidth / 2), strokewidth / 2);
                    path.lineTo(canvas.getWidth() - (strokewidth / 2), canvas.getHeight() - strokewidth / 2);
                    path.lineTo(strokewidth / 2, canvas.getHeight() - strokewidth / 2);
                    path.lineTo(strokewidth / 2, strokewidth / 2);
                    path.lineTo(drawEnd.location, strokewidth / 2);
                }
                canvas.drawPath(path, progressBarPaint);
            }
    
            if (drawEnd.place == Place.RIGHT) {
                path.moveTo(canvas.getWidth() / 2, strokewidth / 2);
                path.lineTo(canvas.getWidth() - (strokewidth / 2), strokewidth / 2);
                path.lineTo(canvas.getWidth() - (strokewidth / 2), strokewidth / 2
                        + drawEnd.location);
                canvas.drawPath(path, progressBarPaint);
            }
    
            if (drawEnd.place == Place.BOTTOM) {
                path.moveTo(canvas.getWidth() / 2, strokewidth / 2);
                path.lineTo(canvas.getWidth() - (strokewidth / 2), strokewidth / 2);
                path.lineTo(canvas.getWidth() - (strokewidth / 2), canvas.getHeight());
                path.moveTo(canvas.getWidth(), canvas.getHeight() - strokewidth / 2);
                path.lineTo(drawEnd.location, canvas.getHeight()
                        - (strokewidth / 2));
                canvas.drawPath(path, progressBarPaint);
            }
    
            if (drawEnd.place == Place.LEFT) {
                path.moveTo(canvas.getWidth() / 2, strokewidth / 2);
                path.lineTo(canvas.getWidth() - (strokewidth / 2), strokewidth / 2);
                path.lineTo(canvas.getWidth() - (strokewidth / 2), canvas.getHeight() - strokewidth / 2);
                path.lineTo(0, canvas.getHeight() - strokewidth / 2);
                path.moveTo(strokewidth / 2, canvas.getHeight() - strokewidth / 2);
                path.lineTo((strokewidth / 2), drawEnd.location);
                canvas.drawPath(path, progressBarPaint);
            }
        }
    }
    
    private void drawStartline() {
        Path outlinePath = new Path();
        outlinePath.moveTo(canvas.getWidth() / 2, 0);
        outlinePath.lineTo(canvas.getWidth() / 2, strokewidth);
        canvas.drawPath(outlinePath, outlinePaint);
    }
    
    private void drawOutline() {
        Path outlinePath = new Path();
        outlinePath.moveTo(0, 0);
        outlinePath.lineTo(canvas.getWidth(), 0);
        outlinePath.lineTo(canvas.getWidth(), canvas.getHeight());
        outlinePath.lineTo(0, canvas.getHeight());
        outlinePath.lineTo(0, 0);
        canvas.drawPath(outlinePath, outlinePaint);
    }
    
    public double getProgress() {
        return progress;
    }
    
    public void setProgress(double progress) {
        this.progress = progress;
        this.invalidate();
    }
    
    public void setColor(int color) {
        progressBarPaint.setColor(color);
        this.invalidate();
    }
    
    public void setWidthInDp(int width) {
        this.widthInDp = width;
        progressBarPaint.setStrokeWidth(CalculationUtil.convertDpToPx(
                widthInDp, getContext()));
        this.invalidate();
    }
    
    public boolean isOutline() {
        return outline;
    }
    
    public void setOutline(boolean outline) {
        this.outline = outline;
        this.invalidate();
    }
    
    public boolean isStartline() {
        return startline;
    }
    
    public void setStartline(boolean startline) {
        this.startline = startline;
        this.invalidate();
    }
    
    private void drawPercent(PercentStyle setting) {
        textPaint.setTextAlign(setting.getAlign());
        if (setting.getTextSize() == 0) {
            textPaint.setTextSize((canvas.getHeight() / 10) * 4);
        } else {
            textPaint.setTextSize(setting.getTextSize());
        }
    
        String percentString = new DecimalFormat("###").format(getProgress());
        if (setting.isPercentSign()) {
            percentString = percentString + percentSettings.getCustomText();
        }
    
        textPaint.setColor(percentSettings.getTextColor());
    
        canvas.drawText(
                percentString,
                canvas.getWidth() / 2,
                (int) ((canvas.getHeight() / 2) - ((textPaint.descent() + textPaint
                        .ascent()) / 2)), textPaint);
    }
    
    public boolean isShowProgress() {
        return showProgress;
    }
    
    public void setShowProgress(boolean showProgress) {
        this.showProgress = showProgress;
        this.invalidate();
    }
    
    public void setPercentStyle(PercentStyle percentSettings) {
        this.percentSettings = percentSettings;
        this.invalidate();
    }
    
    public PercentStyle getPercentStyle() {
        return percentSettings;
    }
    
    public void setClearOnHundred(boolean clearOnHundred) {
        this.clearOnHundred = clearOnHundred;
        this.invalidate();
    }
    
    public boolean isClearOnHundred() {
        return clearOnHundred;
    }
    
    private void drawCenterline(float strokewidth) {
        float centerOfStrokeWidth = strokewidth / 2;
        Path centerlinePath = new Path();
        centerlinePath.moveTo(centerOfStrokeWidth, centerOfStrokeWidth);
        centerlinePath.lineTo(canvas.getWidth() - centerOfStrokeWidth, centerOfStrokeWidth);
        centerlinePath.lineTo(canvas.getWidth() - centerOfStrokeWidth, canvas.getHeight() - centerOfStrokeWidth);
        centerlinePath.lineTo(centerOfStrokeWidth, canvas.getHeight() - centerOfStrokeWidth);
        centerlinePath.lineTo(centerOfStrokeWidth, centerOfStrokeWidth);
        canvas.drawPath(centerlinePath, outlinePaint);
    }
    
    public boolean isCenterline() {
        return centerline;
    }
    
    public void setCenterline(boolean centerline) {
        this.centerline = centerline;
        this.invalidate();
    }
    
    public boolean isIndeterminate() {
        return isIndeterminate;
    }
    
    public void setIndeterminate(boolean indeterminate) {
        isIndeterminate = indeterminate;
        this.invalidate();
    }
    
    public DrawStop getDrawEnd(float percent, Canvas canvas) {
        DrawStop drawStop = new DrawStop();
        strokewidth = CalculationUtil.convertDpToPx(widthInDp, getContext());
        float halfOfTheImage = canvas.getWidth() / 2;
    
        if (percent > halfOfTheImage) {
            float second = percent - halfOfTheImage;
    
            if (second > canvas.getHeight()) {
                float third = second - canvas.getHeight();
    
                if (third > canvas.getWidth()) {
                    float forth = third - canvas.getWidth();
    
                    if (forth > canvas.getHeight()) {
                        float fifth = forth - canvas.getHeight();
    
                        if (fifth == halfOfTheImage) {
                            drawStop.place = Place.TOP;
                            drawStop.location = halfOfTheImage;
                        } else {
                            drawStop.place = Place.TOP;
                            drawStop.location = strokewidth + fifth;
                        }
                    } else {
                        drawStop.place = Place.LEFT;
                        drawStop.location = canvas.getHeight() - forth;
                    }
    
                } else {
                    drawStop.place = Place.BOTTOM;
                    drawStop.location = canvas.getWidth() - third;
                }
            } else {
                drawStop.place = Place.RIGHT;
                drawStop.location = strokewidth + second;
            }
    
        } else {
            drawStop.place = Place.TOP;
            drawStop.location = halfOfTheImage + percent;
        }
    
        return drawStop;
    }
    
    private class DrawStop {
    
        private Place place;
        private float location;
    
        public DrawStop() {
    
        }
    }
    
    public enum Place {
        TOP, RIGHT, BOTTOM, LEFT
    }
    }
    

    第二个view类:

    package com.example.yukun.webapplication.squareprogressbar;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Color;
    import android.graphics.ColorMatrix;
    import android.graphics.ColorMatrixColorFilter;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.widget.ImageView;
    import android.widget.ImageView.ScaleType;
    import android.widget.RelativeLayout;
    
    import com.example.yukun.webapplication.R;
    import com.example.yukun.webapplication.squareprogressbar.utils.CalculationUtil;
    import com.example.yukun.webapplication.squareprogressbar.utils.PercentStyle;
    
    /**
     * The basic {@link SquareProgressBar}. This class includes all the methods you
     * need to modify your {@link SquareProgressBar}.
     * 
     * @author ysigner
     * @since 1.0.0
     */
    public class SquareProgressBar extends RelativeLayout {
    
    private ImageView imageView;
    private final SquareProgressView bar;
    private boolean opacity = false;
    private boolean greyscale;
    private boolean isFadingOnProgress = false;
    
    /**
     * New SquareProgressBar.
     * 
     * @param context
     *            the {@link Context}
     * @param attrs
     *            an {@link AttributeSet}
     * @param defStyle
     *            a defined style.
     * @since 1.0.0
     */
    public SquareProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mInflater.inflate(R.layout.progressbarview, this, true);
        bar = (SquareProgressView) findViewById(R.id.squareProgressBar1);
        imageView = (ImageView) findViewById(R.id.imageView1);
        bar.bringToFront();
    }
    
    /**
     * New SquareProgressBar.
     * 
     * @param context
     *            the {@link Context}
     * @param attrs
     *            an {@link AttributeSet}
     * @since 1.0.0
     */
    public SquareProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mInflater.inflate(R.layout.progressbarview, this, true);
        bar = (SquareProgressView) findViewById(R.id.squareProgressBar1);
        imageView = (ImageView) findViewById(R.id.imageView1);
        bar.bringToFront();
    }
    
    /**
     * New SquareProgressBar.
     * 
     * @param context
     * @since 1.0.0
     */
    public SquareProgressBar(Context context) {
        super(context);
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mInflater.inflate(R.layout.progressbarview, this, true);
        bar = (SquareProgressView) findViewById(R.id.squareProgressBar1);
        imageView = (ImageView) findViewById(R.id.imageView1);
        bar.bringToFront();
    }
    
    /**
     * Sets the image of the {@link SquareProgressBar}. Must be a valid
     * ressourceId.
     * 
     * @param image
     *            the image as a ressourceId
     * @since 1.0
     */
    public void setImage(int image) {
        imageView.setImageResource(image);
    
    }
    
    /**
     * Sets the image scale type according to {@link ScaleType}.
     * 
     * @param scale
     *            the image ScaleType
     * @since 1.3.0
     * @author thiagokimo
     */
    public void setImageScaleType(ScaleType scale) {
        imageView.setScaleType(scale);
    }
    
    /**
     * Sets the progress of the {@link SquareProgressBar}. If opacity is
     * selected then here it sets it. See {@link #setOpacity(boolean)} for more
     * information.
     * 
     * @param progress
     *            the progress
     * @since 1.0.0
     */
    public void setProgress(double progress) {
        bar.setProgress(progress);
        if (opacity) {
            if (isFadingOnProgress) {
                setOpacity(100 - (int) progress);
            } else {
                setOpacity((int) progress);
            }
        } else {
            setOpacity(100);
        }
    }
    
    /**
     * Sets the colour of the {@link SquareProgressBar} to a predefined android
     * holo color. <br/>
     * <b>Examples:</b>
     * <ul>
     * <li>holo_blue_bright</li>
     * <li>holo_blue_dark</li>
     * <li>holo_blue_light</li>
     * <li>holo_green_dark</li>
     * <li>holo_green_light</li>
     * <li>holo_orange_dark</li>
     * <li>holo_orange_light</li>
     * <li>holo_purple</li>
     * <li>holo_red_dark</li>
     * <li>holo_red_light</li>
     * </ul>
     * 
     * @param androidHoloColor
     * @since 1.0.0
     */
    public void setHoloColor(int androidHoloColor) {
        bar.setColor(getContext().getResources().getColor(androidHoloColor));
    }
    
    /**
     * Sets the colour of the {@link SquareProgressBar}. YOu can give it a
     * hex-color string like <i>#C9C9C9</i>.
     * 
     * @param colorString
     *            the colour of the {@link SquareProgressBar}
     * @since 1.1.0
     */
    public void setColor(String colorString) {
        bar.setColor(Color.parseColor(colorString));
    }
    
    /**
     * This sets the colour of the {@link SquareProgressBar} with a RGB colour.
     * 
     * @param r
     *            red
     * @param g
     *            green
     * @param b
     *            blue
     * @since 1.1.0
     */
    public void setColorRGB(int r, int g, int b) {
        bar.setColor(Color.rgb(r, g, b));
    }
    
    /**
     * This sets the colour of the {@link SquareProgressBar} with a RGB colour.
     * Works when used with
     * <code>android.graphics.Color.rgb(int, int, int)</code>
     * 
     * @param r
     *            red
     * @param g
     *            green
     * @param b
     *            blue
     * @since 1.4.0
     */
    public void setColorRGB(int rgb) {
        bar.setColor(rgb);
    }
    
    /**
     * This sets the width of the {@link SquareProgressBar}.
     * 
     * @param width
     *            in Dp
     * @since 1.1.0
     */
    public void setWidth(int width) {
        int padding = CalculationUtil.convertDpToPx(width, getContext());
        imageView.setPadding(padding, padding, padding, padding);
        bar.setWidthInDp(width);
    }
    
    /**
     * This sets the alpha of the image in the view. Actually I need to use the
     * deprecated method here as the new one is only available for the API-level
     * 16. And the min API level of this library is 14.
     * 
     * Use this only as private method.
     * 
     * @param progress
     *            the progress
     */
    private void setOpacity(int progress) {
        imageView.setAlpha((int) (2.55 * progress));
    }
    
    /**
     * Switches the opacity state of the image. This forces the
     * SquareProgressBar to redraw with the current progress. As bigger the
     * progress is, then more of the image comes to view. If the progress is 0,
     * then you can't see the image at all. If the progress is 100, the image is
     * shown full.
     * 
     * @param opacity
     *            true if opacity should be enabled.
     * @since 1.2.0
     */
    public void setOpacity(boolean opacity) {
        this.opacity = opacity;
        setProgress(bar.getProgress());
    }
    
    /**
     * Switches the opacity state of the image. This forces the
     * SquareProgressBar to redraw with the current progress. As bigger the
     * progress is, then more of the image comes to view. If the progress is 0,
     * then you can't see the image at all. If the progress is 100, the image is
     * shown full.
     * 
     * You can also set the flag if the fading should get inverted so the image
     * disappears when the progress increases.
     * 
     * @param opacity
     *            true if opacity should be enabled.
     * @param isFadingOnProgress
     *            default false. This changes the behavior the opacity works. If
     *            the progress increases then the images fades. When the
     *            progress reaches 100, then the image disappears.
     * @since 1.4.0
     */
    public void setOpacity(boolean opacity, boolean isFadingOnProgress) {
        this.opacity = opacity;
        this.isFadingOnProgress = isFadingOnProgress;
        setProgress(bar.getProgress());
    }
    
    /**
     * You can set the image to b/w with this method. Works fine with the
     * opacity.
     * 
     * @param greyscale
     *            true if the grayscale should be activated.
     * @since 1.2.0
     */
    public void setImageGrayscale(boolean greyscale) {
        this.greyscale = greyscale;
        if (greyscale) {
            ColorMatrix matrix = new ColorMatrix();
            matrix.setSaturation(0);
            imageView.setColorFilter(new ColorMatrixColorFilter(matrix));
        } else {
            imageView.setColorFilter(null);
        }
    }
    
    /**
     * If opacity is enabled.
     * 
     * @return true if opacity is enabled.
     */
    public boolean isOpacity() {
        return opacity;
    }
    
    /**
     * If greyscale is enabled.
     * 
     * @return true if greyscale is enabled.
     */
    public boolean isGreyscale() {
        return greyscale;
    }
    
    /**
     * Draws an outline of the progressbar. Looks quite cool in some situations.
     * 
     * @param drawOutline
     *            true if it should or not.
     * @since 1.3.0
     */
    public void drawOutline(boolean drawOutline) {
        bar.setOutline(drawOutline);
    }
    
    /**
     * If outline is enabled or not.
     * 
     * @return true if outline is enabled.
     */
    public boolean isOutline() {
        return bar.isOutline();
    }
    
    /**
     * Draws the startline. this is the line where the progressbar starts the
     * drawing around the image.
     * 
     * @param drawStartline
     *            true if it should or not.
     * @since 1.3.0
     */
    public void drawStartline(boolean drawStartline) {
        bar.setStartline(drawStartline);
    }
    
    /**
     * If the startline is enabled.
     * 
     * @return true if startline is enabled or not.
     */
    public boolean isStartline() {
        return bar.isStartline();
    }
    
    /**
     * Defines if the percent text should be shown or not. To modify the text
     * checkout {@link #setPercentStyle(PercentStyle)}.
     * 
     * @param showProgress
     *            true if it should or not.
     * @since 1.3.0
     */
    public void showProgress(boolean showProgress) {
        bar.setShowProgress(showProgress);
    }
    
    /**
     * If the progress text inside of the image is enabled.
     * 
     * @return true if it is or not.
     */
    public boolean isShowProgress() {
        return bar.isShowProgress();
    }
    
    /**
     * Sets a custom percent style to the text inside the image. Make sure you
     * set {@link #showProgress(boolean)} to true. Otherwise it doesn't shows.
     * The default settings are:</br>
     * <table>
     * <tr>
     * <th>Text align</td>
     * <td>CENTER</td>
     * </tr>
     * <tr>
     * <th>Text size</td>
     * <td>150 [dp]</td>
     * </tr>
     * <tr>
     * <th>Display percentsign</td>
     * <td>true</td>
     * </tr>
     * <tr>
     * <th>Custom text</td>
     * <td>%</td>
     * </tr>
     * </table>
     * 
     * @param percentStyle
     */
    public void setPercentStyle(PercentStyle percentStyle) {
        bar.setPercentStyle(percentStyle);
    }
    
    /**
     * Returns the {@link PercentStyle} of the percent text. Maybe returns the
     * default value, check {@link #setPercentStyle(PercentStyle)} fo that.
     * 
     * @return the percent style of the moment.
     */
    public PercentStyle getPercentStyle() {
        return bar.getPercentStyle();
    }
    
    /**
     * If the progress hits 100% then the progressbar disappears if this flag is
     * set to <code>true</code>. The default is set to false.
     * 
     * @param
     *            if it should disappear or not.
     * @since 1.4.0
     */
    public void setClearOnHundred(boolean clearOnHundred) {
        bar.setClearOnHundred(clearOnHundred);
    }
    
    /**
     * If the progressbar disappears when the progress reaches 100%.
     * 
     * @since 1.4.0
     */
    public boolean isClearOnHundred() {
        return bar.isClearOnHundred();
    }
    
    /**
     * Set an image resource directly to the ImageView.
     *
     * @param bitmap the {@link Bitmap} to set.
     */
    public void setImageBitmap(Bitmap bitmap){
        imageView.setImageBitmap(bitmap);
    }
    
    /**
     * Set the status of the indeterminate mode. The default is false. You can
     * still configure colour, width and so on.
     *
     * @param indeterminate true to enable the indeterminate mode (default true)
     * @since 1.6.0
     */
    public void setIndeterminate(boolean indeterminate) {
        bar.setIndeterminate(indeterminate);
    }
    
    /**
     * Returns the status of the indeterminate mode. The default status is false.
     *
     * @since 1.6.0
     */
    public boolean isIndeterminate() {
        return bar.isIndeterminate();
    }
    
    /**
     * Draws a line in the center of the way the progressbar has to go.
     *
     * @param drawCenterline
     *            true if it should or not.
     * @since 1.6.0
     */
    public void drawCenterline(boolean drawCenterline) {
        bar.setCenterline(drawCenterline);
    }
    
    /**
     * If the centerline is enabled or not.
     *
     * @return true if centerline is enabled.
     * @since 1.6.0
     */
    public boolean isCenterline() {
        return bar.isCenterline();
    }
    
    /**
     * Returns the {@link ImageView} that the progress gets drawn around.
     *
     * @return the main ImageView
     * @since 1.6.0
     */
    public ImageView getImageView(){
        return imageView;
    }
    
    }
    

    下面是这个类的布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    
    <com.example.yukun.webapplication.squareprogressbar.SquareProgressView
        android:id="@+id/squareProgressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/imageView1" />
    
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"
        android:padding="10dp"
        />
    </RelativeLayout>
    

    好了,下面是使用SquareProgressBar的类:

    private com.example.yukun.webapplication.squareprogressbar.SquareProgressBar progressBar;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        ShareSDK.initSDK(this);
        init();
    }
    boolean tag=true;
    int i=0;
    Handler handler=new Handler();
    private void init() {
        textView = (TextView) findViewById(R.id.text);
        progressBar = (com.example.yukun.webapplication.squareprogressbar.SquareProgressBar) findViewById(R.id.squar);
    
    //        progressBar.setImage(R.mipmap.ic_launcher);//可以在中间添加图片
        progressBar.setColor("#C60190");
        progressBar.setWidth(5);
        progressBar.drawOutline(true);
        stareThread();
        progressBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                i=0;
                tag=true;
                stareThread();
            }
        });
    
    }
    
    private void stareThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (tag){
                    try {
                        Thread.sleep(80);
    
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                i++;
                                if(i==99){
                                   // tag=false;
                                    progressBar.setProgress(0);
                                    i=0;
                                    //return;
                                }
                                progressBar.setProgress(i);
                                textView.setText(i+"%");
                            }
                        });
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    }
    

    布局文件:

       <RelativeLayout
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <com.example.yukun.webapplication.squareprogressbar.SquareProgressBar
            android:id="@+id/squar"
            android:layout_width="80dp"
            android:layout_height="80dp">
        </com.example.yukun.webapplication.squareprogressbar.SquareProgressBar>
            <TextView
                android:id="@+id/text"
                android:textSize="25sp"
                android:layout_centerInParent="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
      </RelativeLayout>
    

    简单的修改而已,自己做个笔记,以免忘记。

    相关文章

      网友评论

      • 2f3f79cbc52b:可以详细说下实现的思路吗 看自定义view看得头晕
        不识水的鱼: @一只不愿意透露姓名的汪 这里主要是修改了原来的一部分代码而已,自定义view.推荐你去看一下启舰的博客,百度一下就能看到,写了很多关于这方面的东西,很有用!
      • Javen205:效果不错

      本文标题:SquareProgressBar--一个方形的加载进度条

      本文链接:https://www.haomeiwen.com/subject/zeioettx.html