Android高亮引導(dǎo)控件的實(shí)現(xiàn)代碼
最近項(xiàng)目需求是實(shí)現(xiàn)高亮功能引導(dǎo)頁的效果,查了很多資料Android確實(shí)沒有類似iOS的摳圖的現(xiàn)成控件,就自己寫一個(gè),具體如下:
Demo

代碼
public class HighLightLayout extends FrameLayout {
private Paint mPaint;
private Path mPath = new Path();
private List<RectRegion> mRegions;
public HighLightLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(0xAA000000);
setWillNotDraw(false);
}
@Override
protected void onDraw(Canvas canvas) {
mPath.reset();
mPath.addRect(0, 0, getWidth(), getHeight(), Path.Direction.CCW);
for (RectRegion region : mRegions) {
RectF rectF = region.rectF;
if (region instanceof RoundRectRegion) {
RoundRectRegion roundRectRegion = (RoundRectRegion) region;
mPath.addRoundRect(rectF, roundRectRegion.rx, roundRectRegion.ry, Path.Direction.CW);
} else if (region instanceof CircleRegion) {
CircleRegion circleRegion = (CircleRegion) region;
float cX = (rectF.right + rectF.left) / 2;
float cY = (rectF.bottom + rectF.top) / 2;
mPath.addCircle(cX, cY, circleRegion.radius, Path.Direction.CW);
} else if (region instanceof OvalRegion) {
mPath.addOval(rectF, Path.Direction.CW);
} else {
mPath.addRect(rectF, Path.Direction.CW);
}
}
canvas.drawPath(mPath, mPaint);
}
public void setRegion(@NonNull RectRegion region) {
if (mRegions == null) {
mRegions = new ArrayList<>();
} else {
mRegions.clear();
}
mRegions.add(region);
invalidate();
}
public void setRegions(@NonNull List<RectRegion> regions) {
mRegions = regions;
invalidate();
}
@Override
public void setBackgroundColor(int color) {
mPaint.setColor(color);
}
}
HighLightLayout繼承自FrameLayout,重寫了 onDraw 方法來實(shí)現(xiàn)高亮區(qū)域的繪制; setRegion 設(shè)置一個(gè)高亮區(qū)域, setRegions 設(shè)置多個(gè)高亮區(qū)域;重寫 setBackgroundColor 來實(shí)現(xiàn)設(shè)置高亮背景色。
Region表示了一個(gè)高亮矩形區(qū)域,支持4種高亮類型,
RectRegion 矩形高亮區(qū)域
public class RectRegion implements Parcelable {
public RectF rectF;
//... Parcelable實(shí)現(xiàn)代碼
}
RoundRectRegion 圓角矩形高亮區(qū)域
public class RoundRectRegion extends RectRegion {
public float rx, ry;
//... Parcelable實(shí)現(xiàn)代碼
}
CircleRegion 圓形高亮區(qū)域
public class CircleRegion extends RectRegion {
public float radius;
//... Parcelable實(shí)現(xiàn)代碼
}
OvalRegion 橢圓高亮區(qū)域
public class OvalRegion extends RectRegion {
//... Parcelable實(shí)現(xiàn)代碼
}
使用
創(chuàng)建一個(gè)GuideActivity,該Activity根布局是一個(gè)HighLightLayout,可以在HighLightLayout中添加任何控件
<wangyi.blog.app.view.HighLightLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/highLightLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".GuideActivity"> </wangyi.blog.app.view.HighLightLayout>
啟動(dòng)GuideActivity 并傳遞需要高亮顯示的區(qū)域
ArrayList<RectRegion> regions = new ArrayList<>();
//矩形高亮
RectF rectF1 = LocationUtils.getViewLocation(mButton1);
RectRegion region1 = new RectRegion(rectF1);
regions.add(region1);
//圓角矩形高亮
RectF rectF2 = LocationUtils.getViewLocation(mButton2);
RoundRectRegion region2 = new RoundRectRegion(rectF2, 10, 10);
regions.add(region2);
//圓形高亮
RectF rectF3 = LocationUtils.getViewLocation(mButton3);
float radius = (rectF3.right - rectF3.left) / 2 + 20;
CircleRegion region3 = new CircleRegion(rectF3, radius);
regions.add(region3);
//橢圓高亮
RectF rectF4 = LocationUtils.getViewLocation(mButton4);
LocationUtils.expandRectF(rectF4, 40);
OvalRegion region4 = new OvalRegion(rectF4);
regions.add(region4);
Intent intent = new Intent(this, GuideActivity.class);
intent.putExtra(GuideActivity.EXTRA_REGION_LIST, regions);
startActivity(intent);
GuideActivity的onCreate中設(shè)置高亮區(qū)域
ArrayList<RectRegion> regions = getIntent().getParcelableArrayListExtra(EXTRA_REGION_LIST);
HighLightLayout highLightLayout = findViewById(R.id.highLightLayout);
highLightLayout.setRegions(regions);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 自定義TextView實(shí)現(xiàn)滑動(dòng)解鎖高亮文字
- Android開發(fā)listview選中高亮簡單實(shí)現(xiàn)代碼分享
- Android TextView實(shí)現(xiàn)詞組高亮的示例代碼
- Android中實(shí)現(xiàn)詞組高亮TextView方法示例
- Android TextView中部分文字高亮顯示
- Android 搜索結(jié)果匹配關(guān)鍵字且高亮顯示功能
- Android搜索結(jié)果顯示高亮實(shí)例(有數(shù)據(jù)滑動(dòng)底部自動(dòng)刷新)
- Android中TextView文本高亮和點(diǎn)擊行為的封裝方法
- Android基于RecyclerView實(shí)現(xiàn)高亮搜索列表
- Android實(shí)現(xiàn)仿微信tab高亮icon粘著手的滑動(dòng)效果
相關(guān)文章
Android串口開發(fā)之使用JNI實(shí)現(xiàn)ANDROID和串口通信詳解
這篇文章主要給大家介紹了關(guān)于Android串口開發(fā)之使用JNI實(shí)現(xiàn)ANDROID和串口通信的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
避免 Android中Context引起的內(nèi)存泄露
本文主要介紹Android中Context引起的內(nèi)存泄露的問題,這里對Context的知識(shí)做了詳細(xì)講解,說明如何避免內(nèi)存泄漏的問題,有興趣的小伙伴可以參考下2016-08-08
Android 中 Activity顯示隱式跳轉(zhuǎn)
這篇文章主要介紹了Android 中 Activity顯示隱式跳轉(zhuǎn)的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
Android 中StringBuffer 和StringBuilder常用方法
這篇文章主要介紹了Android 中StringBuffer 和StringBuilder的常用方法及區(qū)別介紹,需要的朋友可以參考下2017-02-02
android開發(fā)教程之a(chǎn)ndroid的handler使用方法
這篇文章主要介紹了android的handler使用方法,大家參考使用吧2014-01-01
Android編程之DatePicker和TimePicke簡單時(shí)間監(jiān)聽用法分析
這篇文章主要介紹了Android編程之DatePicker和TimePicke簡單時(shí)間監(jiān)聽用法,結(jié)合具體實(shí)例形式分析了時(shí)間控件DatePicker和TimePicke布局與具體功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-02-02

