博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
0-9数字图片显示时间
阅读量:5829 次
发布时间:2019-06-18

本文共 6296 字,大约阅读时间需要 20 分钟。

效果图

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:

,,,,,,,,,,,

转载地址:http://vildx.baihongyu.com/

你可能感兴趣的文章
HTML5新手入门指南
查看>>
opennebula 开发记录
查看>>
ubuntu 修改hostname
查看>>
sql 内联,左联,右联,全联
查看>>
C++关于字符串的处理
查看>>
6、Web Service-拦截器
查看>>
Flask 源码流程,上下文管理
查看>>
stream classdesc serialVersionUID = -7218828885279815404, local class serialVersionUID = 1.
查看>>
ZAB与Paxos算法的联系与区别
查看>>
Breaking parallel loops in .NET C# using the Stop method z
查看>>
修改故障转移群集心跳时间
查看>>
[轉]redis;mongodb;memcache三者的性能比較
查看>>
微软职位内部推荐-Sr DEV
查看>>
让你的WPF程序在Win7下呈现Win8风格主题
查看>>
802.11 学习笔记
查看>>
Leetcode-Database-176-Second Highest Salary-Easy(转)
查看>>
构建Docker Compose服务堆栈
查看>>
最小角回归 LARS算法包的用法以及模型参数的选择(R语言 )
查看>>
Hadoop生态圈-Kafka常用命令总结
查看>>
如何基于Redis Replication设计并实现Redis-replicator?
查看>>