Android自定義View實(shí)現(xiàn)圓環(huán)交替效果
下面請(qǐng)先看效果圖:
看上去是不很炫的樣子,它的實(shí)現(xiàn)上也不是很復(fù)雜,重點(diǎn)在與onDraw()方法的繪制。
首先是我們的attrs文件:
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="firstColor" format="color"/> <attr name="secondColor" format="color"/> <attr name="circleWidth" format="dimension"/> <attr name="speed" format="integer"/> <declare-styleable name="CustomView"> <attr name="firstColor" /> <attr name="secondColor" /> <attr name="circleWidth" /> <attr name="speed" /> </declare-styleable> </resources>
接下來(lái)是我們重寫View類的自定義View類:
public class MySelfCircleView extends View {
/*
* 第一圈顏色
*/
int firstColor;
/*
* 第二圈顏色
*/
int secondColor;
/*
* 圓的寬度
*/
int circleWidth;
/*
* 速率
*/
int speed;
/*
* 畫筆
*/
Paint mPaint;
/*
* 進(jìn)度
*/
int mProgress;
/*
* 是否切換標(biāo)志
*/
boolean isNext;
public MySelfCircleView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomView, defStyleAttr, 0);
int n = typedArray.getIndexCount();
for(int i=0; i<n; i++){
int attr = typedArray.getIndex(i);
switch (attr) {
case R.styleable.CustomView_firstColor:
firstColor = typedArray.getColor(attr, Color.RED);
break;
case R.styleable.CustomView_secondColor:
secondColor = typedArray.getColor(attr, Color.RED);
break;
case R.styleable.CustomView_circleWidth:
circleWidth = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
break;
case R.styleable.CustomView_speed:
speed = typedArray.getInt(attr, 20);
break;
}
}
typedArray.recycle();
mPaint = new Paint();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
mProgress++;
if (mProgress == 360) {
mProgress = 0;
if (!isNext)
isNext = true;
else
isNext = false;
}
postInvalidate();
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
public MySelfCircleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MySelfCircleView(Context context) {
this(context, null);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int centre = getWidth() / 2; // 獲取圓心的x坐標(biāo)
int radius = centre - circleWidth / 2;// 半徑
mPaint.setStrokeWidth(circleWidth); // 設(shè)置圓環(huán)的寬度
mPaint.setAntiAlias(true); // 消除鋸齒
mPaint.setStyle(Paint.Style.STROKE); // 設(shè)置空心
RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用于定義的圓弧的形狀和大小的界限
if (!isNext) {// 第一顏色的圈完整,第二顏色跑
mPaint.setColor(firstColor); // 設(shè)置圓環(huán)的顏色
canvas.drawCircle(centre, centre, radius, mPaint); // 畫出圓環(huán)
mPaint.setColor(secondColor); // 設(shè)置圓環(huán)的顏色
canvas.drawArc(oval, -90, mProgress, false, mPaint); // 根據(jù)進(jìn)度畫圓弧
} else {
mPaint.setColor(secondColor); // 設(shè)置圓環(huán)的顏色
canvas.drawCircle(centre, centre, radius, mPaint); // 畫出圓環(huán)
mPaint.setColor(firstColor); // 設(shè)置圓環(huán)的顏色
canvas.drawArc(oval, -90, mProgress, false, mPaint); // 根據(jù)進(jìn)度畫圓弧
}
}
}
最后是我們的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:zhy="http://schemas.android.com/apk/res/com.example.myselfview" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.myselfview.view.MySelfCircleView android:layout_width="120dp" android:layout_height="120dp" android:layout_marginTop="20dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" zhy:circleWidth="15dp" zhy:firstColor="#D4F668" zhy:secondColor="#2F9DD2" zhy:speed="10" /> <com.example.myselfview.view.MySelfCircleView android:layout_width="200dp" android:layout_height="200dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" zhy:circleWidth="24dp" android:layout_marginBottom="40dp" zhy:firstColor="#16A3FA" zhy:secondColor="#D20F02" zhy:speed="5" /> </RelativeLayout>
總結(jié)
好了,到這里我們的效果就算大工告成,感興趣的朋友可以寫寫看,個(gè)人感覺(jué)自定義View需要大量的練習(xí),才能為我所用。希望本文對(duì)大家開(kāi)發(fā)Android能有所幫助。
- Android實(shí)現(xiàn)長(zhǎng)按圓環(huán)動(dòng)畫View效果的思路代碼
- Android自定義View實(shí)現(xiàn)圓環(huán)進(jìn)度條
- Android自定義View實(shí)現(xiàn)圓環(huán)帶數(shù)字百分比進(jìn)度條
- Android自定義view實(shí)現(xiàn)圓環(huán)效果實(shí)例代碼
- android自定義View實(shí)現(xiàn)圓環(huán)顏色選擇器
- Android自定義view繪制圓環(huán)占比動(dòng)畫
- Android中自定義View實(shí)現(xiàn)圓環(huán)等待及相關(guān)的音量調(diào)節(jié)效果
- Android自定義View之酷炫數(shù)字圓環(huán)
- Android開(kāi)發(fā)筆記之:在ImageView上繪制圓環(huán)的實(shí)現(xiàn)方法
- Android自定義view實(shí)現(xiàn)半圓環(huán)效果
相關(guān)文章
android 仿微信demo——注冊(cè)功能實(shí)現(xiàn)(服務(wù)端)
本篇文章主要介紹了微信小程序-閱讀小程序?qū)嵗【幱X(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望能給你們提供幫助2021-06-06
Android調(diào)用系統(tǒng)自帶瀏覽器打開(kāi)網(wǎng)頁(yè)的實(shí)現(xiàn)方法
在Android中可以調(diào)用自帶的瀏覽器,或者指定一個(gè)瀏覽器來(lái)打開(kāi)一個(gè)鏈接。只需要傳入一個(gè)uri,可以是鏈接地址。接下來(lái)通過(guò)本文給大家分享android 自帶瀏覽器打開(kāi)網(wǎng)頁(yè)的實(shí)現(xiàn)方法,需要的朋友參考下吧2017-09-09
Android毛玻璃背景效果簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要介紹了Android毛玻璃背景效果簡(jiǎn)單實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-08-08
Android?App設(shè)計(jì)規(guī)范深入講解
隨著安卓智能手機(jī)不停的更新?lián)Q代,安卓手機(jī)系統(tǒng)越來(lái)越完美,屏幕尺寸也越來(lái)越大啦,下面這篇文章主要給大家介紹了關(guān)于Android?App設(shè)計(jì)規(guī)范的相關(guān)資料,需要的朋友可以參考下2022-10-10
Android編程獲取包名,版本信息及VersionName名稱的方法
這篇文章主要介紹了Android編程獲取包名,版本信息及VersionName名稱的方法,涉及Android包及版本相關(guān)操作函數(shù)使用技巧,需要的朋友可以參考下2016-10-10
android自定義View實(shí)現(xiàn)手勢(shì)解鎖
這篇文章主要為大家詳細(xì)介紹了android自定義View實(shí)現(xiàn)手勢(shì)解鎖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android Studio 配置:自定義頭部代碼注釋及添加模版方式
這篇文章主要介紹了Android Studio 配置:自定義頭部代碼注釋及添加模版方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03

