最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 實(shí)現(xiàn)自定義圓形進(jìn)度條的實(shí)例代碼

 更新時(shí)間:2016年11月26日 08:58:44   投稿:lqh  
進(jìn)度條在Android中教程使用到,本文章向大家介紹一下Android自定義圓形進(jìn)度條實(shí)現(xiàn)代碼,需要的朋友可以參考一下。

Android 自定義圓形進(jìn)度條

今天無意中發(fā)現(xiàn)一個(gè)圓形進(jìn)度,想想自己實(shí)現(xiàn)一個(gè),如下圖:

基本思路是這樣的:

1.首先繪制一個(gè)實(shí)心圓

2.繪制一個(gè)白色實(shí)心的正方形,遮住實(shí)心圓

3.在圓的中心動(dòng)態(tài)繪制當(dāng)前進(jìn)度的百分比字符

4.繪制一個(gè)與之前實(shí)心圓相同顏色的空心圓

5.逐漸改變當(dāng)前的百分比

6.根據(jù)百分比,逐漸改變正方形的大小,逐漸減小正方形的底部y軸的坐標(biāo),不斷重繪,直到達(dá)到100%

首先看看自定義的屬性

在values目錄下新建attrs.xml內(nèi)容如下:

定義繪制圓形的背景色,和繪制圓形的半徑大小

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <attr name="circlecolor" format="color"></attr>
  <attr name="half" format="dimension"></attr>

  <declare-styleable name="myCircleImage">
    <attr name="circlecolor"></attr>
    <attr name="half"></attr>
  </declare-styleable>

</resources>

自定義視圖

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class CirclePro extends View {

 private Paint paint;
 private int circleBack;//圓的背景色
 private int mschedual = 0;//用于控制動(dòng)態(tài)變化
 float circleHalf; //圓的半徑
 String percent = "";//繪制百分比的字符串

 @SuppressLint("Recycle")
 public CirclePro(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 paint = new Paint();
 TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.myCircleImage, defStyleAttr,0);
 @SuppressWarnings("unused")
 int leng = array.length();
 //獲取自定義的屬性,這里注意是R.styleable.myCircleImage_circlecolor而不是R.attr.circlecolor
 circleBack = array.getColor(R.styleable.myCircleImage_circlecolor,Color.GREEN);
 circleHalf = array.getDimension(R.styleable.myCircleImage_half,200.f);
 System.out.println(circleBack);

 }

 /**
 * 這個(gè)構(gòu)造參數(shù),當(dāng)在布局文件中引用該view的時(shí)候,必須重寫該構(gòu)造函數(shù)
 * @param context
 * @param attrs
 */
 public CirclePro(Context context, AttributeSet attrs) {
 this(context, attrs, 0);//調(diào)用自己的構(gòu)造函數(shù)

 }

 /**
 * 根據(jù)文本的
 * @param text
 * @param textSize
 * @return
 */
 public float getTextWidth(String text,float textSize) {

 TextPaint textPaint = new TextPaint();
 textPaint.setTextSize(textSize);
 return textPaint.measureText(text);
 }

 @Override
 protected void onDraw(Canvas canvas) {
 // TODO Auto-generated method stub
 super.onDraw(canvas);

 float height = getHeight();
 float width = getWidth();
// float circleHalf = (float) (width*0.7/2);

 paint.setColor(circleBack);
 paint.setAntiAlias(true);
 paint.setStyle(Paint.Style.FILL);
 canvas.drawCircle(width/2,height/2,circleHalf, paint);//畫實(shí)心圓

 if (mschedual <= 100) {//,如果當(dāng)前進(jìn)度小于100,畫實(shí)心矩形
  paint.setColor(Color.WHITE);
  canvas.drawRect(width/2-circleHalf,height/2-circleHalf,width/2+circleHalf,height/2+circleHalf - mschedual*circleHalf/50, paint);
 }

 //畫當(dāng)前進(jìn)度的字符串
 paint.setColor(Color.BLACK);
 paint.setTextSize(30.f);
 percent = mschedual+" %";
 canvas.drawText(percent, width/2-getTextWidth(percent,30)/2,height/2+paint.getTextSize()*3/8, paint);//字體的高度=paint.getTextSize()*3/4

 //畫空心圓
 paint.setColor(circleBack);
 paint.setStyle(Paint.Style.STROKE);
 canvas.drawCircle(width/2,height/2,circleHalf, paint);

 if (mschedual < 100) {//更改當(dāng)前進(jìn)度值,并重繪
  mschedual++;
  invalidate();
 }
 }
}

在activity_main.xml中,需要用到自定義的屬性,首先添加命名空間:

