<?xml version="1.0" encoding="utf-8"?

>

<resources>

     <declare-styleable name="ArrowTextView">

        <attr name="radius" format="dimension" />

        <attr name="arrowWidth" format="dimension" />

        <attr name="arrowInHeight" format="dimension" />

        <attr name="bg" format="color" />

    </declare-styleable>

</resources>

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



}

}

//使用方法

<com.example.sanjjiaoxing.ArrowTextView

        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" />

自己定义带三角形箭头的TextView的更多相关文章

  1. 如何重载ComboBox 使其下拉按钮(带下箭头的)和下拉列表的垂直滚动条的宽度改变?(自绘ComboBox) [转]

    原文地址:http://bbs.csdn.net/topics/390135022 http://blog.csdn.net/scsdn/article/details/4363299 想使用winf ...

  2. 纯Css绘制三角形箭头三种方法

    在制作网页的过程中少不了绘制类似图片的三角形箭头效果,虽然工程量不大,但是确实麻烦.在学习的过程中,总结了以下三种方法,以及相关的例子. 一.三种绘制三角形箭头方法 1.方法一:利用overflow: ...

  3. WPF进阶教程 - 使用Decorator自定义带三角形的边框

    原文:WPF进阶教程 - 使用Decorator自定义带三角形的边框 写下来,备忘. Decorator,有装饰器.装饰品的意思,很容易让人联想到设计模式里面的装饰器模式.Decorator类负责包装 ...

  4. 纯CCS绘制三角形箭头图案

    用CSS绘制三角形箭头.使用纯CSS,你只需要很少的代码就可以创作出各种浏览器都兼容的三角形箭头! CSS代码: /* create an arrow that points up */ div.ar ...

  5. 带左右箭头切换的自动滚动图片JS特效

    效果图 按钮 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  6. 用css制作一个三角形箭头

    剑走偏锋——用css制作一个三角形箭头   通常,我们做上图那个三角形,一般都是做张图,而且需要两张,因为一般都是下拉菜单的效果,需要有个hover的样式,箭头是反的.那是不是有更好的办法呢,毕竟要用 ...

  7. 纯CSS绘制的三角形箭头图案【原创】

    参考:http://www.webhek.com/css-triangles/ 使用上下左右的三角形箭头标志,直接用css即可完成,直接附上代码. css: div#up { width: 0px; ...

  8. [Android]自己定义带删除输入框

    在项目开发中,带删除button输入框也是人们经常常使用到的,该文章便介绍一下怎样创建一个带删除输入框.当中,须要解决的问题例如以下: a)创建自己定义editText类 b)在自己定义editTex ...

  9. Android自己定义视图(一):带下划线的TextView

    package com.francis.underlinetextviewtest; import android.content.Context; import android.content.re ...

随机推荐

  1. shell run 屏蔽一切log

    &命令: xxx >/dev/null 2>&1 &                           屏蔽一切logxxx >/tmp/xxx.log 2 ...

  2. django数据库操作-增删改查-多对多关系以及一对多(外键)关系

    一.一对多(外键) 例子:一个作者对应多本书,一本书只有一个作者 model代码: class Person(models.Model); name = models.CharField('作者姓名' ...

  3. hdu 5316 Magician(2015多校第三场第1题)线段树单点更新+区间合并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 题意:给你n个点,m个操作,每次操作有3个整数t,a,b,t表示操作类型,当t=1时讲a点的值改 ...

  4. Express浅析

    一.Express是什么? 首先Express是一个Node.js的Web框架,它的API使用各种HTTP实用程序方法和中间件,快速方便地创建强大的API,性能方面Express提供精简的基本web应 ...

  5. BZOJ 2749 HAOI 2012 外星人 数论 欧拉函数

    题意: 给出一个数,给出的形式是其分解质因数后,对应的质因数pi及其次数qi,问对这个数不停求phi,直至这个数变成1,需要多少次.(多组数据) 范围:pi <= 1e5,qi <= 1e ...

  6. hihocoder #1299 : 打折机票 线段树

    #1299 : 打折机票 题目连接: http://hihocoder.com/problemset/problem/1299 Description 因为思念新宿的"小姐姐"们, ...

  7. php的哈希函数

    哈希函数: echo password_hash("rasmuslerdorf", PASSWORD_DEFAULT)."\n"; 验证函数: boolean  ...

  8. mysql数据库表迁移

    @ 把老数据库中的某个表倒出成sql文件 $mysqldump -uroot -p123456 my_db > my_db.sql (输入密码) @ 在新环境中导入 $sudo apt-get ...

  9. Linux服务器压测/拷机软件收集

    最近公司采购了一批服务器,于是收集了一些拷机软件来压测服务器硬件性能.硬件的稳定相对来说比较重要,7x24小时无间断运行,主要看三个硬件:CPU.内存.硬盘. 下面是收集的一些教程,可能网址已经失效了 ...

  10. Python3使用setuptools

    旧版的setuptools已经不能用于Python3上了,国外有大神fork出了另一分支distribute,它可以支持Python3:     1 2 3 4 5 6 7 #下载 wget http ...