把一张图缩小放到另一张背景图上。

本文提供了两种实用的图片处理方法:一种是将图片缩放到指定尺寸,通过计算缩放比例并使用Matrix进行缩放;另一种是在一张图片上叠加另一张图片作为图标,并将其置于背景图之上。这些方法对于开发中处理图片的需求非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

 

   /**
     *  bitmap 图片缩放到指定大小
     */
    public static Bitmap resizeImage(Bitmap bitmap, int w, int h)
    {
        Bitmap BitmapOrg = bitmap;
        int width = BitmapOrg.getWidth();
        int height = BitmapOrg.getHeight();
        int newWidth = w;
        int newHeight = h;
        
        float scaleWidth = ((float)newWidth) / width;
        float scaleHeight = ((float)newHeight) / height;
        
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // if you want to rotate the Bitmap
        // matrix.postRotate(45);
        Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width, height, matrix, true);
        return resizedBitmap;
    }

   /**
     * 把一张图放到另一张背景图上。
     */
public static Drawable addbackground4onlyicon(Bitmap b1, Bitmap b2,Context mContext)
    {
        if (!b1.isMutable())
        {
            // 设置图片为背景为透明
            b1 = b1.copy(Bitmap.Config.ARGB_8888, true);
        }
        Paint paint = new Paint();
        Canvas canvas = new Canvas(b1);
        
        
        
        canvas.drawBitmap(b2, 17.5f, 17.5f, paint);// 叠加新图b2 (120-85)/2= 17.5
        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();
        return  new BitmapDrawable(mContext.getResources(), b1);
    }

 

转载于:https://www.cnblogs.com/lipeineng/p/6381491.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值