Android 自定义带数字的圆形进度条和中间是文字的圆形进度条View

这是一个自定义带数字的圆形进度条和中间是文字的圆形进度条View。自定义View的基本流程在这里就不多说了。(效果图在最后)

自定义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 {


	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);
			}
		};
	};


}

自定义带数字的圆形进度条View基本就完事了。写一个XML布局就可以使用了。在这里就不单个写了,布局都整合在一起了,后面补上。

这个是一个组合View。

先写布局吧!my_cricle.xml布局如下:



    

    

        

        

        
    

类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如下:



    

    

    

    

类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 自定义带数字的圆形进度条和中间是文字的圆形进度条View_第1张图片







你可能感兴趣的:(Android)