Android右箭头的显示文字的View

来源:互联网 发布:幽灵行动荒野优化补丁 编辑:程序博客网 时间:2024/04/29 19:57
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(); }}
0 0
原创粉丝点击