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

       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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值