Android開發(fā)筆記之:在ImageView上繪制圓環(huán)的實(shí)現(xiàn)方法
請看核心源碼:
<SPAN xmlns="http://www.w3.org/1999/xhtml">package yan.guoqi.rectphoto;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;
public class DrawImageView extends ImageView {
private final Paint paint;
private final Context context;
public DrawImageView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.context = context;
this.paint = new Paint();
this.paint.setAntiAlias(true); //消除鋸齒
this.paint.setStyle(Style.STROKE); //繪制空心圓或 空心矩形
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
int center = getWidth()/2;
int innerCircle = dip2px(context, 83); //內(nèi)圓半徑
int ringWidth = dip2px(context, 10); //圓環(huán)寬度
// 第一種方法繪制圓環(huán)
//繪制內(nèi)圓
this.paint.setARGB(255, 138, 43, 226);
this.paint.setStrokeWidth(2);
canvas.drawCircle(center, center, innerCircle, this.paint);
//繪制圓環(huán)
this.paint.setARGB(255, 138, 43, 226);
this.paint.setStrokeWidth(ringWidth);
canvas.drawCircle(center, center, innerCircle + 1 +ringWidth/2, this.paint);
//繪制外圓
this.paint.setARGB(255, 138, 43, 226);
this.paint.setStrokeWidth(2);
canvas.drawCircle(center, center, innerCircle + ringWidth, this.paint);
super.onDraw(canvas);
}
/* 根據(jù)手機(jī)的分辨率從 dp 的單位 轉(zhuǎn)成為 px(像素) */
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}
</SPAN>
總結(jié):
1,這種分三次來繪制的方法,可以將圓環(huán)的內(nèi)圓 圓環(huán) 和外圓的顏色設(shè)成不一樣的,對paint進(jìn)行三次設(shè)置。還可以將繪制圓環(huán)的paint透明度設(shè)成10左右就會有圓環(huán)透明的效果。
2,三次繪制時的canvas.drawCircle圓心都是(center,center),但三次半徑確實(shí)不一樣的。尤其是第二次繪制圓環(huán)的時候,半徑是innerCircle + 1 +ringWidth/2。這里的加1是第一次外圓paint.setStrokeWidth(2);寬度設(shè)成2,也就是說單條線的寬度1。后面的ringWidth/2也是同理。
示例如下(底色是預(yù)覽攝像頭的視頻):

- Android實(shí)現(xiàn)長按圓環(huán)動畫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)占比動畫
- Android自定義View實(shí)現(xiàn)圓環(huán)交替效果
- Android中自定義View實(shí)現(xiàn)圓環(huán)等待及相關(guān)的音量調(diào)節(jié)效果
- Android自定義View之酷炫數(shù)字圓環(huán)
- Android自定義view實(shí)現(xiàn)半圓環(huán)效果
相關(guān)文章
AndroidUI組件SlidingTabLayout實(shí)現(xiàn)ViewPager頁滑動效果
這篇文章主要介紹了AndroidUI組件SlidingTabLayout實(shí)現(xiàn)ViewPager頁滑動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10自定義Adapter并通過布局泵LayoutInflater抓取layout模板編輯每一個item實(shí)現(xiàn)思路
自定義Adapter并通過布局泵LayoutInflater抓取layout模板編輯每一個item,下面我們開始學(xué)習(xí)這一篇的內(nèi)容,感興趣的朋友可以了解下哈2013-06-06Android Webview上的ssl warning的處理方式詳解及實(shí)例
這篇文章主要介紹了Android Webview上的ssl warning的處理方式詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02Android實(shí)現(xiàn)簡單的下拉刷新pulltorefresh
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡單的下拉刷新pulltorefresh的相關(guān)代碼,具有一定的實(shí)用性和操作價值,感興趣的小伙伴們可以參考一下2016-07-07Android實(shí)現(xiàn)一對一藍(lán)牙聊天APP
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)一對一藍(lán)牙聊天APP,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06基于Android自定義控件實(shí)現(xiàn)刮刮樂效果
這篇文章主要介紹了基于Android自定義控件實(shí)現(xiàn)刮刮樂效果 的相關(guān)資料,需要的朋友可以參考下2015-12-12Android EditText隨輸入法一起移動并懸浮在輸入法之上的示例代碼
這篇文章主要介紹了Android EditText隨輸入法一起移動并懸浮在輸入法之上,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06Android 下載網(wǎng)絡(luò)圖片并顯示到本地
本文主要介紹了Android實(shí)現(xiàn)下載網(wǎng)絡(luò)圖片并顯示到本地功能的示例代碼。具有很好的參考價值,下面跟著小編一起來看下吧2017-03-03