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

方法实现如下:


package com.example.mytest;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTextView = (TextView) findViewById(R.id.test);

        Thread thread = new Thread() {
            @Override
            public void run() {
                Drawable mPointMenuBmp = initCounterResources(3);
                Message message = handler.obtainMessage();
                message.what = 1;
                message.obj = mPointMenuBmp;
                handler.sendMessage(message);
                super.run();
            }

        };

        thread.start();

    }

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            Drawable mPointMenuBmp = (Drawable) msg.obj;
            mTextView.setCompoundDrawablesWithIntrinsicBounds(null, null, mPointMenuBmp, null);
            super.handleMessage(msg);
        }

    };

    /** <br>功能简述:画出小红点
     * <br>功能详细描述:
     * <br>注意:
     * @param count
     */
    private Drawable initCounterResources(int count) {
        Drawable mCounterDrawable = null;
        Bitmap bitmapDrawable = null;

        if (mCounterDrawable == null) {
            // 初始化画布
            mCounterDrawable = getResources().getDrawable(R.drawable.go_account_stat_notify);
            Bitmap bitmapDrawables = ((BitmapDrawable) mCounterDrawable).getBitmap();
            int bitmapX = bitmapDrawables.getWidth();
            int bitmapY = bitmapDrawables.getHeight();
            bitmapDrawable = Bitmap.createBitmap(bitmapX, bitmapY, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmapDrawable);

            // 拷贝图片
            Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setDither(true);// 防抖动
            mPaint.setFilterBitmap(true);// 用来对Bitmap进行滤波处理,这样,当你选择Drawable时,会有抗锯
            Rect src = new Rect(0, 0, bitmapX, bitmapX);
            Rect dst = new Rect(0, 0, bitmapX, bitmapX);
            canvas.drawBitmap(((BitmapDrawable) mCounterDrawable).getBitmap(), src, dst, mPaint);

            //            canvas.drawBitmap(bitmapDrawable, bitmapX, bitmapY, mPaint);
            //画数字
            mPaint.setTextAlign(Paint.Align.CENTER);
            mPaint.setTypeface(Typeface.DEFAULT_BOLD);
            mPaint.setColor(Color.BLACK);
            mPaint.setTextSize(20);
            FontMetrics fontMetrics = mPaint.getFontMetrics();

            canvas.drawText(String.valueOf(count), 56, 56 + (fontMetrics.bottom - fontMetrics.top) / 4, mPaint);
            canvas.save();
        }
        return new BitmapDrawable(getResources(), bitmapDrawable);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值