Android自定義view之圓形進度條

2021-09-23 23:31:39 字數 2186 閱讀 8547

本節介紹自定義view-圓形進度條

思路:根據前面介紹的自定義view內容可拓展得之;

1:新建類繼承自view

2:新增自定義view屬性

3:重寫ondraw(canvas canvas)

4:實現功能

下面上**

1.自定義view**:

public class customview extends view 

public customview(context context, attributeset attrs)

public customview(context context, attributeset attrs, int defstyleattr)

public void setcirclecolor(int color)

public int getcirclecolor()

public void setsecondcirclecolor(int color)

public int getsecondcolor()

public void setstrokewidth(float width)

public float getstrokewidth()

public void setprogress(float progress)

public float getprogress()

public void settotalprogress(float totalprogress)

public float gettotalprogress()

public void settextsize(float textsize)

public float gettextsize()

@override

protected void ondraw(canvas canvas) else if(style_type==1)

paint_progress.setcolor(secondcirclecolor);

//畫text

final paint paint_text=new paint();

paint_text.setantialias(true);

if(style_type==0)else if(style_type==1)

paint_text.settextsize(textsize);

paint_text.settextalign(align.center);

if(getwidth()!=getheight())elseelseelse if(style_type==1)

}canvas.drawtext((int)progress+"/"+(int)totalprogress, getleft()+getwidth()/2, gettop()+getheight()/2+textsize/4, paint_text);

} }}

2:attr屬性

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

3:xml布局檔案

當xml檔案中circle:style_type="stroke"時

當xml檔案中circle:style_type="stroke_and_fill"時

4:activity中呼叫

當xml檔案中circle:style_type="stroke_and_fill"時

Android自定義控制項之自定義View 二

效果如下圖 1 自定義ringview繼承view新增其構造方法並建立畫筆 public class ringview extends view protected boolean isrunning false public ringview context context public ring...

Android自定義View 自定義元件

自繪控制項也分兩種,自定義元件和自定義容器,自定義元件是繼承view類,自定義容器時繼承viewgrounp 今天主要分析下自定義元件 還是舉個例子來的實際些,假如我們要畫乙個最簡單的textview,首先想到的就是canvas.drawtext 方法,怎麼畫了?還是得一步一步來 1 寫乙個myte...

Android自定義View實現

android自定義view實現很簡單 繼承view或者其子類,重寫建構函式 ondraw,onmeasure 等函式,根據繼承的類的不同可能有所不同。如果自定義的view需要有自定義的屬性,需要在values下建立attrs.xml。在其中定義你的屬性。在使用到自定義view的xml布局檔案中需要...