Android 帶箭頭的指引tipLayout實(shí)現(xiàn)示例代碼
本文介紹了Android 帶箭頭的指引tipLayout實(shí)現(xiàn)示例代碼,分享給大家,具體如下:

如上是從UI接過(guò)來(lái)的設(shè)計(jì)圖,要求三角形指示器需要?jiǎng)討B(tài)對(duì)齊上面的文本,需要?jiǎng)討B(tài)的實(shí)現(xiàn)對(duì)其三角形。
引用方式
compile 'com.xiaowei:TriangleTipLayout:1.0.0'
實(shí)現(xiàn)思路
準(zhǔn)備一個(gè)三角形指引的圖片即可。
先上代碼
final TextPaint textPaint = mTextView.getPaint();
final int textHeight = (int) (textPaint.descent() - textPaint.ascent());
mRect.set(0, DEFAULT_TOP_HEIGHT, getWidth(), getHeight() + textHeight - DEFAULT_TOP_HEIGHT);
canvas.drawRect(mRect, mRectPaint);
final String text = mTextView.getText().toString();
float left = 0;
if (mIsShowTriangle) {
if (mGravity == Gravity.LEFT || mGravity == Gravity.START) {
LayoutParams layoutParams = (LayoutParams) mTarget.getLayoutParams();
left = mTarget.getLeft() - layoutParams.rightMargin - layoutParams.leftMargin;
} else {
if (mTarget instanceof TextView) {
ViewParent viewParent = mTarget.getParent();
float textWidth = textPaint.measureText(text);
if (viewParent instanceof LinearLayout) {
final float width = mTarget.getWidth() / 2;
left = mTarget.getLeft() + width - (mBitmap.getWidth() / 2);
} else if (viewParent instanceof RelativeLayout) {
left = mTarget.getLeft() + textWidth / 2;
}
} else if (mTarget instanceof ImageView) {
final float width = mTarget.getWidth();
left = mTarget.getLeft() + (width / 2) - (mBitmap.getWidth() / 2);
}
}
canvas.drawBitmap(mBitmap, left, 0, mBitmapPaint);
}
}
核心代碼如上,其思路是先繪制一個(gè)矩形,預(yù)留出三角形所需要的高度,最后將其三行圖片繪制出來(lái)。
配置指示器
mTipsLayout.setRectBackgroundColor(Color.parseColor("#FFF8BE"));
mTipsLayout.setTextColor(Color.parseColor("#FF9B33"));
mTipsLayout.setTriangleBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_triangle_arrow));
mTipsLayout.setTriangleGravity(Gravity.START);
mTipsLayout.bindView(findViewById(R.id.text2));
mTipsLayout.setText("您今日收入已到達(dá)10W+,牛逼。保持努力");
注意:當(dāng)調(diào)用setText之后會(huì)invalidate()重新繪制;
實(shí)現(xiàn)效果如下

github: 源碼
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android使用 ScrollerView 實(shí)現(xiàn) 可上下滾動(dòng)的分類欄實(shí)例
本篇文章主要介紹了android使用 ScrollerView 實(shí)現(xiàn) 可上下滾動(dòng)的分類欄實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
官網(wǎng)項(xiàng)目Jetpack?Startup庫(kù)學(xué)習(xí)
這篇文章主要為大家介紹了官網(wǎng)項(xiàng)目Jetpack?Startup庫(kù)學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android之帶group指示器的ExpandableListView(自寫)
Android缺省的ExpandableListView的group header無(wú)法固定在界面上,在網(wǎng)上搜索了好多都不盡人意,于是乎在別人的基礎(chǔ)上改進(jìn)了一點(diǎn)點(diǎn),原理都差不多2013-06-06
Android利用Canvas標(biāo)點(diǎn)畫線并加入位移動(dòng)畫(1)
這篇文章主要為大家詳細(xì)介紹了Android利用Canvas標(biāo)點(diǎn)畫線并加入位移動(dòng)畫的第一篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
詳解Flutter中StatefulBuilder組件的使用
StatefulBuilder小部件可以在這些區(qū)域的狀態(tài)發(fā)生變化時(shí)僅重建某些小區(qū)域而無(wú)需付出太多努力。本文將來(lái)詳細(xì)講講它的使用,需要的可以參考一下2022-05-05
Android使用Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Android使用Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
如何在Android中實(shí)現(xiàn)漸顯按鈕的左右滑動(dòng)效果
本篇文章是對(duì)在Android中實(shí)現(xiàn)漸顯按鈕的左右滑動(dòng)效果進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android主項(xiàng)目與Module中R類的區(qū)別詳解
這篇文章主要給大家介紹了關(guān)于Android主項(xiàng)目與Module中R類的區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02
Android開(kāi)發(fā)實(shí)現(xiàn)ImageView加載攝像頭拍攝的大圖功能
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)ImageView加載攝像頭拍攝的大圖功能,涉及Android基于ImageView的攝像頭拍攝圖片加載、保存及權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11

