這篇文章給大家介紹怎么在Android中實現(xiàn)一個高亮引導控件,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、雁山網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、html5、購物商城網(wǎng)站建設、集團公司官網(wǎng)建設、成都外貿網(wǎng)站制作、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為雁山等各大城市提供網(wǎng)站開發(fā)制作服務。代碼
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 方法來實現(xiàn)高亮區(qū)域的繪制; setRegion 設置一個高亮區(qū)域, setRegions 設置多個高亮區(qū)域;重寫 setBackgroundColor 來實現(xiàn)設置高亮背景色。
Region表示了一個高亮矩形區(qū)域,支持4種高亮類型,
RectRegion 矩形高亮區(qū)域
public class RectRegion implements Parcelable { public RectF rectF; //... Parcelable實現(xiàn)代碼 }
RoundRectRegion 圓角矩形高亮區(qū)域
public class RoundRectRegion extends RectRegion { public float rx, ry; //... Parcelable實現(xiàn)代碼 }
CircleRegion 圓形高亮區(qū)域
public class CircleRegion extends RectRegion { public float radius; //... Parcelable實現(xiàn)代碼 }
OvalRegion 橢圓高亮區(qū)域
public class OvalRegion extends RectRegion { //... Parcelable實現(xiàn)代碼 }
使用
創(chuàng)建一個GuideActivity,該Activity根布局是一個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>
啟動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中設置高亮區(qū)域
ArrayList<RectRegion> regions = getIntent().getParcelableArrayListExtra(EXTRA_REGION_LIST); HighLightLayout highLightLayout = findViewById(R.id.highLightLayout); highLightLayout.setRegions(regions);
關于怎么在Android中實現(xiàn)一個高亮引導控件就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)頁題目:怎么在Android中實現(xiàn)一個高亮引導控件-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://aaarwkj.com/article40/ccdheo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供服務器托管、網(wǎng)站排名、標簽優(yōu)化、動態(tài)網(wǎng)站、網(wǎng)站制作、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容