Android Triangle Tags (custom textview controls)

Source: Internet
Author: User

Let's take a look at the effect

On the Code

Package Com.zph.view;

Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.Path;
Import Android.util.AttributeSet;
Import Android.widget.TextView;

public class Rotatetextview extends textview{

public RotateTextView(Context context) {    super(context);}public RotateTextView(Context context, AttributeSet attrs) {    super(context, attrs);}@Overrideprotected void onDraw(Canvas canvas) {    // 倾斜度45,上下左右居中    int w=getMeasuredWidth();    canvas.rotate(-45,w,w);    // 绘制这个三角形,你可以绘制任意多边形    Paint p1 = new Paint();    p1.setColor(Color.RED);// 设置红色    p1.setStyle(Paint.Style.FILL);    Path path = new Path();    path.moveTo((int)(w/3), (int)(w/3));// 此点为多边形的起点    path.lineTo((int)(w*1.67), (int)(w/3));    path.lineTo(w, w);    path.close(); // 使这些点构成封闭的多边形    canvas.drawPath(path, p1);    Paint p = new Paint();    p.setColor(Color.WHITE);// 设置白色    p.setTextSize((int)(w*0.23));//设置字体大小    canvas.drawText(text, (int)(w*0.66), (int)(w*0.60), p);    super.onDraw(canvas);}/** 要显示的文字 */private String text="新标签";public void setText(String text) {    this.text=text;    invalidate();//重绘刷新Textview内容}

}

Xml

   <com.zph.view.RotateTextView        android:id="@+id/test_rt1"        android:layout_alignRight="@+id/img"        android:layout_alignBottom="@+id/img"        android:layout_width="288px"        android:layout_height="288px"/>

Code implementation

RotateTextView rt;private void setTestRT(){    Log.i("TAG","RotateTextView");    rt= (RotateTextView) findViewById(R.id.test_rt1);    rt.setText("求推荐");    //需要在自定义控件中重写setext()要不然此处的setext()没有效果}

Android Triangle Tags (custom textview controls)

Related Article

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.