十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章给大家介绍怎么在Android中实现一个高亮引导控件,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
创新互联专注于临沂网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供临沂营销型网站建设,临沂网站制作、临沂网页设计、临沂网站官网定制、小程序开发服务,打造临沂网络公司原创品牌,更为您提供临沂网站排名全网营销落地服务。
代码
public class HighLightLayout extends FrameLayout { private Paint mPaint; private Path mPath = new Path(); private ListmRegions; 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 regions) { mRegions = regions; invalidate(); } @Override public void setBackgroundColor(int color) { mPaint.setColor(color); } }
HighLightLayout继承自FrameLayout,重写了 onDraw方法来实现高亮区域的绘制; setRegion设置一个高亮区域, setRegions设置多个高亮区域;重写 setBackgroundColor来实现设置高亮背景色。
Region表示了一个高亮矩形区域,支持4种高亮类型,
RectRegion 矩形高亮区域
public class RectRegion implements Parcelable { public RectF rectF; //... Parcelable实现代码 }
RoundRectRegion 圆角矩形高亮区域
public class RoundRectRegion extends RectRegion { public float rx, ry; //... Parcelable实现代码 }
CircleRegion 圆形高亮区域
public class CircleRegion extends RectRegion { public float radius; //... Parcelable实现代码 }
OvalRegion 椭圆高亮区域
public class OvalRegion extends RectRegion { //... Parcelable实现代码 }
使用
创建一个GuideActivity,该Activity根布局是一个HighLightLayout,可以在HighLightLayout中添加任何控件
启动GuideActivity 并传递需要高亮显示的区域
ArrayListregions = 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中设置高亮区域
ArrayListregions = getIntent().getParcelableArrayListExtra(EXTRA_REGION_LIST); HighLightLayout highLightLayout = findViewById(R.id.highLightLayout); highLightLayout.setRegions(regions);
关于怎么在Android中实现一个高亮引导控件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。