xmlns:liu=”http://schemas.android.com/apk/res/com.example.androidcirclepro”

其中l(wèi)iu是自定義的一個(gè)前綴,隨意命名的,com.example.androidcirclepro是我們的應(yīng)用的包名

activity_main.xmln內(nèi)容如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:liu="http://schemas.android.com/apk/res/com.example.androidcirclepro"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity" >

  <com.example.androidcirclepro.CirclePro
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    liu:half="90dp"
    liu:circlecolor="#fff0f0"
    />

</RelativeLayout>

至此一個(gè)自定義的圓形進(jìn)度條就完成了,是不是很簡單。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • Android 為ListView添加分段標(biāo)頭的方法

    Android 為ListView添加分段標(biāo)頭的方法

    下面小編就為大家?guī)硪黄狝ndroid 為ListView添加分段標(biāo)頭的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android框架RePlugin使用詳解

    Android框架RePlugin使用詳解

    這篇文章給大家分享了Android 插件化框架 RePlugin使用心得,對(duì)此有興趣的朋友參考學(xué)習(xí)下。
    2018-07-07
  • Android開發(fā)之BottomSheetDialog組件的使用

    Android開發(fā)之BottomSheetDialog組件的使用

    BottomSheetDialog是底部操作控件,可在屏幕底部創(chuàng)建一個(gè)支持滑動(dòng)關(guān)閉視圖。本文將通過示例詳細(xì)講解它的使用,感興趣的小伙伴可以了解一下
    2023-01-01
  • Android用ActionBar高仿微信主界面的實(shí)例代碼

    Android用ActionBar高仿微信主界面的實(shí)例代碼

    這篇文章主要介紹了Android用ActionBar高仿微信主界面的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05
  • Android圖片緩存之Bitmap詳解(一)

    Android圖片緩存之Bitmap詳解(一)

    這篇文章主要為大家詳細(xì)介紹了Android圖片緩存之Bitmap,點(diǎn)學(xué)習(xí)一下Bitmap、BitmapFactory這兩個(gè)類,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android View與Compose互相調(diào)用實(shí)例探究

    Android View與Compose互相調(diào)用實(shí)例探究

    這篇文章主要介紹了Android View與Compose互相調(diào)用,Compose 具有超強(qiáng)的兼容性,兼容現(xiàn)有的所有代碼,Compose 能夠與現(xiàn)有 View 體系并存,可實(shí)現(xiàn)漸進(jìn)式替換
    2023-01-01
  • 詳解Flutter中key的正確使用方式

    詳解Flutter中key的正確使用方式

    這篇文章主要為大家介紹了詳解Flutter中key的正確使用方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Android?Retrofit使用詳細(xì)教程

    Android?Retrofit使用詳細(xì)教程

    Retrofit是Android用來接口請(qǐng)求的網(wǎng)絡(luò)框架,內(nèi)部是基于OkHttp實(shí)現(xiàn)的,retrofit負(fù)責(zé)接口請(qǐng)求的封裝,retrofit可以直接將接口數(shù)據(jù)解析為Bean類、List集合等,直接簡化了中間繁瑣的數(shù)據(jù)解析過程,這篇文章主要介紹了Android?Retrofit使用詳情,需要的朋友可以參考下
    2024-03-03
  • Android開發(fā)實(shí)現(xiàn)從相冊(cè)中選擇照片功能詳解

    Android開發(fā)實(shí)現(xiàn)從相冊(cè)中選擇照片功能詳解

    這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)從相冊(cè)中選擇照片功能,涉及Android權(quán)限控制、事件綁定、文件路徑與獲取等相關(guān)操作技巧,需要的朋友可以參考下
    2019-03-03
  • Android基于Http協(xié)議實(shí)現(xiàn)文件上傳功能的方法

    Android基于Http協(xié)議實(shí)現(xiàn)文件上傳功能的方法

    這篇文章主要介紹了Android基于Http協(xié)議實(shí)現(xiàn)文件上傳功能的方法,結(jié)合實(shí)例形式分析了Android的HTTP協(xié)議原理與文件上傳功能實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-07-07

最新評(píng)論

无为县| 景宁| 弥渡县| 文安县| 房山区| 大港区| 贺兰县| 巴马| 彭阳县| 和田市| 邛崃市| 邓州市| 余庆县| 湖南省| 永宁县| 财经| 沁水县| 丹寨县| 宿迁市| 沾益县| 西华县| 长阳| 隆回县| 图片| 宁陵县| 金坛市| 工布江达县| 阜阳市| 贞丰县| 宝清县| 扎赉特旗| 焉耆| 新安县| 关岭| 石狮市| 文山县| 榆树市| 兴业县| 安溪县| 怀集县| 西丰县|