<?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. redis 发布/订阅 模式

    发布/订阅模式的命令如下: * 进入发布订阅模式的客户端,不能执行除发布订阅模式以上命令的其他命令,否则出错.

  2. Javascript中call方法和apply方法用法和区别

    第一次在博客园上面写博客,知识因为看书的时候发现了一些有意思的知识,顺便查了一下资料,就发到博客上来了,希望对大家有点帮助. 连续几天阅读<javascript高级程序设计>这本书了,逐渐 ...

  3. AGC009D Uninity

    一些无关紧要的事: 似乎很久没写题解了……象征性地更一篇.另外很多blog都设了私密,不是很敢公开,不过说不定哪天会公开的. link 题意: 最优树的点分治:使得点分最大层数最小.(听说是经典问题) ...

  4. 【最短路Dijistra】【一般堆优化】【配对堆优化】

    突然觉得堆优化$O(log_n)$的复杂度很优啊,然而第n次忘记了$Dijistra$怎么写QAQ发现之前都是用的手写堆,这次用一下$stl$ #include<bits/stdc++.h> ...

  5. ZOJ 3626 Treasure Hunt I 树上DP

    E - Treasure Hunt I Time Limit:2000MS Memory Limit:65536KB Description Akiba is a dangerous country ...

  6. C#高级编程9-第8章 委托、lamdba表达式和事件

    委托.lamdba表达式和事件 1.引用方法 函数指针是一个指向内存位置的指针,不是类型安全的.无法判断实际指向.参数和返回类型也无从知晓..NET委托是类型安全的.定义了返回类型和参数类型,不仅包含 ...

  7. Git_解决冲突

    人生不如意之事十之八九,合并分支往往也不是一帆风顺的. 准备新的feature1分支,继续我们的新分支开发: $ git checkout -b feature1 Switched to a new ...

  8. weblogic部署异常: cvc-enumeration-valid: string value '3.0' is not a valid enumeration value for web-app-versionType in namespace http://java.sun.com/xml/ns/javaee:<null>

    尝试使用weblogic部署一个Demo应用,在选择应用目录后,报出下面的异常: VALIDATION PROBLEMS WERE FOUND problem: cvc-enumeration-val ...

  9. Maven最佳实践 划分模块 配置多模块项目 pom modules

    所有用Maven管理的真实的项目都应该是分模块的,每个模块都对应着一个pom.xml.它们之间通过继承和聚合(也称作多模块,multi-module)相互关联.那么,为什么要这么做呢?我们明明在开发一 ...

  10. [MySql]锁表与解锁

    摘要 为啥会出现锁表的情况?锁表会导致数据表的其他操作超时,频繁的插入修改查询很容易出现锁表的情况.如果遇到这种情况,临时的解决办法,可以通过下面的方式进行解锁.如果长期有效的解决,那么就需要优化项目 ...