android 自己定义水平和圆形progressbar 仅仅定义一些style就能够

效果图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" >

改动progressbar 让他满足自己的需求一般来说仅仅改动一下,progressbar的style就能够满足需求了:

activity代码:只显示一个布局而已

package com.example.testcustomprogressbar;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.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"
    tools:context="com.example.testcustomprogressbar.MainActivity" >

    <ProgressBar
        android:id="@+id/my_customprogressbar1"
        style="@style/mycustom_progressbar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <ProgressBar
        android:id="@+id/my_customprogressbar2"
        style="@style/mycustom_progressbar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/my_customprogressbar1"
        android:layout_below="@+id/my_customprogressbar1"
        android:layout_marginTop="26dp" />

    <ProgressBar
        android:id="@+id/my_customprogressbar3"
        style="@style/mycustom_progressbar3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/my_customprogressbar2"
        android:layout_below="@+id/my_customprogressbar2"
        android:layout_marginTop="26dp" />

     <ProgressBar
        android:id="@+id/my_customprogressbar4"
        style="@style/mycustom_progressbar_hor"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/my_customprogressbar3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:max="100"
        android:progress="40"
        android:secondaryProgress="60" />

    <ProgressBar
        android:id="@+id/my_customprogressbar5"
        style="@style/mycustom_progressbar_hor2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/my_customprogressbar4"
        android:layout_below="@+id/my_customprogressbar4"
        android:layout_marginTop="26dp"
        android:max="100"
        android:progress="40"
        android:secondaryProgress="60" />

</RelativeLayout>

第一种圆形progressbar 只让一个图片旋转起来:

style的代码:

 <style name="mycustom_progressbar1">
        <item name="android:indeterminateDrawable">@drawable/progressbar_ver_image</item>
        <item name="android:minHeight">30dp</item>
        <item name="android:minWidth">30dp</item>
        <item name="android:maxHeight">60dp</item>
        <item name="android:maxWidth">60dp</item>
    </style>

progressbar_ver_image的代码:

<?

xml version="1.0" encoding="utf-8"?>
<!-- 定义一个旋转的动画 ,图片是我们自己定义的-->
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/resizeapi"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360" >

</rotate>

另外一种圆形progressbar 使用一个动画:

style:

 <style name="mycustom_progressbar2">
        <item name="android:indeterminateDrawable">@drawable/progressbar_ver_animation</item>
        <item name="android:minHeight">60dp</item>
        <item name="android:minWidth">60dp</item>
        <item name="android:maxHeight">100dp</item>
        <item name="android:maxWidth">100dp</item>
    </style>

progressbar_ver_animation的代码:

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

    <item
        android:drawable="@drawable/ok1"
        android:duration="250"/>
    <item
        android:drawable="@drawable/ok2"
        android:duration="250"/>
    <item
        android:drawable="@drawable/ok3"
        android:duration="250"/>
    <item
        android:drawable="@drawable/ok4"
        android:duration="250"/>

</animation-list>

第三种圆形progressbar 使用shape来构建drawable:

<style name="mycustom_progressbar3">
        <item name="android:indeterminateDrawable">@drawable/progressbar_ver_shape</item>
        <item name="android:minHeight">60dp</item>
        <item name="android:minWidth">60dp</item>
        <item name="android:maxHeight">100dp</item>
        <item name="android:maxWidth">100dp</item>
    </style>

progressbar_ver_shape的代码:

<?xml version="1.0" encoding="utf-8"?>
<!-- 定义一个旋转的动画 ,图片是我们自己定义的shape -->
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360" >
    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="8"
        android:useLevel="false" >
        <gradient
            android:centerColor="#FFFFFF"
            android:centerY="0.50"
            android:endColor="#1E90FF"
            android:startColor="#000000"
            android:type="sweep"
            android:useLevel="false" />
    </shape>

</rotate>

第四种: 横向progressbar 复制粘贴系统的style,然后改动:

<?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:radius="5dip" />
            <gradient
                    android:startColor="#ffff0000"
                    android:centerColor="#ffff0000"
                    android:centerY="0.75"
                    android:endColor="#ffff0000"
                    android:angle="270"
            />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#800000ff"
                        android:centerColor="#800000ff"
                        android:centerY="0.75"
                        android:endColor="#800000ff"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress"
        >
        <clip >

            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#ff00ff00"
                        android:centerColor="#ff00ff00"
                        android:centerY="0.75"
                        android:endColor="#ff00ff00"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>

</layer-list>

这样直接在progressbar中引用该style就能够满足需求了,当然我们也能够把progressbar background progress secondprogress都分别定义成一个drawable,然后使用:

background:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:radius="5dip" />
    <gradient
        android:angle="270"
        android:centerColor="#ffff0000"
        android:centerY="0.75"
        android:endColor="#ffff0000"
        android:startColor="#ffff0000" />
</shape>

progress:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <corners android:radius="5dip" />

    <gradient
        android:angle="270"
        android:centerColor="#ff00ff00"
        android:centerY="0.75"
        android:endColor="#ff00ff00"
        android:startColor="#ff00ff00" />

