android 三角形 标签,自己定义带三角形箭头的TextView

>

package com.example.sanjjiaoxing;

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 android.util.TypedValue;

import android.view.View;

import android.widget.LinearLayout;

import android.widget.TextView;

/**

* @author wuxif_000  带三角形箭头的(三角形在一定高度居中,超过该高度无论......),四角带圆角,

*

*/

public class ArrowTextView extends TextView {

public ArrowTextView(Context context, AttributeSet attrs) {

super(context, attrs);

ini(context, attrs);

}

private void ini(Context context, AttributeSet attrs) {

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ArrowTextView);

radius=typedArray.getDimension(R.styleable.ArrowTextView_radius, 0);

arrowWidth=typedArray.getDimension(R.styleable.ArrowTextView_arrowWidth, 0);

arrowInHeight=typedArray.getDimension(R.styleable.ArrowTextView_arrowInHeight, 0);

color=typedArray.getColor(R.styleable.ArrowTextView_bg, Color.RED);

}

public ArrowTextView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

ini(context, attrs);

}

public ArrowTextView(Context context) {

super(context);

}

private float radius;

private float   arrowWidth;

/**

* 三角形箭头在此高度居中......

*/

private float  arrowInHeight;

private int color;

/**

* @param arrowWidth  三角形箭头的宽度.......

*/

public void setArrowWidth(float arrowWidth){

this.arrowWidth=arrowWidth;

invalidate();

}

/**

* @param arrowInHeight   三角形箭头在此高度居中......

*/

public void setArrowInHeight(float arrowInHeight){

this.arrowInHeight=arrowInHeight;

invalidate();

}

/**

* @param radius  矩形四角圆角的半径..........

*/

public void setRadius(float radius){

this.radius=radius;

invalidate();

}

/**

* @param color   箭头矩形的背景色.........

*/

public void setBgColor(int color){

this.color=color;

invalidate();

}

@Override

protected void onDraw(Canvas canvas) {

Paint paint=new Paint();

paint.setColor(color==0?Color.RED:color);

paint.setAntiAlias(true);

if(radius==0){

radius=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());

}

if(arrowWidth==0){

arrowWidth=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());

}

//带圆角的矩形(左边减去三角形的宽度...........)

int left = (int) (getPaddingLeft()-arrowWidth);

int height=getHeight();

canvas.drawRoundRect(new RectF(left, 0, getWidth(), height), radius, radius, paint);

if(arrowInHeight==0){

arrowInHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());

}

height = (int) (height>arrowInHeight?

arrowInHeight:height);

//画三角形

Path path=new Path();

path.setFillType(Path.FillType.EVEN_ODD);

float yMiddle = height/2;

float yTop=yMiddle-(arrowWidth/2);

float yBottom=yMiddle+(arrowWidth/2);

path.moveTo(0, yMiddle);

path.lineTo(left, yTop);

path.lineTo(left, yBottom);

path.lineTo(0, yMiddle);

path.close();

canvas.drawPath(path, paint);

//canvas.restore();

//canvas.translate(left, 0);

super.onDraw(canvas);

}

}

//使用方法

android:id="@+id/arrowText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_below="@+id/textView1"

android:layout_marginTop="63dp"

android:paddingBottom="10dp"

android:paddingLeft="15dp"

android:paddingRight="10dp"

android:paddingTop="10dp"

android:text="qqqqqqqqqqqqddddddsdfsfdfdfddddfdsfdfdfdfdfdfdfdfdddddddddddddddddqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"

wuxifu:bg="@color/green"

wuxifu:radius="10dp" />

17eed7bde2f47501a2fd8409f81ce3de.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值