Android自定義荷載進(jìn)度的兩種方法
本文將用兩個(gè)方法來寫類似汽車荷載的進(jìn)度
用LinearLayout的addview方法加上for循環(huán)
用自定義控件的方法
先上截圖

1. 用LinearLayout的addview方法加上for循環(huán)
1.1 processtest01.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
1.2 LinearLayout的子布局view01.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/hezai_img"
android:layout_width="12.5dp"
android:layout_height="31.5dp"/>
</LinearLayout>
1.3 ProcessTest01.java
package com.example.progresstest;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ProcessTest01 extends Activity {
private LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.processtest01);
ll = (LinearLayout) findViewById(R.id.ll);
setProcess(16);
}
private void setProcess(int green){
LayoutInflater inflater = this.getLayoutInflater();
//有進(jìn)度就填充綠色圖片
for (int i = 0; i < green; i++) {
View v = inflater.inflate(R.layout.view01, null);
ImageView img = (ImageView) v.findViewById(R.id.hezai_img);
img.setImageResource(R.drawable.green);
ll.addView(v);
}
//沒有進(jìn)度就填充灰色圖片
for (int i = 0; i < 24-green; i++) {
View v = inflater.inflate(R.layout.view01, null);
ImageView img = (ImageView) v.findViewById(R.id.hezai_img);
img.setImageResource(R.drawable.gray);
ll.addView(v);
}
}
}
1.4 這里涉及了兩個(gè)帶邊界的圖片元素
最后得到的截圖

