Android開(kāi)發(fā)筆記之:在ImageView上繪制圓環(huán)的實(shí)現(xiàn)方法
更新時(shí)間:2013年05月28日 10:25:15 作者:
本篇文章是對(duì)Android中在ImageView上繪制圓環(huán)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
繪制圓環(huán)其實(shí)很簡(jiǎn)單,有大概以下三種思路. 這里先說(shuō)網(wǎng)上提到的一種方法。思路是先繪制內(nèi)圓,然后繪制圓環(huán)(圓環(huán)的寬度就是paint設(shè)置的paint.setStrokeWidth的寬度),最后繪制外圓。
請(qǐng)看核心源碼:
<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,這種分三次來(lái)繪制的方法,可以將圓環(huán)的內(nèi)圓 圓環(huán) 和外圓的顏色設(shè)成不一樣的,對(duì)paint進(jìn)行三次設(shè)置。還可以將繪制圓環(huán)的paint透明度設(shè)成10左右就會(huì)有圓環(huán)透明的效果。
2,三次繪制時(shí)的canvas.drawCircle圓心都是(center,center),但三次半徑確實(shí)不一樣的。尤其是第二次繪制圓環(huán)的時(shí)候,半徑是innerCircle + 1 +ringWidth/2。這里的加1是第一次外圓paint.setStrokeWidth(2);寬度設(shè)成2,也就是說(shuō)單條線的寬度1。后面的ringWidth/2也是同理。
示例如下(底色是預(yù)覽攝像頭的視頻):

請(qǐng)看核心源碼:
復(fù)制代碼 代碼如下:
<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,這種分三次來(lái)繪制的方法,可以將圓環(huán)的內(nèi)圓 圓環(huán) 和外圓的顏色設(shè)成不一樣的,對(duì)paint進(jìn)行三次設(shè)置。還可以將繪制圓環(huán)的paint透明度設(shè)成10左右就會(huì)有圓環(huán)透明的效果。
2,三次繪制時(shí)的canvas.drawCircle圓心都是(center,center),但三次半徑確實(shí)不一樣的。尤其是第二次繪制圓環(huán)的時(shí)候,半徑是innerCircle + 1 +ringWidth/2。這里的加1是第一次外圓paint.setStrokeWidth(2);寬度設(shè)成2,也就是說(shuō)單條線的寬度1。后面的ringWidth/2也是同理。
示例如下(底色是預(yù)覽攝像頭的視頻):

您可能感興趣的文章:
- 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)交替效果
- 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)文章
Android使用多線程進(jìn)行網(wǎng)絡(luò)聊天室通信
這篇文章主要為大家詳細(xì)介紹了Android使用多線程進(jìn)行網(wǎng)絡(luò)聊天室通信,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android實(shí)現(xiàn)簡(jiǎn)單圖片壓縮的方法
這篇文章主要介紹了Android實(shí)現(xiàn)簡(jiǎn)單圖片壓縮的方法,詳細(xì)分析了Android針對(duì)圖片的讀取、縮放及保存等操作技巧,需要的朋友可以參考下2016-06-06
Android動(dòng)畫系列之幀動(dòng)畫和補(bǔ)間動(dòng)畫的示例代碼
Android 提供三種動(dòng)畫:幀動(dòng)畫、補(bǔ)間動(dòng)畫和屬性動(dòng)畫,本篇文章介紹幀動(dòng)畫以及補(bǔ)間動(dòng)畫的使用,屬性動(dòng)畫的使用將在后面的文章中分享,那就來(lái)復(fù)習(xí)一下這兩種動(dòng)畫的使用吧2020-09-09
Android藍(lán)牙通信聊天實(shí)現(xiàn)發(fā)送和接受功能
這篇文章主要為大家詳細(xì)介紹了Android藍(lán)牙通信聊天實(shí)現(xiàn)發(fā)送和接受功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
Android9.0 SystemUI 網(wǎng)絡(luò)信號(hào)欄定制修改的流程解析
這篇文章主要介紹了Android9.0 SystemUI 網(wǎng)絡(luò)信號(hào)欄定制修改的流程,本文通過(guò)圖文實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11

