广告
返回顶部
首页 > 资讯 > 移动开发 >Android自定义圆形进度条效果
  • 145
分享到

Android自定义圆形进度条效果

2024-04-02 19:04:59 145人浏览 独家记忆
摘要

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

本文实例为大家分享了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;


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);

        //第三步:
        
        //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();

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

--结束END--

本文标题: Android自定义圆形进度条效果

本文链接: https://www.lsjlt.com/news/123918.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作