</shape>

secondprogress:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <corners android:radius="5dip" />

    <gradient
        android:angle="270"
        android:centerColor="#800000ff"
        android:centerY="0.75"
        android:endColor="#800000ff"
        android:startColor="#800000ff" />

</shape>

这个时候style中的代码就会清晰一点:

<?

xml version="1.0" encoding="utf-8"?

>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@android:id/background"
        android:drawable="@drawable/progressbar_hor_bg">
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip android:drawable="@drawable/progressbar_hor_second_progress" >
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip android:drawable="@drawable/progressbar_hor_progress" >
        </clip>
    </item>

    <!-- progress 这个地方原作者写的是一个点9的图-->
    <!-- <item android:id="@android:id/progress">
        <clip>
            <nine-patch android:src="@drawable/progress_patch_galy" />
        </clip>
    </item>   -->
</layer-list>

嘿嘿..最后,做人要厚道:神一般男人的原博主博文连接http://blog.csdn.net/mad1989/article/details/38042875

对了,代码还是贴上来吧.总感觉在ide中直接看代码会更舒服一些:http://download.csdn.net/detail/u010399316/9000197

时间: 2024-08-09 00:07:26

android 自己定义水平和圆形progressbar 仅仅定义一些style就能够的相关文章

android 自定义水平和圆形progressbar 只定义一些style就可以

效果图: 修改progressbar 让他满足自己的需求一般来说只修改一下,progressbar的style就可以满足需求了: activity代码:仅仅显示一个布局而已 package com.example.testcustomprogressbar; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public cl

Android圆形图片--自己定义控件

Android圆形图片控件效果图例如以下: 代码例如以下: RoundImageView.java package com.dxd.roundimageview; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Ca

Android自定义进度条-带文本(文字进度)的水平进度条(ProgressBar)

/** * 带文本提示的进度条 */ public class TextProgressBar extends ProgressBar { private String text; private Paint mPaint; public TextProgressBar(Context context) { super(context); initText(); } public TextProgressBar(Context context, AttributeSet attrs, int d

Android自定义带边框的圆形view

由于项目需要,需要做一个圆形的带边框并且里边还有文字的view →_→ ↓↓↓↓这样↓↓↓↓ 如果在布局文件中做的话是非常麻烦的,而且复用性也不高.所以想到用自定义一个view的来实现该功能,这样封装性和复用性就会相对提高,可方便在以后类似的项目中使用.可能也有同学有过这样的需求,所以在这分享出来供大家参考,不足之处还请多多指点. 看代码: 1package com.stock.manage.friend.view;import android.content.Context; 2 import

Maven的核心概念:maven仅仅定义了抽象的生命周期,具体的任务都是交由插件完成的

一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中唯一的一点. 1.2.Maven坐标主要组成 groupId:组织标识(包名) artifactId:项目名称 version:项目的当前版本 packaging:项目的打包方式,最为常见的jar和war两种 样例: 1.3.Maven为什么使用坐标? Maven世界拥有大量构建,我们需要找一个用来唯一标识一个构建的统一规范. 拥有了统一规范,就可以把查找工作交给机器. 二.依赖管理 2.1.依赖配置 依赖配置主要

Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示

Android中的ImageView只能显示矩形的图片,为了用户体验更多,Android实现圆角矩形,圆形或者椭圆等图形,一般通过自定义ImageView来实现,首先获取到图片的Bitmap,然后通过Paint和onDraw()进行圆形图片显示. 效果图: 代码: 1 activity_image.xml 2 <?xml version="1.0" encoding="utf-8"?> 3 <LinearLayoutxmlns:android=&q

Android OpenGL ES(七)基本几何图形定义 .

在前面Android OpenGL ES(六):创建实例应用OpenGLDemos程序框架 我们创建了示例程序的基本框架,并提供了一个“Hello World”示例,将屏幕显示为红色. 本例介绍OpenGL ES 3D图形库支持的几种基本几何图形,通常二维图形库可以绘制点,线,多边形,圆弧,路径等等.OpenGL ES 支持绘制的基本几何图形分为三类:点,线段,三角形.也就是说OpenGL ES 只能绘制这三种基本几何图形.任何复杂的2D或是3D图形都是通过这三种几何图形构造而成的. 比如下图复

自定义各式各样的圆形ProgressBar

     上面三个图分别是 开始时的样子,走进度时候的样子,最后完成的样子 自定义各式各样的圆形ProgressBar

Android OpenGL ES绘图教程之二 : 定义形状

在OpenGL ES view中可以定义要绘制图形的形状,是你创建高端图形杰作的第一步.在不知道一些基础的情况下来绘制会有点棘手,比如OpenGL ES是如何定义图形对象的. 本教程解释了OpenGL ES坐标系统与Android设备屏幕的关系,如果定义基础的形状,比如三角形和四边形. 1. 定义一个三角形 OpenGL ES允许你在三维坐标系统中定义绘制对象,所以在绘制三角形之前,必须定义它的坐标.在OpenGL里面,定义坐标的典型方式是定义一个浮点型的顶点坐标数组,为了最大化效率,将这些坐标