這種方法有好處也有壞處。這種方法簡(jiǎn)單易懂,且耗內(nèi)存少,運(yùn)行速度快等,但若要不同的顏色,就要自己截不同大小,不同顏色的圖片,并且還要左右?guī)чg距的,比較麻煩。下面的一種方法是自定義控件的方法,也是有好有壞。
2 用自定義控件的方法
2.1 myprocesstest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.progresstest.HezaiProgress
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:id="@+id/process"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</com.example.progresstest.HezaiProgress>
</LinearLayout>
這里的com.example.progresstest是HezaiProgress所在的包名,HezaiProgress是自定義的控件代碼。
2.2 HezaiProgress.java文件
package com.example.progresstest;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class HezaiProgress extends View{
private int process = 10;
private int width,height;
private int h = 32;
private int w = 12;
private int divideWidth=5;
private int maxCount = 24;
private int processColor=Color.GREEN;
private int j = 1;
private int num = 0;
public int getProcessColor() {
return processColor;
}
/**
* 自定義顏色,輸入的是“#ffffff”形式的顏色字符串
* @param processColor
*/
public void setProcessColor(String processColor) {
char c = processColor.charAt(0);
int r = 0;
int g = 0;
int b = 0;
//默認(rèn)顏色為綠色
int rgb = Color.GREEN;
if(c=='#'){
for(int i = 1;i<processColor.length();i++){
c = processColor.charAt(i);
if(i<3){
r += rOrgOrb(c,i);
}
else if(i<5){
g += rOrgOrb(c,i);
}else{
b += rOrgOrb(c,i);
}
}
rgb = Color.rgb(r, g, b);
}
this.processColor = rgb;
}
private int rOrgOrb(char c,int i){
num++;
char b = 0;
if(c>='0'&&c<='9'){
//j是用于判斷是十六進(jìn)制的哪一位
if(i==j){
//十六進(jìn)制的右數(shù)第二位
b += Integer.valueOf(c)*16;
}
if(i==j+1){
//十六進(jìn)制的右數(shù)第一位
b += Integer.valueOf(c);
}
}
//十六進(jìn)制的大寫字母形式
if(c>='A'&&c<='F'){
if(i==j){
//ascii碼減去55,如A的ASCII碼為65,減去55后等于10
b += ((int)c-55)*16;
}
if(i==j+1){
b += (int)c-55;
}
}
//十六進(jìn)制的小寫字母形式
if(c>='a'&&c<='f'){
if(i==j){
//ascii碼減去87,如a的ASCII碼為97,減去87后等于10
b += ((int)c-87)*16;
}
if(i==j+1){
b += (int)c-87;
}
}
//若計(jì)數(shù)為偶數(shù),則給j加2
if(num%2==0){
j=j+2;
}
return b;
}
//得到最大進(jìn)度數(shù)
public int getMaxCount() {
return maxCount;
}
//設(shè)置最大進(jìn)度數(shù)
public void setMaxCount(int maxCount) {
this.maxCount = maxCount;
}
//得到進(jìn)度
public int getProcess() {
return process;
}
//設(shè)置進(jìn)度
public void setProcess(int process) {
this.process = process;
}
//設(shè)置間隔寬度
public void setDivideWidth(int divideWidth){
this.divideWidth = divideWidth;
}
public HezaiProgress(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public HezaiProgress(Context context, AttributeSet attrs) {
this(context, attrs,0);
// TODO Auto-generated constructor stub
}
public HezaiProgress(Context context) {
this(context,null);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
setHezai(16, canvas);
}
//設(shè)置進(jìn)度條高度
public void setProcessHeight(int h){
if(h==0)
this.h = 32;
else
this.h = h;
}
//設(shè)置進(jìn)度條寬度
public void setProcessWidth(int w){
if(w==0)
this.w = 12;
else
this.w = w;
}
private void setHezai(int process,Canvas canvas){
Rect rect;
Paint paint;
int tmp = 2;
//進(jìn)度取整,如果進(jìn)度條總寬度大于指定控件寬度,則只顯示整數(shù)個(gè)進(jìn)度
if(process*(w+tmp)+tmp>width)
process = width/(w+divideWidth);
//顯示進(jìn)度
for (int i = 0;i<process;i++){
rect = new Rect(tmp, 2,w+tmp,h);
paint = new Paint();
tmp=tmp+w+divideWidth;
paint.setColor(processColor);
canvas.drawRect(rect, paint);
}
//顯示灰色進(jìn)度,及默認(rèn)沒進(jìn)度狀態(tài)狀態(tài)
for (int i = 0; i < maxCount-process; i++) {
rect = new Rect(tmp, 2,w+tmp,h);
paint = new Paint();
tmp=tmp+w+divideWidth;
paint.setColor(Color.rgb(211, 211, 211));
canvas.drawRect(rect, paint);
}
}
//dp轉(zhuǎn)換成px
private int dipToPx(int dip){
float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));//加0.5是為了四舍五入
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
//MeasureSpec.EXACTLY,精確尺寸
if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) {
width = widthSpecSize;
} else {
//如果不是精確尺寸,則顯示設(shè)置尺寸或默認(rèn)尺寸
width = w;
}
//MeasureSpec.AT_MOST,最大尺寸,只要不超過父控件允許的最大尺寸即可,MeasureSpec.UNSPECIFIED未指定尺寸
if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) {
//顯示設(shè)置尺寸或默認(rèn)尺寸
height = dipToPx(h);
} else {
height = heightSpecSize;
}
//設(shè)置控件實(shí)際大小
setMeasuredDimension(width, height);
}
}
代碼里做了詳細(xì)注釋,這里就不在說了。
2.3 ProcessTest.java
package com.example.progresstest;
import android.app.Activity;
import android.os.Bundle;
public class ProcessTest extends Activity {
private HezaiProgress process;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.myprocesstest);
process = (HezaiProgress) findViewById(R.id.process);
//設(shè)置進(jìn)度高度
process.setProcessHeight(63);
//設(shè)置進(jìn)度寬度
process.setProcessWidth(25);
//設(shè)置間隔寬度
process.setDivideWidth(10);
//設(shè)置進(jìn)度顏色
process.setProcessColor("#008b8b");
}
}
這里比第一種方法好的地方就是可以隨意改變高度,寬度,間隔寬度,進(jìn)度顏色等,而且只需要一句代碼就行,因?yàn)榍懊嬉呀?jīng)封裝好了,但似乎比較耗內(nèi)存。
這里是改變顏色后的截圖,就是本博客的第一張圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義控件ViewGroup實(shí)現(xiàn)標(biāo)簽云(四)
這篇文章主要為大家詳細(xì)介紹了Android自定義控件ViewGroup實(shí)現(xiàn)標(biāo)簽云的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
以一個(gè)著色游戲展開講解Android中區(qū)域圖像填色的方法
這篇文章主要介紹了Android中實(shí)現(xiàn)區(qū)域圖像顏色填充的方法,文中以一個(gè)著色游戲?yàn)槔v解了邊界的填充等各種填色操作,需要的朋友可以參考下2016-02-02
Android 不同Activity間數(shù)據(jù)的傳遞 Bundle對(duì)象的應(yīng)用
本篇文章小編為大家介紹,Android 不同Activity間數(shù)據(jù)的傳遞 Bundle對(duì)象的應(yīng)用。需要的朋友參考下2013-04-04
Android如何判斷當(dāng)前點(diǎn)擊位置是否在圓的內(nèi)部
這篇文章主要為大家詳細(xì)介紹了Android如何判斷當(dāng)前點(diǎn)擊位置是否在圓的內(nèi)部,解析拖動(dòng)圓形控件之內(nèi)響應(yīng)觸摸事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android使用Dialog風(fēng)格彈出框的Activity
這篇文章主要為大家詳細(xì)介紹了Android使用Dialog風(fēng)格彈出框的Activity,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
android APP登陸頁(yè)面適配的實(shí)現(xiàn)
這篇文章主要介紹了android APP登陸頁(yè)面適配的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09
Android Flutter實(shí)戰(zhàn)之為照片添加顏色濾鏡
這篇文章我們將利用TweenAnimationBuilder來實(shí)現(xiàn)一個(gè)圖片調(diào)色的過渡動(dòng)畫,從而實(shí)現(xiàn)為照片添加顏色濾鏡的效果,感興趣的可以了解一下2022-12-12
Android實(shí)現(xiàn)文件存儲(chǔ)并讀取的示例代碼
本篇文章主要介紹了Android實(shí)現(xiàn)文件存儲(chǔ)的示例代碼,文件內(nèi)容可以分別存儲(chǔ)在手機(jī)內(nèi)存和外存中,并且都可以讀去取出來,有興趣的可以了解一下。2017-01-01

