码迷,mamicode.com
首页 > 其他好文 > 详细

水平进度条ProgressBar

时间:2015-07-03 16:01:46      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

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

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>

效果图如下:

技术分享

技术分享



版权声明:本文为博主原创文章,未经博主允许不得转载。

水平进度条ProgressBar

标签:

原文地址:http://blog.csdn.net/zxy13167201381/article/details/46740705

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!