Android:将数字画在图片上合成一张图的两种实现方法(一)

来源:互联网 发布:数据课程设计设计思路 编辑:程序博客网 时间:2024/04/28 13:52
       1  Bitmap,可以来自资源/文件,也可以在程序中创建,实际上的功能相当于图片的存储空间;

       2 Canvas,紧密与Bitmap联系,把Bitmap比喻内容的话,那么Canvas就是提供了众多方法操作Bitamp的平台;

       Paint,与Canvas紧密联系,是"画板"上的笔刷工具,也用于设置View控件上的样式;

       Drawable,如果说前三者是看不见地在内存中画图(虚拟的),那么Drawable就是把前三者绘图结果表现出来的接口(真实的)。

              Drawable多个子类,例如:位图(BitmapDrawable)、图形(ShapeDrawable)、图层(LayerDrawable)等。


<span style="font-size:14px;"> private Drawable initCounterResources(int count) {        Drawable mCounterDrawable = null;        Paint mPaint = null;        if (mCounterDrawable == null) {            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);            mCounterDrawable = mActivity.getResources().getDrawable(R.drawable.notify);            mPaint.setTextAlign(Paint.Align.CENTER);            mPaint.setTypeface(Typeface.DEFAULT_BOLD);            mPaint.setColor(Color.WHITE);        }        if (count < 10) {            mPaint.setTextSize(DrawUtils.sp2px(16));        } else {            mPaint.setTextSize(DrawUtils.sp2px(12));        }        FontMetrics fontMetrics = mPaint.getFontMetrics();        Bitmap bitmapDrawable = ((BitmapDrawable) mCounterDrawable).getBitmap().copy(Bitmap.Config.ARGB_8888, true);        int bitmapX = bitmapDrawable.getWidth();        int bitmapY = bitmapDrawable.getHeight();        Canvas canvas = new Canvas(bitmapDrawable);        canvas.drawText(String.valueOf(count), bitmapX / 2, bitmapY / 2 + (fontMetrics.bottom - fontMetrics.top) / 4,                mPaint);        canvas.save();        return new BitmapDrawable(mActivity.getResources(), bitmapDrawable);    }</span>



调用


                 

<span style="font-size:14px;"> mPointMenuBmp = initCounterResources(mMessageCount); numText.setCompoundDrawablesWithIntrinsicBounds(null, null, mPointMenuBmp, null); numText.setVisibility(View.VISIBLE);</span>


0 0
原创粉丝点击