标签:
有些时间没来写了 ,接下来继续分享下本姑娘写的水平进度条,望能帮到初学者~~~
MainActivity 类
package com.lanzx.customprogressbar;
import android.app.Activity;}
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>
效果图如下:
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/zxy13167201381/article/details/46740705