網站首頁 編程語言 正文
近期公司要做報表功能,在網上搜索下表格的樣式后便自己寫了一個自定義的表格控件,該表格控件能根據設置的數據中數據的最大值自動設置左側信息欄顯示的值,使得條形圖能盡量的充滿控件,條形圖部分支持左右滑動,數據的長度可能超過控件本身所能容納的長度,所以在繪制的時候做了判斷,當需要繪制的部分不再控件范圍內則不進行繪制,具體請閱讀代碼,目前只支持一個名稱對應一條數據,如有不足之處,大家提出幫忙修改
使用方法如下:
在xml文件中定義控件屬性
? ?
在Activity中設置控件要顯示的數據、設置顯示的樣式
public class MainActivity extends AppCompatActivity { ? ? private MyChatView mMyChatView; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? mMyChatView = (MyChatView) this.findViewById(R.id.chatView); ? ? ? ? mMyChatView.setData(getData1()) ? ? ? ? ? ? ? ? .setDataCompany("美元") ? ? ? ? ? ? ? ? .setDataTitle("測試") ? ? ? ? ? ? ? ? .setLeftTextColor(Color.RED) ? ? ? ? ? ? ? ? .setBottomTextColor(Color.BLUE) ? ? ? ? ? ? ? ? .setmDataTopTextColor(Color.RED) ? ? ? ? ? ? ? ? ? ? ? ? .setDataBackgroundColor(Color.parseColor("#ABCDEF")) ? ? ? ? ? ? ? ? .setDataColor(Color.RED) ? ? ? ? ? ? ? ? .setTitleColor(Color.BLUE) ? ? ? ? ? ? ? ? .setLeftTextSize(12) ? ? ? ? ? ? ? ? .setBottomTextSize(15) ? ? ? ? ? ? ? ? .setDataTopTextSize(10) ? ? ? ? ? ? ? ? .setTitleTextSize(20) ? ? ? ? ? ? ? ? .setSpanBottomText(15); ? ? } ? ? /** ? ? ?* 獲取測試數據 ? ? ?* @return 測試用的數據 ? ? ?*/ ? ? private List
自定義控件代碼:
public class MyChatView extends View { ? ? /** 數據集合中的 Map 集合存放信息的鍵 */ ? ? public static final String NAME = "name"; ? ? /** 數據集合中的 Map 集合存放數據的鍵 */ ? ? public static final String VALUE = "value"; ? ? /** 上下文 */ ? ? private Context mContext; ? ? /** 控件的高度 */ ? ? private int mHeight; ? ? /** 控件的寬度 */ ? ? private int mWidget; ? ? /** 數據 */ ? ? private List> mData; ? ? /** 數據單位 */ ? ? private String mDataCompany = "單位: "; ? ? /** 底部表格名稱 */ ? ? private String mDataTitle = null; ? ? /** 底部信息欄文字的大小 */ ? ? private int mBottomTextSize; ? ? /** 左側等分信息欄文字的大小 */ ? ? private int mLeftTextSize; ? ? /** 柱狀圖頂部文字的大小 */ ? ? private int mDataTopTextSize; ? ? /** 表格標題文字大小 */ ? ? private int mTitleTextSize; ? ? /** 左側文字與數據區域的間隔 */ ? ? private int mSpanLeftText; ? ? /** 柱狀圖頂部文字與柱狀圖的間隔 */ ? ? int mSpanDataTopText; ? ? /** 底部信息字符串間隔 */ ? ? int mSpanBottomText; ? ? /** 底部信息字符串與控件底部間隔 */ ? ? int mSpanBottom; ? ? /** 繪制數據部分的背景顏色 */ ? ? private int mDataBackgroundColor = Color.WHITE; ? ? /** 底部信息字符串顏色 */ ? ? private int mBottomTextColor = Color.BLACK; ? ? /** 柱狀圖柱狀部分顏色 */ ? ? private int mDataColor = Color.BLACK; ? ? /** 左邊信息欄文字顏色 */ ? ? private int mLeftTextColor = Color.BLACK; ? ? /** 柱狀圖頂部文字顏色 */ ? ? private int mDataTopTextColor = Color.BLACK; ? ? /** 標題顏色 */ ? ? private int mTitleColor = Color.BLACK; ? ? /** 表格移動的位置 */ ? ? private int mChartMovedSize = 0; ? ? /** 用戶按下時 X 方向位置 */ ? ? private int mDownX = 0; ? ? /** 用戶松手是 X 方向位置 */ ? ? private int mUpX = 0; ? ? /** 表格 X 方向移動的最大距離 */ ? ? private int mChartMaxMovedLengthX; ? ? public MyChatView(Context context) { ? ? ? ? this(context, null); ? ? } ? ? public MyChatView(Context context, AttributeSet attrs) { ? ? ? ? this(context, attrs, 0); ? ? } ? ? public MyChatView(Context context, AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? ? ? this.mContext = context; ? ? ? ? this.mBottomTextSize = dpToPx(context, 15); ? ? ? ? this.mLeftTextSize = dpToPx(context, 10); ? ? ? ? this.mDataTopTextSize = dpToPx(context, 10); ? ? ? ? this.mSpanLeftText = dpToPx(context, 2); ? ? ? ? this.mSpanDataTopText = dpToPx(context, 3); ? ? ? ? this.mSpanBottomText = dpToPx(context, 10); ? ? ? ? this.mSpanBottom = dpToPx(context, 8); ? ? ? ? this.mTitleTextSize = dpToPx(context, 20); ? ? } ? ? @Override ? ? protected void onSizeChanged(int w, int h, int oldw, int oldh) { ? ? ? ? super.onSizeChanged(w, h, oldw, oldh); ? ? ? ? this.mHeight = h; ? ? ? ? this.mWidget = w; ? ? } ? ? @Override ? ? public boolean onTouchEvent(@NonNull MotionEvent event) { ? ? ? ? int action = event.getAction(); ? ? ? ? switch (action) { ? ? ? ? ? ? case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? ? ? mDownX = (int) event.getX(); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? ? ? mUpX = (int) event.getX(); ? ? ? ? ? ? ? ? shouldMoveChart(); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case MotionEvent.ACTION_UP: ? ? ? ? ? ? ? ? mUpX = (int) event.getX(); ? ? ? ? ? ? ? ? shouldMoveChart(); ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? return true; ? ? } ? ? /** ? ? ?* 判斷移動的距離大于規定距離就移動表格 ? ? ?*/ ? ? private void shouldMoveChart (){ ? ? ? ? if (mChartMaxMovedLengthX =size || (mDownX-mUpX)>=size ) { ? ? ? ? ? ? mChartMovedSize += (mUpX - mDownX); ? ? ? ? ? ? mDownX = mUpX; ? ? ? ? ? ? if (mChartMovedSize>=0) { ? ? ? ? ? ? ? ? mChartMovedSize = 0; ? ? ? ? ? ? } ? ? ? ? ? ? if (mChartMovedSize<-mChartMaxMovedLengthX) { ? ? ? ? ? ? ? ? mChartMovedSize = -mChartMaxMovedLengthX; ? ? ? ? ? ? } ? ? ? ? ? ? this.invalidate(); ? ? ? ? } ? ? } ? ? private InnerDraw innerDraw; ? ? @Override ? ? protected void onDraw(Canvas canvas) { ? ? ? ? super.onDraw(canvas); ? ? ? ? if (innerDraw==null) { ? ? ? ? ? ? innerDraw = new InnerDraw(canvas); ? ? ? ? } else { ? ? ? ? ? ? innerDraw.initData(canvas); ? ? ? ? } ? ? ? ? innerDraw.drawLeftMenue(); ? ? ? ? innerDraw.drawDataBackground(); ? ? ? ? innerDraw.drawDataTitle(); ? ? ? ? innerDraw.drawCompany(); ? ? ? ? int dataSize = mData.size(); ? ? ? ? for (int i=0; i maxLengthString.length()) { ? ? ? ? ? ? ? ? maxLengthString = tempString; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? if (maxLengthString.contains(".")) { ? ? ? ? ? ? maxLengthString = maxLengthString.substring(0, maxLengthString.indexOf('.')+2); ? ? ? ? } ? ? ? ? return maxLengthString; ? ? } ? ? /** ? ? ?* 獲取最長信息字符串 ? ? ?* @return 底部信息欄中最長字符串 ? ? ?*/ ? ? private String getBottomMaxLegthString (){ ? ? ? ? int size = mData.size(); ? ? ? ? String maxString = "你好我好大家好才是真的好"; ? ? ? ? String tempString; ? ? ? ? for (int i=0; i tempString.length()) { ? ? ? ? ? ? ? ? maxString = tempString; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return maxString; ? ? } ? ? /** ? ? ?* 獲取數據中的最大值 ? ? ?* @return 數據中的最大值 int float 類型 ? ? ?*/ ? ? private int getMaxValue (){ ? ? ? ? int maxValue = Float.valueOf(mData.get(0).get(VALUE)).intValue(); ? ? ? ? int tempValue; ? ? ? ? for (Map map : mData) { ? ? ? ? ? ? tempValue = Float.valueOf(map.get(VALUE)).intValue(); ? ? ? ? ? ? if (maxValue map : mData) { ? ? ? ? ? ? xTotalLength += getTextWidth(paint, map.get(NAME), mBottomTextSize); ? ? ? ? ? ? xTotalLength = ?xTotalLength + mSpanBottomText; ? ? ? ? } ? ? ? ? return xTotalLength; ? ? } ? ? /** ? ? ?* 根據數據中的最大值將數據分成10等分,每等分為10的倍數 ? ? ?* @param maxValue 數據中的最大值 ? ? ?* @return 左側等分欄的每一等分的數值 int 類型 ? ? ?*/ ? ? private int getPiceValue (int maxValue){ ? ? ? ? int piceValue = 1; ? ? ? ? while (maxValue>10) { ? ? ? ? ? ? maxValue = maxValue/10; ? ? ? ? ? ? piceValue = piceValue * 10; ? ? ? ? } ? ? ? ? if (maxValue<=5) { ? ? ? ? ? ? piceValue = piceValue / 2; ? ? ? ? } ? ? ? ? return piceValue; ? ? } ? ? /** ? ? ?* 根據手機的分辨率從 dp 的單位 轉成為 px(像素) ? ? ?* @param context 上下文 ? ? ?* @param dpValue 要轉換的 dp 值 ? ? ?* @return 轉換后的 px 值 ? ? ?*/ ? ? private int dpToPx(Context context, float dpValue) { ? ? ? ? final float scale = context.getResources().getDisplayMetrics().density; ? ? ? ? return (int) (dpValue * scale + 0.5f); ? ? } ? ? /** ? ? ?* 設置數據 ? ? ?* @param data 表中的數據 ? ? ?* ? ? ? ? ? ? data集合中的Map ? ? ?* ? ? ? ? ? ? "name"存放名稱,使用MyCharView中的常量 NAME ? ? ?* ? ? ? ? ? ? "value"存放數據,數據為int類型的字符串,使用MyCharView中的常量 VALUE ? ? ?*/ ? ? public MyChatView setData (List > data){ ? ? ? ? try { ? ? ? ? ? ? this.mData = data==null? new ArrayList >():data; ? ? ? ? }catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return this; ? ? } ? ? /** ? ? ?* 設置繪制數據部分背景顏色 ? ? ?* @param dataBackgroundColor 16進制 int 類型顏色 ? ? ?* @return 對象本身 ? ? ?*/ ? ? public MyChatView setDataBackgroundColor (int dataBackgroundColor){ ? ? ? ? try { ? ? ? ? ? ? this.mDataBackgroundColor = dataBackgroundColor; ? ? ? ? }catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return this; ? ? } ? ? /** ? ? ?* 設置左邊信息欄文字顏色 ? ? ?* @param leftTextColor 16進制 int 類型顏色 ? ? ?* @return 對象本身 ? ? ?*/ ? ? public MyChatView setLeftTextColor (int leftTextColor){ ? ? ? ? try { ? ? ? ? ? ? this.mLeftTextColor = leftTextColor; ? ? ? ? }catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return this; ? ? } ? ? /** ? ? ?* 設置底部信息文字顏色 ? ? ?* @param bottomTextColor 16進制 int 類型顏色 ? ? ?* @return 對象本身 ? ? ?*/ ? ? public MyChatView setBottomTextColor (int bottomTextColor){ ? ? ? ? try { ? ? ? ? ? ? this.mBottomTextColor = bottomTextColor; ? ? ? ? }catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return this; ? ? } ? ? /** ? ? ?* 設置柱狀條的背景顏色 ? ? ?* @param dataColor 16進制 int 類型顏色 ? ? ?* @return 對象本身 ? ? ?*/ ? ? public MyChatView setDataColor (int dataColor){ ? ? ? ? try { ? ? ? ? ? ? this.mDataColor = dataColor; ? ? ? ? }catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return this; ? ? } ? ? /** ? ? ?* 設置柱狀條頂部文字顏色 ? ? ?* @param dataTopTextColor 16進制 int 類型顏色 ? ? ?* @return 對象本身 ? ? ?*/ ? ? public MyChatView setmDataTopTextColor (int dataTopTextColor){ ? ? ? ? try { ? ? ? ? ? ? this.mDataTopTextColor = dataTopTextColor; ? ? ? ? }catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return this; ? ? } ? ? /** ? ? ?* 設置標題顏色 ? ? ?* @param titleColor 顏色16進制的 int 類型 ? ? ?* @return 對象本身 ? ? ?*/ ? ? public MyChatView setTitleColor (int titleColor){ ? ? ? ? try { ? ? ? ? ? ? this.mTitleColor = titleColor; ? ? ? ? }catch (Exception e){ ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return this; ? ? } ? ? /** ? ? ?* 設置底部信息文字大小 ? ? ?* @param bottomTextSize int 類型 dp ? ? ?* @return 對象本身 ? ? ?*/ ? ? public MyChatView setBottomTextSize (int bottomTextSize){ ? ? ? ? try { ? ? ? ? ? ? this.mBottomTextSize = dpToPx(mContext, bottomTextSize); ? ? ? ? }catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return this; ? ? } ? ? /** ? ? ?* 設置左側信息文字大小 ? ? ?* @param leftTextSize int 類型 dp ? ? ?* @return 對象本身 ? ? ?*/ ? ? public MyChatView setLeftTextSize (int leftTextSize){ ? ? ? ? try { ? ? ? ? this.mLeftTextSize = dpToPx(mContext, leftTextSize); ? ? }catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return this; ? ? } ? ? /** ? ? ?* 設置柱狀條頂部文字大小 ? ? ?* @param dataTopTextSize int 類型 dp ? ? ?* @return 對象本身 ? ? ?*/ ? ? public MyChatView setDataTopTextSize (int dataTopTextSize){ ? ? ? ? try { ? ? ? ? ? ? this.mDataTopTextSize = dpToPx(mContext, dataTopTextSize); ? ? ? ? }catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return this; ? ? } ? ? /** ? ? ?* 設置底部表格標題文字大小 ? ? ?* @param titleTextSize 標題文字大小 ? ? ?* @return 對象本身 ? ? ?*/ ? ? public MyChatView setTitleTextSize (int titleTextSize){ ? ? ? ? try { ? ? ? ? ? ? this.mTitleTextSize = dpToPx(mContext,titleTextSize); ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return this; ? ? } ? ? /** ? ? ?* 設置表格數據單位 ? ? ?* @param dataCompany 數據單位 ? ? ?* @return 對象本身 ? ? ?*/ ? ? public MyChatView setDataCompany (String dataCompany){ ? ? ? ? try { ? ? ? ? ? ? this.mDataCompany += dataCompany==null? "空":dataCompany; ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return this; ? ? } ? ? /** ? ? ?* 設置表格標題 ? ? ?* @param dataTitle 表格標題 ? ? ?* @return 對象本身 ? ? ?*/ ? ? public MyChatView setDataTitle (String dataTitle){ ? ? ? ? try { ? ? ? ? ? ? mDataTitle = dataTitle; ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return this; ? ? } ? ? /** ? ? ?* 設置底部信息欄文字的間隔 默認為 10dp ? ? ?* @param spanBottomText 間隔距離 dp ? ? ?* @return 對象本身 ? ? ?*/ ? ? public MyChatView setSpanBottomText (int spanBottomText){ ? ? ? ? try { ? ? ? ? ? ? this.mSpanBottomText = dpToPx(mContext, spanBottomText); ? ? ? ? } catch (Exception e){ ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return this; ? ? } ? ? //****************************** 繪制表格的類 ********************************** ? ? /** ? ? ?* 繪制控件界面的類 ? ? ?*/ ? ? private class InnerDraw{ ? ? ? ? private Canvas canvas; ? ? ? ? private ?Paint paint; ? ? ? ? /** 記錄繪制到的位置 */ ? ? ? ? private ?int bottomTextPainted; ? ? ? ? /** 底部標題高度 */ ? ? ? ? private ?int bottomTitleHeight; ? ? ? ? /** 底部信息的高度 */ ? ? ? ? private ?int bottomMessageHeight; ? ? ? ? /** 左側信息欄最長文字 */ ? ? ? ? private ?String leftValueMaxString; ? ? ? ? /** 表格中柱狀圖上文字高度 */ ? ? ? ? private ?int dataTopTextHeight; ? ? ? ? /** 左側信息欄寬度 */ ? ? ? ? private ?int leftValueWidth; ? ? ? ? /** 繪制數據部分的高度 */ ? ? ? ? private ?int chartDataHeight; ? ? ? ? /** 左側等分欄的每等分高度 */ ? ? ? ? private ?int piceValueHeight; ? ? ? ? /** 左側等分欄每等分的數值 */ ? ? ? ? private ?int piceValue; ? ? ? ? /** 柱狀的寬度 */ ? ? ? ? private ?int dataBarWidth; ? ? ? ? /** 底部信息 */ ? ? ? ? String bottomMessage; ? ? ? ? /** 底部信息的寬度 */ ? ? ? ? private int bottomMessageWidth; ? ? ? ? /** 數據信息 */ ? ? ? ? Map data; ? ? ? ? /** 要繪制信息的位置 */ ? ? ? ? int index; ? ? ? ? public InnerDraw (Canvas canvas){ ? ? ? ? ? ? initData(canvas); ? ? ? ? } ? ? ? ? /** ? ? ? ? ?* 初始化數據 ? ? ? ? ?*/ ? ? ? ? public void initData(Canvas canvas){ ? ? ? ? ? ? this.canvas = canvas; ? ? ? ? ? ? this.paint = new Paint(); ? ? ? ? ? ? this.bottomTitleHeight = mDataTitle==null? 0:(getTextHeight(paint, mTitleTextSize)+mSpanBottom/2); ? ? ? ? ? ? this.bottomTextPainted = 0; ? ? ? ? ? ? this.bottomMessageHeight = getTextHeight(paint, mBottomTextSize) + ?mSpanBottom + bottomTitleHeight; ? ? ? ? ? ? this.leftValueMaxString = getLeftValueMaxString(); ? ? ? ? ? ? this.dataTopTextHeight = getTextHeight(paint, mDataTopTextSize); ? ? ? ? ? ? this.leftValueWidth = getTextWidth(paint, leftValueMaxString , mLeftTextSize) + mSpanLeftText; ? ? ? ? ? ? this.chartDataHeight = mHeight - bottomMessageHeight - mSpanBottom; ? ? ? ? ? ? this.piceValueHeight = (chartDataHeight-dataTopTextHeight-getTextHeight(paint, mSpanDataTopText))/10; ? ? ? ? ? ? this.piceValue = getPiceValue(getMaxValue()); ? ? ? ? ? ? this.dataBarWidth = getTextWidth(paint, getBottomMaxLegthString(), mBottomTextSize); ? ? ? ? ? ? this.bottomMessage = ""; ? ? ? ? ? ? this.bottomMessageWidth = 0; ? ? ? ? ? ? this.data = new HashMap<>(); ? ? ? ? ? ? this.index = 0; ? ? ? ? } ? ? ? ? /** ? ? ? ? ?* 繪制左側等分欄 ? ? ? ? ?*/ ? ? ? ? public void drawLeftMenue(){ ? ? ? ? ? ? paint.setColor(mLeftTextColor); ? ? ? ? ? ? paint.setTextSize(mLeftTextSize); ? ? ? ? ? ? int textLeft; ? ? ? ? ? ? int textTop; ? ? ? ? ? ? String valueStr; ? ? ? ? ? ? int strLength; ? ? ? ? ? ? String maxValueStr = String.valueOf(piceValue * 10); ? ? ? ? ? ? int textMaxLength = maxValueStr.length(); ? ? ? ? ? ? int topTextHeight = getTextHeight(paint, mDataTopTextSize); ? ? ? ? ? ? int leftTextHeight = getTextHeight(paint, mLeftTextSize); ? ? ? ? ? ? for (int i=0; i<=10; i++) { ? ? ? ? ? ? ? ? textLeft = 0; ? ? ? ? ? ? ? ? valueStr = String.valueOf(i * piceValue); ? ? ? ? ? ? ? ? strLength = valueStr.length(); ? ? ? ? ? ? ? ? if (strLength data, int index){ ? ? ? ? ? ? this.data = data; ? ? ? ? ? ? this.bottomMessage = data.get(NAME).trim(); ? ? ? ? ? ? this.bottomMessageWidth = getTextWidth(paint, bottomMessage, mBottomTextSize); ? ? ? ? ? ? this.index = index; ? ? ? ? ? ? int bottomMsgBegainDrawX = (index + 1) * mSpanBottomText + mChartMovedSize + leftValueWidth + bottomTextPainted; ? ? ? ? ? ? if ((bottomMsgBegainDrawX+bottomMessageWidth) mWidget) {//需要繪制的區域超出了控件的右邊,結束繪制 ? ? ? ? ? ? ? ? return -1; ? ? ? ? ? ? } ? ? ? ? ? ? return 1; ? ? ? ? } ? ? ? ? /** ? ? ? ? ?* 結束繪制 ? ? ? ? ?*/ ? ? ? ? public void endDrawBody(){ ? ? ? ? ? ? bottomTextPainted += bottomMessageWidth; ? ? ? ? } ? ? ? ? /** ? ? ? ? ?* 繪制底部信息欄 ? ? ? ? ?*/ ? ? ? ? public void drawBottomMessage() { ? ? ? ? ? ? paint.setColor(mBottomTextColor); ? ? ? ? ? ? paint.setTextSize(mBottomTextSize); ? ? ? ? ? ? int bottomLeft = (index + 1) * mSpanBottomText + mChartMovedSize + leftValueWidth + bottomTextPainted; ? ? ? ? ? ? int bottomTop = chartDataHeight + bottomMessageHeight - bottomTitleHeight - dpToPx(mContext, 2); ? ? ? ? ? ? if (bottomLeft >= leftValueWidth && bottomLeft < mWidget) { ? ? ? ? ? ? ? ? canvas.drawText(bottomMessage, bottomLeft, bottomTop, paint); ? ? ? ? ? ? } else if ((bottomLeft+bottomMessageWidth)>leftValueWidth) { ? ? ? ? ? ? ? ? int index = (leftValueWidth-bottomLeft)*bottomMessage.length()/bottomMessageWidth+1; ? ? ? ? ? ? ? ? if (index>=0 && index leftValueWidth) { ? ? ? ? ? ? ? ? dataLeft = leftValueWidth; ? ? ? ? ? ? } ? ? ? ? ? ? Rect dataRect = new Rect(dataLeft, dataTop, dataRight, dataBottom); ? ? ? ? ? ? if (dataRight>leftValueWidth) { ? ? ? ? ? ? ? ? canvas.drawRect(dataRect, paint); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? /** ? ? ? ? ?* 繪制條形數據頂部文字 ? ? ? ? ?*/ ? ? ? ? public void drawDataTopText() { ? ? ? ? ? ? String topTextMessage = data.get(VALUE); ? ? ? ? ? ? int topTextWidth = getTextWidth(paint, topTextMessage, mDataTopTextSize); ? ? ? ? ? ? paint.setColor(mDataTopTextColor); ? ? ? ? ? ? paint.setTextSize(mDataTopTextSize); ? ? ? ? ? ? int bottomLeft = (index + 1) * mSpanBottomText + mChartMovedSize + leftValueWidth + bottomTextPainted; ? ? ? ? ? ? int topTextLeft = bottomLeft + (bottomMessageWidth - topTextWidth) / 2; ? ? ? ? ? ? int dataValue = Float.valueOf(data.get(VALUE)).intValue(); ? ? ? ? ? ? int dataTop = chartDataHeight - (dataValue*(chartDataHeight-dataTopTextHeight - mSpanDataTopText)/piceValue)/10; ? ? ? ? ? ? int topTextTop = dataTop - mSpanDataTopText * 2 / 3; ? ? ? ? ? ? if (topTextLeft >= leftValueWidth && bottomLeft < mWidget) { ? ? ? ? ? ? ? ? canvas.drawText(topTextMessage, topTextLeft, topTextTop, paint); ? ? ? ? ? ? } else if ((topTextLeft+topTextWidth)>leftValueWidth) { ? ? ? ? ? ? ? ? int index = (leftValueWidth-topTextLeft)*topTextMessage.length()/topTextWidth+1; ? ? ? ? ? ? ? ? if (index>=0 && index
實現效果如下圖
原文鏈接:https://blog.csdn.net/zhoujiulong90/article/details/51106658
相關推薦
- 2022-05-06 golang導入私有倉庫報錯:“server response: not found:xxx: in
- 2022-06-02 C++實現投骰子的隨機游戲_C 語言
- 2022-08-16 C#獲取Description特性的擴展類詳解_C#教程
- 2024-01-27 Linux關于Centos IP靜態配置
- 2022-12-21 使用RedisAtomicInteger計數出現少計問題及解決_Redis
- 2022-11-27 Git基礎學習之文件刪除操作命令詳解_相關技巧
- 2022-05-13 c++ remove_reference
- 2021-12-14 nginx.pid打開失敗以及失效的解決方案_nginx
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支