Android自定義Progress控件的方法
progress各種各樣的都有,自定義大多數(shù)也是簡(jiǎn)單的,根據(jù)業(yè)務(wù)需求來(lái)自己定義,記錄一下,先上效果圖
本來(lái)想找個(gè)第三方改改就上的,不過(guò)自己的業(yè)務(wù)需求有點(diǎn)不搭,一下子沒(méi)找到合適的,也沒(méi)這么多時(shí)間去找了,想想還是自己寫(xiě)個(gè)吧,因?yàn)橐埠?jiǎn)單。
主要就是需求就是橢圓進(jìn)度,百分比跟隨漸變背景,這樣一想其實(shí)就是一個(gè)布局,然后控制里面的進(jìn)度長(zhǎng)度,或者移動(dòng),我這是控制長(zhǎng)度,這樣畢竟簡(jiǎn)單,而且擴(kuò)展好,以后進(jìn)度條有什么奇葩需求也好改。
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.support.annotation.AttrRes; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.TextView; /** * Created by LiuZhen on 2017/7/8. */ public class UpdateProgressBar extends FrameLayout { private TextView tv_progress; private int width; private ViewGroup.LayoutParams params; /** * The progress text offset. */ private int mOffset; /** * The progress text size. */ private float mTextSize; /** * The progress text color. */ private int mTextColor; private float default_text_size; /** * The progress area bar color. */ private int mReachedBarColor; /** * The bar unreached area color. */ private int mUnreachedBarColor; private final int default_reached_color = Color.rgb(66, 145, 241); private final int default_unreached_color = Color.rgb(204, 204, 204); private final int default_text_color = Color.rgb(66, 145, 241); public UpdateProgressBar(@NonNull Context context) { this(context,null); } public UpdateProgressBar(@NonNull Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public UpdateProgressBar(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int desiredWidth = 100; int desiredHeight = 100; int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int height; //Measure Width if (widthMode == MeasureSpec.EXACTLY) { //Must be this size width = widthSize; } else if (widthMode == MeasureSpec.AT_MOST) { //Can't be bigger than... width = Math.min(desiredWidth, widthSize); } else { //Be whatever you want width = desiredWidth; } //Measure Height if (heightMode == MeasureSpec.EXACTLY) { //Must be this size height = heightSize; } else if (heightMode == MeasureSpec.AT_MOST) { //Can't be bigger than... height = Math.min(desiredHeight, heightSize); } else { //Be whatever you want height = desiredHeight; } int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); ViewGroup.LayoutParams lp = child.getLayoutParams(); int childWidthSpec = getChildMeasureSpec(widthMeasureSpec, 0, lp.width); int childHeightSpec = getChildMeasureSpec(heightMeasureSpec, 0, lp.height); child.measure(childWidthSpec, childHeightSpec); } params = tv_progress.getLayoutParams(); params.width = ViewGroup.LayoutParams.WRAP_CONTENT; params.height = ViewGroup.LayoutParams.MATCH_PARENT; tv_progress.setLayoutParams(params); height = tv_progress.getMeasuredHeight(); //MUST CALL THIS setMeasuredDimension(width, height); } private void init(AttributeSet attrs, int defStyleAttr){ default_text_size = 8; //load styled attributes. final TypedArray attributes = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.UpdateProgressBar, defStyleAttr, 0); mTextSize = attributes.getDimension(R.styleable.UpdateProgressBar_update_text_size, default_text_size); mReachedBarColor = attributes.getResourceId(R.styleable.UpdateProgressBar_update_reached_color, default_reached_color); mUnreachedBarColor = attributes.getResourceId(R.styleable.UpdateProgressBar_update_unreached_color, default_unreached_color); mTextColor = attributes.getColor(R.styleable.UpdateProgressBar_update_text_color, default_text_color); setDefaultProgressBar(); mOffset = px2dip(3); attributes.recycle(); } private void setDefaultProgressBar(){ setBackgroundResource(mUnreachedBarColor); tv_progress = new TextView(getContext()); tv_progress.setTextSize(mTextSize); tv_progress.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL); tv_progress.setTextColor(mTextColor); tv_progress.setLines(1); tv_progress.setBackgroundResource(mReachedBarColor); tv_progress.setPadding(0,0,5,1); tv_progress.setText("0%"); addView(tv_progress); } public void setProgress(int progress){ tv_progress.setText(progress+"%"); int proWidth = width*progress/100; if (tv_progress.getWidth() < proWidth) params.width = proWidth;//這里不能填充mOffset,因?yàn)槭菣E圓進(jìn)度條,填充會(huì)導(dǎo)致橢圓寬度被進(jìn)度條覆蓋,導(dǎo)致不美觀 tv_progress.setLayoutParams(params); } /** * 根據(jù)手機(jī)的分辨率從 dp 的單位 轉(zhuǎn)成為 px(像素) */ public int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根據(jù)手機(jī)的分辨率從 px(像素) 的單位 轉(zhuǎn)成為 dp */ public int px2dip(float pxValue) { final float scale = getContext().getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * 將px值轉(zhuǎn)換為sp值,保證文字大小不變 */ public int px2sp(float pxValue) { final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); } /** * 將sp值轉(zhuǎn)換為px值,保證文字大小不變 */ public int sp2px(float spValue) { final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); } }
用法布局文件
<com.progressbar.example.UpdateProgressBar xmlns:pro="http://schemas.android.com/apk/res-auto" android:id="@+id/progress" android:layout_width="match_parent" android:layout_height="wrap_content" pro:update_text_size="6sp" pro:update_text_color="#FFFFFF" pro:update_unreached_color="@drawable/shape_corner_progressbg" pro:update_reached_color="@drawable/shape_corner_progressbar"/>
MainActivity
import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; import com.progressbar.NumberProgressBar; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends AppCompatActivity { private Timer timer; private UpdateProgressBar progressBar; private int progress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar = (UpdateProgressBar)findViewById(R.id.progress); timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { progress++; progressBar.setProgress(progress); if(progress == 100) { Toast.makeText(getApplicationContext(), getString(R.string.finish), Toast.LENGTH_SHORT).show(); // progress = 0; // progressBar.setProgress(0); timer.cancel(); } } }); } }, 1000, 100); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override protected void onDestroy() { super.onDestroy(); timer.cancel(); } }
漸變背景
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#4984f2"/> <gradient android:startColor="#4984f2" android:endColor="#000" /> <corners android:topLeftRadius="15dp" android:topRightRadius="15dp" android:bottomLeftRadius="15dp" android:bottomRightRadius="15dp"/> </shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#dadada"/> <gradient android:startColor="#FFF" android:endColor="#000" /> <corners android:topLeftRadius="15dp" android:topRightRadius="15dp" android:bottomLeftRadius="15dp" android:bottomRightRadius="15dp"/> </shape>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解析android中ProgressBar的用法
- android中ProgressDialog與ProgressBar的使用詳解
- android ListView和ProgressBar(進(jìn)度條控件)的使用方法
- Android ProgressBar進(jìn)度條和ProgressDialog進(jìn)度框的展示DEMO
- 實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
- Android自定義ProgressDialog進(jìn)度等待框
- Android ProgressBar進(jìn)度條使用詳解
- Android三種方式實(shí)現(xiàn)ProgressBar自定義圓形進(jìn)度條
- Android 自定義ProgressDialog進(jìn)度條對(duì)話框用法詳解
- Android ProgressDialog進(jìn)度條使用詳解
相關(guān)文章
Android實(shí)現(xiàn)帶數(shù)字的圓形進(jìn)度條(自定義進(jìn)度條)
在項(xiàng)目開(kāi)發(fā)中經(jīng)常遇到帶圓形進(jìn)度條的需求,在GitHub上逛了一圈,發(fā)現(xiàn)沒(méi)有,今天小編抽空給大家分享Android實(shí)現(xiàn)帶數(shù)字的圓形進(jìn)度條(自定義進(jìn)度條),需要的朋友參考下2017-02-02使用PlatformView將?Android?控件view制作成Flutter插件
這篇文章主要為大家介紹了使用PlatformView將?Android?控件view制作成Flutter插件實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android listview與adapter詳解及實(shí)例代碼
本文主要介紹Android listview與adapter的知識(shí)詳解,這里整理了相關(guān)資料及實(shí)現(xiàn)代碼和實(shí)現(xiàn)效果圖,有興趣的小伙伴可以參考下2016-09-09Flutter?日歷組件簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家介紹了Flutter?日歷組件簡(jiǎn)單實(shí)現(xiàn)的圖文示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android CameraX結(jié)合LibYUV和GPUImage自定義相機(jī)濾鏡
之前使用Camera實(shí)現(xiàn)了一個(gè)自定義相機(jī)濾鏡(Android自定義相機(jī)濾鏡 ),但是運(yùn)行起來(lái)有點(diǎn)卡頓,這次用Camerax來(lái)實(shí)現(xiàn)一樣的效果發(fā)現(xiàn)很流暢,在此記錄一下,也希望能幫到有需要的同學(xué)2021-12-12viewpager+photoview實(shí)現(xiàn)圖片查看器
這篇文章主要為大家詳細(xì)介紹了viewpager+photoview實(shí)現(xiàn)圖片查看器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android編程之頁(yè)面切換測(cè)試實(shí)例
這篇文章主要介紹了Android編程之頁(yè)面切換測(cè)試,實(shí)例分析了Android實(shí)現(xiàn)頁(yè)面點(diǎn)擊切換的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04android配合viewpager實(shí)現(xiàn)可滑動(dòng)的標(biāo)簽欄示例分享
本文主要介紹了android實(shí)現(xiàn)可滑動(dòng)的標(biāo)簽欄示例,配合viewpager作為標(biāo)簽欄,且可以設(shè)置每頁(yè)顯示的標(biāo)簽個(gè)數(shù),超出可滑動(dòng)顯示,需要的朋友可以參考下2014-02-02Android中pendingIntent與Intent的深入分析
這篇文章主要介紹了Android中pendingIntent的深入分析的相關(guān)資料,需要的朋友可以參考下2017-04-04Android中監(jiān)聽(tīng)判斷網(wǎng)絡(luò)連接狀態(tài)的方法
這篇文章主要介紹了Android中監(jiān)聽(tīng)判斷網(wǎng)絡(luò)連接狀態(tài)的方法,介紹了是否有網(wǎng)絡(luò)連接判斷、連接的類(lèi)型和監(jiān)聽(tīng)網(wǎng)絡(luò)狀態(tài)的方法,需要的朋友可以參考下2014-06-06