溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Android中利用tipLayout實現(xiàn)一個帶箭頭的指引代碼

發(fā)布時間:2021-04-12 16:10:35 來源:億速云 閱讀:195 作者:Leah 欄目:移動開發(fā)

怎么在Android中利用tipLayout實現(xiàn)一個帶箭頭的指引代碼?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

引用方式

compile 'com.xiaowei:TriangleTipLayout:1.0.0'

實現(xiàn)思路

準備一個三角形指引的圖片即可。

先上代碼

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);
    }
  }

核心代碼如上,其思路是先繪制一個矩形,預留出三角形所需要的高度,最后將其三行圖片繪制出來。

配置指示器

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("您今日收入已到達10W+,牛逼。保持努力");

注意:當調(diào)用setText之后會invalidate()重新繪制;

看完上述內(nèi)容,你們掌握怎么在Android中利用tipLayout實現(xiàn)一個帶箭頭的指引代碼的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI