先来看一下效果
Android 三角标签(自定义Textview控件)_第1张图片

上代码

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

代码实现

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

更多相关文章

  1. Android ToggleButton控件的用法
  2. 两种方式使用android时间和日期控件
  3. android 下动态获取控件的id
  4. android动态生成控件
  5. Android 3D 旋转的三角形(三)
  6. design principle:模拟 android Button 控件点击事件
  7. 三,android编码规范 & 常用布局 & 常用控件
  8. 转帖并消化:Android中一种使用AttributeSet自定义控件的方法

随机推荐

  1. 藏不住了,Flink 未来发展的最新方向在这里
  2. js基础知识
  3. 利用openwrt编译添加zabbix_proxy3.4.10
  4. iframe与css基础
  5. CSS引入方式与选择器
  6. sed+awk 实现单个文件多行字符合并成单行
  7. C语言5.0
  8. 1Mbps能做什么?
  9. 你说的 Flink 和搜索引擎有什么关系
  10. 基于 Flink 的实时数仓生产实践