水平進度條ProgressBar,進度條progressbar

來源:互聯網
上載者:User

水平進度條ProgressBar,進度條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>

如下:




著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.