Android條紋進(jìn)度條的實(shí)現(xiàn)(調(diào)整view寬度仿進(jìn)度條)
前言
本文主要給大家介紹了關(guān)于Android條紋進(jìn)度條(調(diào)整view寬度仿進(jìn)度條)的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧
方法如下:
美工同學(xué)指定了一個進(jìn)度條樣式
進(jìn)度條樣式
這斑斕的進(jìn)度條,如果要自己畫實(shí)在是勞民傷財(cái)。于是請美工切了一張素材。
素材樣例
如果用shape或者.9圖片不太好處理這個條紋。轉(zhuǎn)變思路,放置2張圖片。一張作為背景(底,bottom),一張作為進(jìn)度條圖片(cover)。
進(jìn)度改變時(shí),改變上面圖片的寬度。
這就要求上面的圖片是圓角的。自定義ImageView,調(diào)用canvas.clipPath
來切割畫布。
public class RoundCornerImageView extends android.support.v7.widget.AppCompatImageView { private float mRadius = 18; private Path mClipPath = new Path(); private RectF mRect = new RectF(); public RoundCornerImageView(Context context) { super(context); } public RoundCornerImageView(Context context, AttributeSet attrs) { super(context, attrs); } public RoundCornerImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void setRadiusDp(float dp) { mRadius = dp2px(dp, getResources()); postInvalidate(); } public void setRadiusPx(int px) { mRadius = px; postInvalidate(); } @Override protected void onDraw(Canvas canvas) { mRect.set(0, 0, this.getWidth(), this.getHeight()); mClipPath.reset(); // remember to reset path mClipPath.addRoundRect(mRect, mRadius, mRadius, Path.Direction.CW); canvas.clipPath(mClipPath); super.onDraw(canvas); } private float dp2px(float value, Resources resources) { return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, resources.getDisplayMetrics()); } }
每次繪制都切割一次圓角。記得調(diào)用Path.reset()
方法。
回到我們要的進(jìn)度條。布局文件中放置好層疊的圖片。
<RelativeLayout android:id="@+id/progress_layout" android:layout_width="190dp" android:layout_height="10dp" android:layout_centerInParent="true"> <ImageView android:id="@+id/p_bot_iv" android:layout_width="190dp" android:layout_height="10dp" android:src="@drawable/shape_round_corner_bottom" /> <com.rustfisher.view.RoundCornerImageView android:id="@+id/p_cover_iv" android:layout_width="100dp" android:layout_height="10dp" android:scaleType="centerCrop" android:src="@drawable/pic_cover_blue_white" /> </RelativeLayout>
需要在代碼中動態(tài)地改變cover的寬度;dialog中提供如下方法改變LayoutParams
public void updatePercent(int percent) { mPercent = percent; mPercentTv.setText(String.format(Locale.CHINA, "%2d%%", mPercent)); float percentFloat = mPercent / 100.0f; final int ivWidth = mBotIv.getWidth(); RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mProgressIv.getLayoutParams(); int marginEnd = (int) ((1 - percentFloat) * ivWidth); lp.width = ivWidth - marginEnd; mProgressIv.setLayoutParams(lp); mProgressIv.postInvalidate(); }
顯示出dialog并傳入進(jìn)度,就可以看到效果了。
這只是實(shí)現(xiàn)效果的一種方法,如果有更多的想法,歡迎和我交流~
相關(guān)代碼請參閱:
package com.rust.aboutview.activity; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; import android.support.v7.app.AppCompatActivity; import android.view.View; import com.rust.aboutview.R; import com.rust.aboutview.widget.RoundCornerProgressDialog; import com.rustfisher.view.RoundCornerImageView; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; /** * 圓角圖片示例 * Created by Rust on 2018/5/23. */ public class RoundCornerActivity extends AppCompatActivity implements View.OnClickListener { @BindView(R.id.r_iv_1) RoundCornerImageView mRIv1; @BindView(R.id.r_iv_2) RoundCornerImageView mRIv2; @BindView(R.id.r_iv_3) RoundCornerImageView mRIv3; @BindView(R.id.r_iv_4) RoundCornerImageView mRIv4; private Handler mMainHandler = new Handler(Looper.getMainLooper()); private RoundCornerProgressDialog mRoundCornerProgressDialog; private ProgressThread mProgressThread; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.act_round_corner); initUI(); } private void initUI() { ButterKnife.bind(this); mRIv1.setRadiusDp(12); mRIv2.setRadiusDp(23); mRIv3.setRadiusPx(40); mRIv4.setRadiusPx(200); } @OnClick(R.id.pop_dialog_btn) @Override public void onClick(View v) { switch (v.getId()) { case R.id.pop_dialog_btn: popRoundProgressDialog(); break; } } private void popRoundProgressDialog() { if (null == mRoundCornerProgressDialog) { mRoundCornerProgressDialog = new RoundCornerProgressDialog(); } mRoundCornerProgressDialog.setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTranslucentOrigin); mRoundCornerProgressDialog.show(getSupportFragmentManager(), RoundCornerProgressDialog.F_TAG); if (null != mProgressThread) { mProgressThread.interrupt(); try { mProgressThread.join(400); } catch (InterruptedException e) { e.printStackTrace(); } mProgressThread = null; } mProgressThread = new ProgressThread(); mProgressThread.start(); } private class ProgressThread extends Thread { private int progress = 0; @Override public void run() { super.run(); while (!isInterrupted()) { progress++; try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); break; } if (progress > 100) { progress = 0; } final int p = progress; mMainHandler.post(new Runnable() { @Override public void run() { mRoundCornerProgressDialog.updatePercent(p); } }); } } } }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- Android 七種進(jìn)度條的樣式
- Android中實(shí)現(xiàn)Webview頂部帶進(jìn)度條的方法
- android自定義進(jìn)度條漸變色View的實(shí)例代碼
- Android文件下載進(jìn)度條的實(shí)現(xiàn)代碼
- android ListView和ProgressBar(進(jìn)度條控件)的使用方法
- Android中自定義進(jìn)度條詳解
- Android編程之ProgressBar圓形進(jìn)度條顏色設(shè)置方法
- android自定義進(jìn)度條漸變圓形
- Android帶進(jìn)度的圓形進(jìn)度條
- Android自定義水平進(jìn)度條的圓角進(jìn)度
相關(guān)文章
Android ViewFlipper用法實(shí)例分析
這篇文章主要介紹了Android ViewFlipper用法,結(jié)合實(shí)例形式分析了ViewFlipper圖片操作的相關(guān)技巧,需要的朋友可以參考下2016-01-01android使用RxJava實(shí)現(xiàn)預(yù)加載
這篇文章主要為大家詳細(xì)介紹了android使用RxJava實(shí)現(xiàn)預(yù)加載的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android中判斷是否聯(lián)網(wǎng)實(shí)現(xiàn)代碼
這篇文章主要介紹了Android中判斷是否聯(lián)網(wǎng)實(shí)現(xiàn)代碼,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06Android實(shí)現(xiàn)壓縮字符串的方法示例
最近在做Android開發(fā),遇到了需要壓縮字符串的功能,下面這篇文章主要給大家介紹了Android實(shí)現(xiàn)壓縮字符串的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08Android使用ViewPager快速切換Fragment時(shí)卡頓的優(yōu)化方案
今天小編就為大家分享一篇關(guān)于Android使用ViewPager快速切換Fragment時(shí)卡頓的優(yōu)化方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12Android實(shí)現(xiàn)微信朋友圈圖片和視頻播放
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)微信朋友圈圖片和視頻播放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05Android基于虹軟(ArcSoft)實(shí)現(xiàn)人臉識別
人工智能時(shí)代快速來臨,其中人臉識別是當(dāng)前比較熱門的技術(shù),在國內(nèi)也越來越多的運(yùn)用,例如刷臉打卡,刷臉APP,身份識別,人臉門禁等。本文將為大家介紹Android基于虹軟(ArcSoft)實(shí)現(xiàn)人臉識別的demo,快來跟隨小編一起學(xué)習(xí)吧2021-12-12Kotlin協(xié)程flowOn與線程切換超詳細(xì)示例介紹
這篇文章主要介紹了Kotlin協(xié)程flowOn與線程切換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09Android實(shí)現(xiàn)購物車整體頁面邏輯詳解
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)購物車的整體頁面邏輯,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11