效果图
xml:目前只能指定大小,不然显示不了
复制代码
ImageNumberView.java复制代码
package android.car.app.radio;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.View;public class ImageNumberView extends View { private static final int DOT = 10; private static final int NEGATIVE = 11; private static final int CENTIGRADE = 12; private static final int FAHRENHEIT = 13; private static final int IMG_COUNT = 14; private static final int MAX_NUMBER_LENGTH = 20; private static final int SPACING_BETWEEN_IMAGES = 2; protected Paint mPaint = new Paint(); // The image draw list (convert from String number) protected int[] mDrawList = new int[MAX_NUMBER_LENGTH]; protected int mDrawCount = 0; protected String mShowNumber; protected Bitmap[] mBitmaps = new Bitmap[IMG_COUNT]; protected int mDrawStartX; protected int mDrawPosY; protected int mTotalNumberWidth; protected int mTotalNumberHeigth; public ImageNumberView(Context context) { super(context); } public ImageNumberView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ImageNumberView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } void setImageResList(int[] ress) { Drawable drawable; for (int i = 0; i < ress.length && i < mBitmaps.length; ++i) { drawable = getResources().getDrawable(ress[i]); if (drawable != null) { mBitmaps[i] = ((BitmapDrawable) drawable).getBitmap(); } } } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); calcDrawPos(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = measureSize(widthMeasureSpec, getSuggestedMinimumWidth()); int height = measureSize(heightMeasureSpec, getSuggestedMinimumHeight()); setMeasuredDimension(width, height); } private int measureSize(int measureSpec, int minSize) { int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); // Default size if no limits are specified. int result = minSize; switch (specMode) { case MeasureSpec.AT_MOST: result = Math.min(result, specSize); break; case MeasureSpec.EXACTLY: result = specSize; break; } return result; }// public String getNumber(){// return mShowNumber;// } public void setNumber(String number) { if (mShowNumber != null && number.equals(mShowNumber)) { return; } mShowNumber = number; if (mShowNumber == null) { mDrawCount = 0; invalidate(); return; } char c; int index; mDrawCount = mShowNumber.length(); if (mDrawCount > mDrawList.length) { mDrawList = new int[mDrawCount]; } //图片间距比图片个数少一个 mTotalNumberWidth = -SPACING_BETWEEN_IMAGES; mTotalNumberHeigth = 0; for (int i = 0; i < mDrawCount; i++) { c = mShowNumber.charAt(i); index = getCharImageIndex(c); mDrawList[i] = index; if (index != -1 && index < IMG_COUNT && mBitmaps[index] != null) { mTotalNumberWidth += mBitmaps[index].getWidth() + SPACING_BETWEEN_IMAGES; mTotalNumberHeigth = Math.max(mTotalNumberHeigth, mBitmaps[index].getHeight()); } } if (!calcDrawPos()) { invalidate(); } } private int getCharImageIndex(char c) { switch (c) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; case '.': return DOT; case '-': return NEGATIVE; case 'C': return CENTIGRADE; case 'F': return FAHRENHEIT; default: return -1; } } private boolean calcDrawPos() { int drawPosY = (getHeight() - mTotalNumberHeigth) / 2; int drawPosX = (getWidth() - mTotalNumberWidth) / 2; if (drawPosX != mDrawStartX || drawPosY != mDrawPosY) { mDrawStartX = drawPosX; mDrawPosY = drawPosY; invalidate(); return true; } return false; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); float x = 0; Bitmap curBmp; int curIndex; mPaint.setAntiAlias(true); for (int i = 0; i < mDrawCount; i++) { curIndex = mDrawList[i]; if (curIndex >= IMG_COUNT || curIndex < 0) { continue; } curBmp = mBitmaps[curIndex]; if (curBmp != null) { canvas.drawBitmap(curBmp, x + mDrawStartX, mDrawPosY + mTotalNumberHeigth - curBmp.getHeight(), mPaint); x += curBmp.getWidth() + SPACING_BETWEEN_IMAGES; } } } void test() { }}复制代码
使用:在Actvity中
private final int[] freqImageList= { R.drawable.fm_digital_0, R.drawable.fm_digital_1, R.drawable.fm_digital_2, R.drawable.fm_digital_3, R.drawable.fm_digital_4, R.drawable.fm_digital_5, R.drawable.fm_digital_6, R.drawable.fm_digital_7, R.drawable.fm_digital_8, R.drawable.fm_digital_9, R.drawable.fm_dot};protected void onCreate(@Nullable Bundle savedInstanceState) { mManFreqView.setImageResList(freqImageList);}//然后在指定的地方调用set方法显示你需要的数组即可:mManFreqView.setNumber(str);//str:需要显示的字符串复制代码
资源1:
,,,,,,,,,,,