Android 自定义带数字的圆形进度条和中间是文字的圆形进度条View
2015-12-29 23:58
525 查看
这是一个自定义带数字的圆形进度条和中间是文字的圆形进度条View。自定义View的基本流程在这里就不多说了。(效果图在最后)
自定义View分好几种。我这个Demo主要用到了自己绘画的和组合的View。
首先看怎么自定义带数字的圆形进度条的View。注解读写在代码里了,在这里不多说。
类MyCirclePB自定义的View。具体代码如下:
自定义带数字的圆形进度条View基本就完事了。写一个XML布局就可以使用了。在这里就不单个写了,布局都整合在一起了,后面补上。
这个是一个组合View。
先写布局吧!my_cricle.xml布局如下:
主布局activity_main.xml如下:
具体的效果图如下:
自定义View分好几种。我这个Demo主要用到了自己绘画的和组合的View。
首先看怎么自定义带数字的圆形进度条的View。注解读写在代码里了,在这里不多说。
类MyCirclePB自定义的View。具体代码如下:
package com.yehu.z01_circlepb; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.util.Log; import android.view.View; import android.widget.Toast; import com.yehu.z01_circlepb.My_Cricle.My_CricleListern; public class MainActivity extends Activity { <span style="white-space:pre"> </span>private My_Cricle my_Cricle; <span style="white-space:pre"> </span>private MyCirclePB myCirclePB; <span style="white-space:pre"> </span>String[] strs = { "从未超越", "超越自我", "旗鼓相当" }; <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>protected void onCreate(Bundle savedInstanceState) { <span style="white-space:pre"> </span>super.onCreate(savedInstanceState); <span style="white-space:pre"> </span>setContentView(R.layout.activity_main); <span style="white-space:pre"> </span>// 圆形进条 <span style="white-space:pre"> </span>myCirclePB = (MyCirclePB) findViewById(R.id.my_criclePB); <span style="white-space:pre"> </span>myCirclePB.setOnClickListener(new View.OnClickListener() { <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void onClick(View v) { <span style="white-space:pre"> </span>myCirclePB.setEnabled(false); <span style="white-space:pre"> </span>new Thread() { <span style="white-space:pre"> </span>public void run() { <span style="white-space:pre"> </span>int n = 1000; <span style="white-space:pre"> </span>myCirclePB.setMax(n); <span style="white-space:pre"> </span>for (int i = 0; i <= n; i++) { <span style="white-space:pre"> </span>Log.e("TAG", "download i=" + i); <span style="white-space:pre"> </span>myCirclePB.setProgress(i); <span style="white-space:pre"> </span>if (myCirclePB.getProgress() == myCirclePB.getMax()) { <span style="white-space:pre"> </span>handler.sendEmptyMessage(2); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>SystemClock.sleep(5); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}; <span style="white-space:pre"> </span>}.start(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}); <span style="white-space:pre"> </span>// 文本 圆形框 <span style="white-space:pre"> </span>my_Cricle = (My_Cricle) findViewById(R.id.my_cricle); <span style="white-space:pre"> </span>my_Cricle.init_view(); <span style="white-space:pre"> </span>my_Cricle.setOnClickListener(new View.OnClickListener() { <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void onClick(View v) { <span style="white-space:pre"> </span>my_Cricle.setEnabled(false); <span style="white-space:pre"> </span>final MyCirclePB circlePB = (MyCirclePB) (((My_Cricle) v) <span style="white-space:pre"> </span>.getMyCirclePB()); <span style="white-space:pre"> </span>new Thread() { <span style="white-space:pre"> </span>public void run() { <span style="white-space:pre"> </span>int n = 1000; <span style="white-space:pre"> </span>circlePB.setMax(n); <span style="white-space:pre"> </span>for (int i = 0; i <= n; i++) { <span style="white-space:pre"> </span>Log.e("TAG", "download i=" + i); <span style="white-space:pre"> </span>circlePB.setProgress(i); <span style="white-space:pre"> </span>String str = null; <span style="white-space:pre"> </span>Message msg = Message.obtain(); <span style="white-space:pre"> </span>if (circlePB.getProgress() < circlePB.getMax() / 3) { <span style="white-space:pre"> </span>str = strs[0]; <span style="white-space:pre"> </span>msg.what = 3; <span style="white-space:pre"> </span>} else if (circlePB.getProgress() < circlePB <span style="white-space:pre"> </span>.getMax() * 2 / 3) { <span style="white-space:pre"> </span>str = strs[1]; <span style="white-space:pre"> </span>msg.what = 4; <span style="white-space:pre"> </span>} else { <span style="white-space:pre"> </span>str = strs[2]; <span style="white-space:pre"> </span>msg.what = 5; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>msg.obj = str; <span style="white-space:pre"> </span>handler.sendMessage(msg); <span style="white-space:pre"> </span>if (circlePB.getProgress() == circlePB.getMax()) { <span style="white-space:pre"> </span>handler.sendEmptyMessage(1); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>SystemClock.sleep(5); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}; <span style="white-space:pre"> </span>}.start(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>private Handler handler = new Handler() { <span style="white-space:pre"> </span>public void handleMessage(android.os.Message msg) { <span style="white-space:pre"> </span>String str = (String) msg.obj; <span style="white-space:pre"> </span>int flag = msg.what; <span style="white-space:pre"> </span>if (flag == 1) { <span style="white-space:pre"> </span>my_Cricle.setEnabled(true); <span style="white-space:pre"> </span>} else if (flag == 2) { <span style="white-space:pre"> </span>myCirclePB.setEnabled(true); <span style="white-space:pre"> </span>} else if (flag == 3) { <span style="white-space:pre"> </span>my_Cricle.getText2().setText(str); <span style="white-space:pre"> </span>my_Cricle.getText2().setTextColor(0xffff0000); <span style="white-space:pre"> </span>} else if (flag == 4) { <span style="white-space:pre"> </span>my_Cricle.getText2().setText(str); <span style="white-space:pre"> </span>my_Cricle.getText2().setTextColor(0xff0000ff); <span style="white-space:pre"> </span>} else if (flag == 5) { <span style="white-space:pre"> </span>my_Cricle.getText2().setText(str); <span style="white-space:pre"> </span>my_Cricle.getText2().setTextColor(0xff00ff00); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}; <span style="white-space:pre"> </span>}; }
自定义带数字的圆形进度条View基本就完事了。写一个XML布局就可以使用了。在这里就不单个写了,布局都整合在一起了,后面补上。
这个是一个组合View。
先写布局吧!my_cricle.xml布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <com.yehu.z01_circlepb.MyCirclePB android:id="@+id/mycirlepb" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical" > <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="aaa" /> <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="aaaa" /> <TextView android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="aaa" /> </LinearLayout> </RelativeLayout>类My_Cricle具体代码如下:
package com.yehu.z01_circlepb; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.RelativeLayout; import android.widget.TextView; public class My_Cricle extends RelativeLayout { private Context context; private View v; private TextView text2; private MyCirclePB myCirclePB; public MyCirclePB getMyCirclePB() { return myCirclePB; } /** * 这里写一些事件的回调 * @author sunmj * */ public interface My_CricleListern { public void back(String name); } public My_Cricle(Context context) { this(context, null); init(context); } public My_Cricle(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { // TODO Auto-generated method stub this.context = context; v = LayoutInflater.from(context).inflate(R.layout.my_cricle, this, true); text2 = (TextView) v.findViewById(R.id.text2); } /** * 这个方法里面写一些配置的文件例如 */ public void init_view() { myCirclePB = (MyCirclePB) v.findViewById(R.id.mycirlepb); int n = 100; myCirclePB.setMax(n); myCirclePB.setRoundColor(Color.GRAY); myCirclePB.setRoundProgressColor(Color.RED); myCirclePB.setRoundWidth(40); myCirclePB.setProgress(0); myCirclePB.setShowProgress(false); } public TextView getText2() { return text2; } //回调回去 public void set_listern(My_CricleListern nCricleListern) { nCricleListern.back("1111"); } }到这里基本就实现了带数字的圆形进度条,和中间是文字的圆形进度条。下面就是具体的使用了。
主布局activity_main.xml如下:
<LinearLayout 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:gravity="center_horizontal" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="圆形进度条" android:textSize="16sp" /> <com.yehu.z01_circlepb.MyCirclePB android:id="@+id/my_criclePB" android:layout_width="240dp" android:layout_height="200dp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="圆形文本框" android:textSize="16sp" /> <com.yehu.z01_circlepb.My_Cricle android:id="@+id/my_cricle" android:layout_width="180dp" android:layout_height="220dp" /> </LinearLayout>类MainActivity具体代码如下:
package com.yehu.z01_circlepb; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.util.Log; import android.view.View; import android.widget.Toast; import com.yehu.z01_circlepb.My_Cricle.My_CricleListern; public class MainActivity extends Activity { private My_Cricle my_Cricle; private MyCirclePB myCirclePB; String[] strs = { "从未超越", "超越自我", "旗鼓相当" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 圆形进条 myCirclePB = (MyCirclePB) findViewById(R.id.my_criclePB); myCirclePB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myCirclePB.setEnabled(false); new Thread() { public void run() { int n = 1000; myCirclePB.setMax(n); for (int i = 0; i <= n; i++) { Log.e("TAG", "download i=" + i); myCirclePB.setProgress(i); if (myCirclePB.getProgress() == myCirclePB.getMax()) { handler.sendEmptyMessage(2); } SystemClock.sleep(5); } }; }.start(); } }); // 文本 圆形框 my_Cricle = (My_Cricle) findViewById(R.id.my_cricle); my_Cricle.init_view(); my_Cricle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { my_Cricle.setEnabled(false); final MyCirclePB circlePB = (MyCirclePB) (((My_Cricle) v) .getMyCirclePB()); new Thread() { public void run() { int n = 1000; circlePB.setMax(n); for (int i = 0; i <= n; i++) { Log.e("TAG", "download i=" + i); circlePB.setProgress(i); String str = null; Message msg = Message.obtain(); if (circlePB.getProgress() < circlePB.getMax() / 3) { str = strs[0]; msg.what = 3; } else if (circlePB.getProgress() < circlePB .getMax() * 2 / 3) { str = strs[1]; msg.what = 4; } else { str = strs[2]; msg.what = 5; } msg.obj = str; handler.sendMessage(msg); if (circlePB.getProgress() == circlePB.getMax()) { handler.sendEmptyMessage(1); } SystemClock.sleep(5); } }; }.start(); } }); } private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { String str = (String) msg.obj; int flag = msg.what; if (flag == 1) { my_Cricle.setEnabled(true); } else if (flag == 2) { myCirclePB.setEnabled(true); } else if (flag == 3) { my_Cricle.getText2().setText(str); my_Cricle.getText2().setTextColor(0xffff0000); } else if (flag == 4) { my_Cricle.getText2().setText(str); my_Cricle.getText2().setTextColor(0xff0000ff); } else if (flag == 5) { my_Cricle.getText2().setText(str); my_Cricle.getText2().setTextColor(0xff00ff00); } }; }; }代码的不怎么简洁,一直在努力中……
具体的效果图如下:
相关文章推荐
- 喝酒不骑马的Android自学日记(11)-Viewpager
- Xamarin Mono For Android、Monotouch 安装、破解
- Android绘图(三)
- Android名片识别
- Android的SlidingDrawer用法总结
- Android头像缓存
- Android自定义页面指示器
- Android友盟消息推送
- Android之NDK开发
- Ubuntu14.04下编译Android4.4
- android RecyclerView 简单实现横竖布局穿插
- Android实战(八)------点击空白可消失的弹出层dialog中显示动态高度宽度的gridview
- Android实战(八)------点击空白可消失的弹出层dialog中显示动态高度宽度的gridview
- Android开发者网址导航
- Android 打造万能适配器
- 解压android系统映像文件system.img
- Android Studio如何添加外部资源
- RecycleView使用体验(一)
- Android Lint使用分析
- Android应用开发按下返回键退向后台运行