Custom TextView with triangle arrow and triangle arrow textview

Source: Internet
Author: User

Custom TextView with triangle arrow and triangle arrow textview

<? 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 the arrow with a triangle (the triangle is centered at a certain height and beyond this height, regardless of ......). The four corners have rounded corners,
*
*/
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;
/**
* The triangle arrow is centered at this height ......
*/
Private float arrowInHeight;
Private int color;
/**
* @ Param arrowWidth width of the triangle arrow .......
*/
Public void setArrowWidth (float arrowWidth ){
This. arrowWidth = arrowWidth;
Invalidate ();

}
/**
* @ Param arrowInHeight the triangle arrow centered at this height ......
*/
Public void setArrowInHeight (float arrowInHeight ){
This. arrowInHeight = arrowInHeight;
Invalidate ();
}
/**
* @ Param radius the radius of the rounded corner of the rectangle ..........
*/
Public void setRadius (float radius ){
This. radius = radius;
Invalidate ();

}
/**
* @ Param color the background color of the arrow rectangle .........
*/
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 ());
}
// Rectangle with rounded corners (left minus the width of the triangle ...........)
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 );
// Draw a triangle
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 );

}


}

// Usage

<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 = "qqqqqqqqqqqqddddddsdfsfdfdfddddfdsfdfdfdfdfdfdfdfdddddddddddddddddqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqq"
Wuxifu: bg = "@ color/green"
Wuxifu: radius = "10dp"/>





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.