Horizontal progress bar, progress bar

Source: Internet
Author: User

Horizontal progress bar, progress bar

Some time has not come to write, next I will continue to share the level progress bar written by this girl, hope to help beginners ~~~

MainActivityClass

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 MainActivityExtends 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 ());
// Get the custom progress bar
Bar = (CustomProgressBar) findViewById (R. id. item_progress_SeekBar );
// Set the maximum value of the progress bar
Bar. setMax (100 );
// Set the progress Value
Bar. setProgress (60 );
// Enable the thread
New Thread (yuanlirunnable). start ();
}

Runnable yuanlirunnable = new Runnable (){

@ Override
Public void run (){
Message = handler. obtainMessage ();

Try {
For (int I = 0; I <= 100; I ++ ){
/*
* Control the progress here
*/
// 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 class

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 );
}
// Circle angle of the progress bar
Public final int roundCorners = 15;
Shape getDrawableShape (){
Final float [] roundedCorners = new float [] {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 );
}

/** Convert dp to px */
Public static float dp2px (Context context, float dp ){
Final float scale = context. getResources (). getDisplayMetrics (). density;
Return dp * scale;
}
}

Activity_main.xml Layout

<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(In the drawable file)

<? 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 the color of the progress bar -->
<Stroke
Android: dashWidth = "2dip"
Android: width = "2dip"
Android: color = "#79E911"/> <! -- Stroke -->
</Shape>
</Item>
<! -- Fill progress color -->
<Item
Android: id = "@ android: id/progress"
Android: drawable = "@ drawable/red">
</Item>

</Layer-list>


Main. xml (in the menu file)

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

As follows:




Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.