統計圖,圖文組合控制元件
阿新 • • 發佈:2018-11-12
效果圖:
package com.logistics.widgets; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.util.Log; import android.view.View; import com.usung.gxzy_logistics.R; /** * Descriptions:顏色方框和文字,用於統計圖顏色標註 * Created by fenghui on 2018/9/25. */ public class CustomStatisticsColor extends View { private int width; // 整個控制元件的寬 private int height; // 整個控制元件的高 private float tagWidth = dpToPx(15); // tag寬 private float tagHeight = dpToPx(15); // tag高 private float offsetSpacing = dpToPx(5); // 文字和前部分標記之間的間距 private Paint paint; private Paint paintText; private int tagColor = 0xff315178; // 標記顏色(前半部分顏色) private int textColor = 0xff00ffcd; // 標記顏色(前半部分顏色) private RectF rectF; // 矩形 用於繪製矩形 private Rect rect; // 用於測量文字 private String TAG = "fenghui"; private String text = "顏色"; public CustomStatisticsColor(Context context) { super(context); init(); } public CustomStatisticsColor(Context context, @Nullable AttributeSet attrs) { super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomStatisticsColor); tagColor = typedArray.getColor(R.styleable.CustomStatisticsColor_tagColor, tagColor); textColor = typedArray.getColor(R.styleable.CustomStatisticsColor_tagTextColor, getResources().getColor(R.color.blue)); text = typedArray.getString(R.styleable.CustomStatisticsColor_tagText); tagWidth = typedArray.getDimensionPixelOffset(R.styleable.CustomStatisticsColor_tagWidth, 15); tagHeight = typedArray.getDimensionPixelOffset(R.styleable.CustomStatisticsColor_tagHeight, 15); offsetSpacing = typedArray.getDimensionPixelOffset(R.styleable.CustomStatisticsColor_offsetSpacing, 5); typedArray.recycle(); init(); } void init(){ paint = new Paint(); paint.setColor(tagColor); paint.setStyle(Paint.Style.FILL); paint.setAntiAlias(true); paintText = new Paint(); paintText.setColor(textColor); paintText.setStyle(Paint.Style.FILL); paintText.setTextSize(spToPx(12)); paintText.setAntiAlias(true); rect = new Rect(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 獲取 int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); int sizeHeight= MeasureSpec.getSize(heightMeasureSpec); int mode = MeasureSpec.getMode(widthMeasureSpec); paintText.getTextBounds(text, 0, text.length(), rect); if (mode == MeasureSpec.EXACTLY) { width = sizeWidth; height = sizeHeight; } else { width = (int) (tagWidth + offsetSpacing + rect.width() + rect.right); if (tagHeight > rect.height()){ height = (int) tagHeight; }else{ height = rect.height(); } } Log.i(TAG, "width========= " + width + "\n" + "height========= " + height); setMeasuredDimension(width, height); rectF = new RectF(0, 0, tagWidth, tagHeight); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawRect(rectF, paint); // canvas.drawText(text,offsetSpacing + rectF.width(), tagHeight/2 + rect.height()/2 -rect.bottom, paintText); canvas.drawText(text,tagWidth + offsetSpacing, tagHeight/2 + rect.height()/2 -rect.bottom, paintText); } /** * dp轉化成為px * * @param dp * @return */ private int dpToPx(float dp) { float density = getContext().getResources().getDisplayMetrics().density; return (int) (dp * density + 0.5f * (dp >= 0 ? 1 : -1)); } /** * sp轉化為px * * @param sp * @return */ private int spToPx(int sp) { float scaledDensity = getContext().getResources().getDisplayMetrics().scaledDensity; return (int) (scaledDensity * sp + 0.5f * (sp >= 0 ? 1 : -1)); } }