Android MarkTipsView文字標識控件使用方法
更新時間:2021年04月16日 10:44:46 作者:RelinRan
這篇文章主要為大家詳細介紹了Android MarkTipsView文字標識控件的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android MarkTipsView文字標識控件的具體代碼,供大家參考,具體內(nèi)容如下
效果預覽

使用
使用方式跟TextView使用方式一樣,背景設置直接setBackgroundColor
源碼:
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatTextView;
/**
* 繪制mark標注
*/
public class MarkTipsView extends AppCompatTextView {
/**
* 畫筆
*/
private Paint paint;
/**
* 寬度
*/
private int width;
/**
* 高度
*/
private int height;
/**
* 標識寬度
*/
private int markWidth = 12;
/**
* 標識高度
*/
private int markHeight = 12;
/**
* 圓角大小
*/
private float radius = 10;
/**
* 填充顏色
*/
private int solid = Color.parseColor("#F1AA44");
public MarkTipsView(Context context) {
super(context);
initAttributeSet(context, null);
}
public MarkTipsView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttributeSet(context, attrs);
}
public MarkTipsView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttributeSet(context, attrs);
}
private void initAttributeSet(Context context, AttributeSet attrs) {
paint = new Paint();
paint.setDither(true);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
TypedArray array = context.obtainStyledAttributes(attrs,new int[]{android.R.attr.background});
solid = array.getInt(0,solid);
array.recycle();
}
@Override
public void setBackgroundColor(int color) {
solid = color;
invalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getMeasuredWidth();
height = getMeasuredHeight();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom() + markHeight);
}
@Override
protected void onDraw(Canvas canvas) {
paint.setColor(solid);
//矩形
Path path = new Path();
RectF rect = new RectF(0, 0, width, height - markHeight);
path.addRoundRect(rect, radius, radius, Path.Direction.CCW);
//三角形
Path onePath = new Path();
onePath.lineTo(width / 2 - markWidth, rect.height());
onePath.lineTo(width / 2, height);
onePath.lineTo(width / 2 + markWidth, rect.height());
onePath.close();
path.addPath(onePath);
//繪制路徑
canvas.drawPath(path, paint);
super.onDraw(canvas);
//重繪文字
setText(getText());
}
/**
* 獲取底部標識箭頭高度
* @return
*/
public int getMarkHeight() {
return markHeight;
}
/**
* 設置底部標識箭頭高度
* @param markHeight
*/
public void setMarkHeight(int markHeight) {
this.markHeight = markHeight;
invalidate();
}
/**
* 獲取底部標識箭頭寬度
* @return
*/
public int getMarkWidth() {
return markWidth;
}
/**
* 設置底部標識箭頭寬度
* @param markWidth
*/
public void setMarkWidth(int markWidth) {
this.markWidth = markWidth;
invalidate();
}
/**
* 獲取圓角大小
* @return
*/
public float getRadius() {
return radius;
}
/**
* 設置圓角大小
* @param radius
*/
public void setRadius(float radius) {
this.radius = radius;
invalidate();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android使用glide加載gif動畫設置播放次數(shù)
這篇文章主要為大家詳細介紹了Android使用glide加載gif動畫設置播放次數(shù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Android直播系統(tǒng)平臺搭建之圖片實現(xiàn)陰影效果的方法小結
這篇文章主要介紹了Android直播系統(tǒng)平臺搭建, 圖片實現(xiàn)陰影效果的若干種方法,本文給大家?guī)砣N方法,每種方法通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2021-08-08
Android開發(fā)基礎之創(chuàng)建啟動界面Splash Screen的方法
這篇文章主要介紹了Android開發(fā)基礎之創(chuàng)建啟動界面Splash Screen的方法,以實例形式較為詳細的分析了Android定制啟動界面的布局及功能實現(xiàn)相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
淺談Android onTouchEvent 與 onInterceptTouchEvent的區(qū)別詳解
本篇文章小編為大家介紹,Android onTouchEvent 與 onInterceptTouchEvent的區(qū)別詳解。需要的朋友參考下2013-04-04

