Android自定义控件,优化TextView性能。

Android的TextView控件的渲染效率是比较低的,并且有多个View以及一个View里面显示多个文本的话,UI性能是非常低的,比如下面这张图片:
这里写图片描述

所以我自定义了一个KeyValueTextView,源码如下:
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.gci.nutil.ViewUtil;
import com.serenegiant.usbcameratest4.R;

/**
* Created by hjh on 2018/6/26.
*/

public class KeyValueTextView extends View {
private String mKey;
private String mValue;
private int mSize;
private int mKeyColor = Color.WHITE;
private int mValueColor = 0xff5ffff1;
private int mBackGround = 0xff222c42;

private Paint mKeyPaint;
private Paint mValuePaint;

public void setmKey(String mKey) {
    this.mKey = mKey;
}

public void setmValue(String mValue) {
    this.mValue = mValue;
    invalidate();
}

public void setmBackGround(int mBackGround) {
    this.mBackGround = mBackGround;
    invalidate();
}

public KeyValueTextView(Context context) {
    super(context);
}

public KeyValueTextView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.KeyValueTextView);
    if (typedArray != null) {
        mSize = typedArray.getInteger(R.styleable.KeyValueTextView_textSize, (int) ViewUtil.sp2px(getResources(),25));
        mKeyColor = typedArray.getColor(R.styleable.KeyValueTextView_key_color, mKeyColor);
        mValueColor = typedArray.getColor(R.styleable.KeyValueTextView_value_color,mValueColor);
        mKey = typedArray.getString(R.styleable.KeyValueTextView_key);
        mBackGround = typedArray.getColor(R.styleable.KeyValueTextView_bg,mBackGround);
        typedArray.recycle();
    }
    mKeyPaint = new Paint();
    mValuePaint = new Paint();
    mKeyPaint.setColor(mKeyColor);
    mKeyPaint.setTextSize(ViewUtil.sp2px(getResources(),mSize));
    mValuePaint.setColor(mValueColor);
    mValuePaint.setTextSize(ViewUtil.sp2px(getResources(),mSize));
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Paint.FontMetrics fm = mKeyPaint.getFontMetrics();
    setMeasuredDimension(widthMeasureSpec,
            MeasureSpec.makeMeasureSpec((int) (getPaddingTop() + getPaddingBottom() + fm.descent - fm.ascent),MeasureSpec.EXACTLY));
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
}

@Override
protected void onDraw(Canvas canvas) {
    Paint.FontMetrics fm = mKeyPaint.getFontMetrics();
    canvas.drawColor(mBackGround);
    if(mKey == null) return;
    canvas.drawText(mKey,getPaddingLeft(),getPaddingTop() - fm.ascent,mKeyPaint);
    if(mValue == null) return;
    Rect rect = new Rect();
    mValuePaint.getTextBounds(mValue,0,mValue.length(),rect);
    canvas.drawText(mValue,canvas.getWidth() - getPaddingRight() - rect.width(),getPaddingTop() - fm.ascent,mValuePaint);
}

}

猜你喜欢

转载自blog.csdn.net/jiayouwangqiuwangzi/article/details/80824396