Android自定义圆形进度条效果

 更新时间:2021年04月19日 09:52:10   作者:dongxiaogang5_5  
这篇文章主要为大家详细介绍了Android自定义圆形进度条效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android自定义圆形进度条效果的具体代码,供大家参考,具体内容如下

1 控件 RoundProgress

package listview.tianhetbm.p2p.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import listview.tianhetbm.p2p.R;

/**
 * @date:2015/9/14
 * @author:dongxiaogang
 * @description: 自定义圆形进度条
 */
public class RoundProgress extends View {
    private Paint paint = new Paint();

    private int roundColor;
    private int roundProgressColor;
    private int textColor;
    private float textSize;
    private float roundWidth;
    private int max = 100;

    private int progress = 50;

    public RoundProgress(Context context) {
        this(context, null);
    }

    public RoundProgress(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundProgress);
        //圆环的颜色
        roundColor = ta.getColor(R.styleable.RoundProgress_roundColor, Color.RED);
        //圆环进度的颜色
        roundProgressColor = ta.getColor(R.styleable.RoundProgress_roundProgressColor, Color.GREEN);
        //中间进度百分比文字字符串的颜色
        textColor = ta.getColor(R.styleable.RoundProgress_textColor, Color.GREEN);
        //中间进度百分比的字符串的字体大小
        textSize = ta.getDimension(R.styleable.RoundProgress_textSize, 15);
        //圆环的宽度
        roundWidth = ta.getDimension(R.styleable.RoundProgress_roundWidth, 5);
        ta.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
//第一步:绘制一个最外层的圆
        paint.setColor(roundColor);
        paint.setStrokeWidth(roundWidth);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        int center = getWidth() / 2;
        int radius = (int) (center - roundWidth / 2-45);
        //canvas.drawCircle(center, center, radius, paint);
        RectF oval = new RectF(center - radius, center - radius, center + radius, center + radius);
        canvas.drawArc(oval, 135, 270, false, paint);
        //第二步:绘制正中间的文本
        float textWidth = paint.measureText(progress + "%");
        paint.setColor(textColor);
        paint.setTextSize(textSize);
        paint.setStrokeWidth(0);
        canvas.drawText(progress + "%", center - textWidth / 2, center + textSize / 2, paint);

        //第三步:
        /**
         * 参数解释:
         * oval:绘制弧形圈所包含的矩形范围轮廓
         * 0:开始的角度
         * 360 * progress / max:扫描过的角度
         * false:是否包含圆心
         * paint:绘制弧形时候的画笔
         */
        //RectF oval = new RectF(center - radius, center - radius, center + radius, center + radius);
        paint.setColor(roundProgressColor);
        paint.setStrokeWidth(roundWidth);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawArc(oval, 135, 270 * progress / max, false, paint);
        Log.e("测试角度",(270 * progress / max)+"");

        Paint mp=new Paint();
        mp.setAntiAlias(true);
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.tiger);
        int bitmapHeight = bitmap.getHeight()/2;
        int bitmapWidth = bitmap.getWidth()/2;
        //canvas.translate(-center, center);
        float y=0f,x=0f;
//        if(270 * progress / max<=45){
            y = (float) (center-bitmapWidth - (radius) * Math.cos((270 * progress / max+225)*Math.PI/180));
            x = (float) (center-bitmapWidth + (radius) * Math.sin((270 * progress / max+225)*Math.PI/180));
//        }
    //canvas.translate(center, center*2);
    Log.e("测试角度", y + "-----" + x);
    canvas.drawBitmap(bitmap, x, y, mp);


    }
    public void setProgress(int progress){
        this.progress = progress;
        if(progress>100){
            this.progress = 100;
        }
        postInvalidate();
    }
}

2 xml 布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">
    <listview.tianhetbm.p2p.ui.RoundProgress

        android:layout_marginTop="30dp"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_marginLeft="10dp"
        app:roundColor="@color/back_blue"
        app:roundProgressColor="@color/back_orange"
        android:id="@+id/ce"
        app:roundWidth="10dp"
        app:textSize="18sp"
        app:textColor="@color/record_red"
        />
</RelativeLayout>

3 activity(主要代码)

 super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_three);
        ButterKnife.bind(this);
        new Thread(){
           @Override
           public void run() {
               while (progress<80){
                   progress+=1;
                   ce.setProgress(progress);
                   try {
                       Thread.sleep(50);
                   } catch (Exception e) {
                       e.printStackTrace();
                   }

               }
           }
}.start();

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android开发笔记之Android中数据的存储方式(二)

    Android开发笔记之Android中数据的存储方式(二)

    我们在实际开发中,有的时候需要储存或者备份比较复杂的数据。这些数据的特点是,内容多、结构大,比如短信备份等,通过本文给大家介绍Android开发笔记之Android中数据的存储方式(二),对android数据存储方式相关知识感兴趣的朋友一起学习吧
    2016-01-01
  • Android Studio中使用SQLite数据库实现登录和注册功能

    Android Studio中使用SQLite数据库实现登录和注册功能

    SQLite是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中,下面这篇文章主要给大家介绍了关于Android Studio中使用SQLite数据库实现登录和注册功能的相关资料,需要的朋友可以参考下
    2024-06-06
  • 完美解决安卓jni项目会删除其他so文件的问题

    完美解决安卓jni项目会删除其他so文件的问题

    下面小编就为大家带来一篇完美解决安卓jni项目会删除其他so文件的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • Android开发Compose集成高德地图实例

    Android开发Compose集成高德地图实例

    这篇文章主要为大家介绍了Android开发Compose里使用高德地图实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Android图文居中显示控件使用方法详解

    Android图文居中显示控件使用方法详解

    这篇文章主要为大家详细介绍了Android图文居中显示控件的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-09-09
  • Android SharedPreferences的使用分析

    Android SharedPreferences的使用分析

    本篇文章小编为大家介绍,Android SharedPreferences的使用分析。需要的朋友参考下
    2013-04-04
  • 详解Android Studio Git分支实践

    详解Android Studio Git分支实践

    这篇文章主要介绍了Android Studio Git分支实践,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • Android基础之Activity生命周期

    Android基础之Activity生命周期

    activity类是Android 应用生命周期的重要部分。在系统中的Activity被一个Activity栈所管理。当一个新的Activity启动时,将被放置到栈顶,成为运行中的Activity,前一个Activity保留在栈中,不再放到前台,直到新的Activity退出为止。
    2016-05-05
  • Android EditText被软键盘遮盖的处理方法

    Android EditText被软键盘遮盖的处理方法

    android app新增了透明栏效果,结果发现键盘弹起后会遮盖屏幕底部的EditText,没有像想象中的调整窗口大小,并滚动ScrollView,将EditText显示在键盘上方。下面小编把解决方法记录一下,特此分享到脚本之家平台,感兴趣的朋友一起看看吧
    2016-10-10
  • Android编程之selector下设置背景属性值的方法

    Android编程之selector下设置背景属性值的方法

    这篇文章主要介绍了Android编程之selector下设置背景属性值的方法,结合实例形式分析了Android的selector背景选择器相关使用技巧,需要的朋友可以参考下
    2016-01-01

最新评论