水平进度条ProgressBar

有些时间没来写了 ,接下来继续分享下本姑娘写的水平进度条,望能帮到初学者~~~

MainActivity

package com.lanzx.customprogressbar;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView textnumber;
    private int num;
    private int progress=0;
    private Message message;
    CustomProgressBar bar;
    
    private Handler handler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int p=msg.what;
            //mPbar.setProgress(p);
            bar.setProgress(p);
        }
        
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        textnumber = (TextView)findViewById(R.id.textnumber);
        num = (int) (Math.random()*100);    
        textnumber.setText(String.valueOf(num+"/100").toString());
        //拿到自定义的进度条
        bar = (CustomProgressBar) findViewById(R.id.item_progress_SeekBar);
        //设置进度条的最大值
        bar.setMax(100);
        //设置进度值
        bar.setProgress(60);
        //开启线程
        new Thread(yuanlirunnable).start();
    }
    
    Runnable yuanlirunnable=new Runnable() {
        
        @Override
        public void run() {
            message=handler.obtainMessage();
        
            try {
                for (int i = 0; i <= 100; i++) {
                    /*
                     * 在这里控制进度
                     */
                    //int x=++progress;    
                    //int randomnumberprogress=(int) (Math.random()*100);                        
                    //int x=randomnumberprogress;                
                    int x=num;
                    message.what=x;                    
                    handler.sendEmptyMessage(message.what);
                    Thread.sleep(100);
                    
                }
                
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {        
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {       
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

CustomProgressBar类

package com.lanzx.customprogressbar;
import android.content.Context;
import android.graphics.drawable.shapes.RoundRectShape;
import android.graphics.drawable.shapes.Shape;
import android.util.AttributeSet;
import android.widget.ProgressBar;

public class CustomProgressBar extends ProgressBar {

    public CustomProgressBar(Context context) {
        super(context);
    }

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

    public CustomProgressBar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs, defStyle,0);

    }

    public CustomProgressBar(Context context, AttributeSet attrs, int defStyle, int styleRes) {        
        super(context, attrs, defStyle);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    //进度条圆角度
    public final int roundCorners = 15;
    Shape getDrawableShape() {
        final float[] roundedCorners = new float[] { 0, 0, 0, 0, 0, 0, 0, 0 };
        for(int i=0;i<roundedCorners.length;i++){
            roundedCorners[i] = dp2px(getContext(), roundCorners);
        }
        return new RoundRectShape(roundedCorners, null, null);
    }

    /**dp转换成px*/
    public static float dp2px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return dp * scale;
    }
}

activity_main.xml布局

<RelativeLayout 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:background="@android:color/black"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.lanzx.customprogressbar.MainActivity" >

    <com.lanzx.customprogressbar.CustomProgressBar
        android:id="@+id/item_progress_SeekBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="100dp"
        android:indeterminateOnly="false"
        android:max="100"
        android:progress="60"
        android:progressDrawable="@drawable/progressbar_drawable"
        android:visibility="visible" />

    <TextView
        android:id="@+id/textnumber"
        android:layout_width="wrap_content"
        android:layout_height="20dip"
        android:layout_alignBottom="@+id/item_progress_SeekBar"
        android:layout_alignRight="@+id/item_progress_SeekBar"
        android:layout_marginBottom="40dp"
        android:text="/100"
        android:textColor="#FFFFFF"
        android:textSize="15dip" />

</RelativeLayout>

progressbar_drawable.xml    (drawable文件中)

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/background">
        <shape>
            <corners
                android:bottomLeftRadius="15dp"
                android:bottomRightRadius="15dp"
                android:topLeftRadius="15dp"
                android:topRightRadius="15dp" />

            <solid android:color="#C9C7C7" /> <!-- #bfbfbf   这里是进度条的颜色 -->
            <stroke
                android:dashWidth="2dip"
                android:width="2dip"
                android:color="#79E911" /><!-- 描边 -->
        </shape>
    </item>
    <!-- 填充进度的颜色 -->
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/red">
    </item>

</layer-list>


main.xml    (menu文件中)

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.lanzx.customprogressbar.MainActivity" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>

</menu>

效果图如下:




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值