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
- Android右箭头的显示文字的View
- Android用View实现显示中英文混排的文字
- Android自定义可以在文字上方显示备注的View
- Android编程之仿微信显示更多文字的View
- Android编程之仿微信显示更多文字的View
- Android开发之仿微信显示更多文字的View
- 一个用粒子动画显示文字的 Android 自定义 View
- combobox和listbox的文字可以右对齐显示
- android 带箭头的textview文字伸缩效果
- popwindow在View的上,下,左,右 显示
- 【Tech-Android-View】巧设置ExpandableListView右边的箭头
- Android 文字显示的研究
- UILabel 文字靠右显示 并且 文字尾部距UILabel有一定的距离,使用attributeString
- TextView右边箭头的显示
- Android右划喜欢左划不喜欢的View
- 自定义View 带箭头的圆旋转
- android 让多余的文字不显示
- Android的ImageButton不能显示文字
- 洛谷 LUOGU P1629 邮递员送信 最短路问题
- 继承:父类的构建器有自变量,子类的构建器必须调用父类的构建器super(i)
- LeetCode-20. Valid Parentheses
- 读书笔记 effective c++ Item 16 成对使用new和delete时要用相同的形式
- 历届试题 最大子阵
- Android右箭头的显示文字的View
- PHP中如何获取多个checkbox的值
- 书评:硅谷之谜
- npm scripts 使用指南
- scala 排序算法 堆排序
- SQLSERVER配置的一些问题
- POJ 2533 Longest Ordered Subsequence(基础dp)
- 常见的标志寄存器FLAG的标志位
- (转)android事件一些原理知识(事件对象,事件实体,事件监听器,回调)