Android高亮引導(dǎo)控件的實(shí)現(xiàn)代碼
最近項(xiàng)目需求是實(shí)現(xiàn)高亮功能引導(dǎo)頁(yè)的效果,查了很多資料Android確實(shí)沒(méi)有類(lèi)似iOS的摳圖的現(xiàn)成控件,就自己寫(xiě)一個(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,重寫(xiě)了 onDraw 方法來(lái)實(shí)現(xiàn)高亮區(qū)域的繪制; setRegion 設(shè)置一個(gè)高亮區(qū)域, setRegions 設(shè)置多個(gè)高亮區(qū)域;重寫(xiě) setBackgroundColor 來(lái)實(shí)現(xiàn)設(shè)置高亮背景色。
Region表示了一個(gè)高亮矩形區(qū)域,支持4種高亮類(lèi)型,
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);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 自定義TextView實(shí)現(xiàn)滑動(dòng)解鎖高亮文字
- Android開(kāi)發(fā)listview選中高亮簡(jiǎn)單實(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中Drawable使用知識(shí)總結(jié)
本篇文章主要介紹了淺談Android中Drawable使用知識(shí)總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Android實(shí)現(xiàn)圖片的高斯模糊(兩種方式)
本文給大家分享兩種實(shí)現(xiàn)圖片的高斯模糊效果,非常不錯(cuò),具有參考借鑒價(jià)值,對(duì)android圖片高斯模糊效果感興趣的朋友一起看看吧2017-03-03
Android中通過(guò)view方式獲取當(dāng)前Activity的屏幕截圖實(shí)現(xiàn)方法
這篇文章主要介紹了Android中通過(guò)view方式獲取當(dāng)前Activity的屏幕截圖實(shí)現(xiàn)方法,本文方法相對(duì)簡(jiǎn)單,容易理解,需要的朋友可以參考下2014-09-09
Android 監(jiān)聽(tīng)?wèi)?yīng)用前/后臺(tái)切換實(shí)例代碼
本篇文章主要介紹了Android 監(jiān)聽(tīng)?wèi)?yīng)用前/后臺(tái)切換實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
Android自定義View之組合控件實(shí)現(xiàn)類(lèi)似電商app頂部欄
這篇文章主要為大家詳細(xì)介紹了Android自定義View之組合控件,實(shí)現(xiàn)類(lèi)似電商app頂部欄的相關(guān)資料,具有參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
Android編程開(kāi)發(fā)之Spinner組件用法
這篇文章主要介紹了Android編程開(kāi)發(fā)之Spinner組件用法,結(jié)合實(shí)例形式分析介紹了Android中Spinner組件的功能、定義及具體使用技巧,需要的朋友可以參考下2015-12-12
Android 中不用線程如何實(shí)現(xiàn)倒計(jì)時(shí)
本文給大家分享android中不用線程如何實(shí)現(xiàn)倒計(jì)時(shí)功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01

