Android右箭头的显示文字的View

package com.example.demo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;

public class RightView extends View implements Runnable {
 // 声明Paint对象
 private Paint mPaint = null;
 private TextPaint mTextPaint=null;
 private int DEFAULT_VIEW_WIDTH=300;
 private int DEFAULT_VIEW_HEIGHT=60;
 private int width;
 private int height;
 private int width1;
 private int textSize=14;
 private String text="你好吗?";
 private DisplayMetrics dm;

 public RightView(Context context) {
 // TODO Auto-generated constructor stub
 super(context);
 init(context);
 mPaint = new Paint();
 mTextPaint=new TextPaint();
 new Thread(this).start();
 }

 public RightView(Context context, AttributeSet attr){
 super(context,attr);
 init(context);
 mPaint = new Paint();
 mTextPaint=new TextPaint();
 new Thread(this).start();
 }

 public RightView(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
 init(context);
 mPaint = new Paint();
 mTextPaint=new TextPaint();
 new Thread(this).start();
 }

 private void init(Context context){
 dm= context.getResources().getDisplayMetrics();
 textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, textSize, dm);
 DEFAULT_VIEW_WIDTH=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_VIEW_WIDTH, dm);
 DEFAULT_VIEW_HEIGHT=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_VIEW_HEIGHT, dm);
 }


 @Override
 protected void onLayout(boolean changed, int left, int top, int right,
 int bottom) {
 // TODO Auto-generated method stub
 super.onLayout(changed, left, top, right, bottom);
 }

 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width  = measureDimension(DEFAULT_VIEW_WIDTH, widthMeasureSpec);
        height = measureDimension(DEFAULT_VIEW_HEIGHT, heightMeasureSpec);
        width1=width-(int)Math.ceil((double)(Math.sqrt(3)*height/3));
        setMeasuredDimension(width, height);                
    }
     
    protected int measureDimension( int defaultSize, int measureSpec ) {
         
        int result = defaultSize;
         
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
                 
        //1. layout给出了确定的值,比如:100dp
        //2. layout使用的是match_parent,但父控件的size已经可以确定了,比如设置的是具体的值或者match_parent
        if (specMode == MeasureSpec.EXACTLY) {      
            result = specSize; 
        } 
        //1. layout使用的是wrap_content
        //2. layout使用的是match_parent,但父控件使用的是确定的值或者wrap_content
        else if (specMode == MeasureSpec.AT_MOST) {             
            result = Math.min(defaultSize, specSize); //建议:result不能大于specSize
        } 
        //UNSPECIFIED,没有任何限制,所以可以设置任何大小
        //多半出现在自定义的父控件的情况下,期望由自控件自行决定大小
        else {      
            result = defaultSize; 
        }
         
        return result;
    }

 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 canvas.drawColor(Color.TRANSPARENT);
 mPaint.setAntiAlias(true);
 mPaint.setStyle(Paint.Style.FILL);
 Path path1 = new Path();
 path1.moveTo(0, 0);
 path1.lineTo(width1,0);
 path1.lineTo(width, height/2);
 path1.lineTo(width1, height);
 path1.lineTo(0, height);
 path1.lineTo(0, 0);
 path1.close();
 mPaint.setColor(Color.GRAY);
 canvas.drawPath(path1, mPaint);
 mTextPaint.setColor(Color.WHITE);
 mTextPaint.setTextSize(textSize);
 mTextPaint.setAntiAlias(true);
        int baseX = (int) (width1 / 2 - mTextPaint.measureText(text) / 2);
        int baseY = (int) ((height / 2) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2));
        canvas.drawText(text, baseX, baseY, mTextPaint);
 }

 @Override
 public void run() {
 while (!Thread.currentThread().isInterrupted()) {
 try {
 Thread.sleep(100);
 } catch (InterruptedException e) {
 Thread.currentThread().interrupt();
 }
 postInvalidate();
 }

 }

 public void setText(String text) {
 this.text = text;
 postInvalidate();

 }



}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值