图片分割,bitmap处理

本文介绍了Android中Bitmap对象的使用,包括利用createBitmap进行图片切割,zoomImage方法实现图片的缩放,以及图片的旋转、透明度调整、倒影生成和圆角处理等操作。同时提到了如何将Drawable转换为Bitmap,并展示了如何通过ColorMatrix实现图片的灰度过滤。

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

此文章摘自不同博主内容混合总结部分以测试过:

这里的切割,主要使用的是Bitmap对象的createBitmap方法。

List<ImagePiece> pieces = new ArrayList<ImagePiece>(xPiece * yPiece);  

Bitmap bitmap = ;

        int bitmapW = bitmap.getWidth();  
        int bitmapH = bitmap.getHeight();  
        int pieceWidth = width / 3;  
        int pieceHeight = height / 3;  
        for (int i = 0; i < yPiece; i++) {  
            for (int j = 0; j < xPiece; j++) {  

Bitmap imgBitmap = Bitmap.createBitmap(bitmap, i % 2 *bitmapW, i / 2 * bitmapH, i % 2 *bitmapW + 

bitmapW, i / 2 * bitmapH + bitmapH);

                pieces.add(imgBitmap);  
            }  

        }  

    
    
    public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) {  
        Bitmap bitmap;  
        try {  
            bitmap = Bitmap.createBitmap(source, x, y, width, height, m, filter);  
        } catch (OutOfMemoryError localOutOfMemoryError) {  
            gc();  
            bitmap = Bitmap.createBitmap(source, x, y, width, height, m, filter);  
        }  
        return bitmap;  
    }  

返回一个不可变的源位图的位图的子集,改变了可选的矩阵。新的位图可能与源相同的对象,或可能是一个副本。

它初始化与原始位图的密度。如果源位图是不可变的,请求的子集是一样的源位图本身,然后返回源位图,没有新的位图创建。

sourceBitmap: The bitmap we are subsetting  产生子位图的源位图;
x int: The x coordinate of the first pixel in source 子位图第一个像素在源位图的X坐标
y int: The y coordinate of the first pixel in source 子位图第一个像素在源位图的y坐标
width int: The number of pixels in each row 子位图每一行的像素个数
height int: The number of rows 子位图的行数
m Matrix: Optional matrix to be applied to the pixels 对像素值进行变换的可选矩阵
filter boolean: true if the source should be filtered. Only applies if the matrix contains more than just translation. 如果为true,源图要被过滤。

该参数仅在matrix包含了超过一个翻转才有效

----------------------------------------------------------------------------------------------

图片处理

public Bitmap zoomImage(Bitmap bgimage, int newWidth, int newHeight) { 
// 获取这个图片的宽和高  
int width = bgimage.getWidth(); 
int height = bgimage.getHeight(); 
// 创建操作图片用的matrix对象  
Matrix matrix = new Matrix(); 
// 计算缩放率,新尺寸除原始尺寸  
float scaleWidth = ((float) newWidth) / width; 
float scaleHeight = ((float) newHeight) / height; 
// 缩放图片动作  
matrix.postScale(scaleWidth, scaleHeight); 
Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height, 
matrix, true); 
return bitmap; 

.绘制带有边框的文字,一般在游戏中起文字的美化作用

@param strMsg:绘制内容     @param g :画布     @param paint :画笔

@param setx:X轴起始坐标 @param sety:Y轴的起始坐标

@param fg:前景色@param bg:背景色
public void drawText(String strMsg, Canvas g, Paint paint, int setx,
int sety, int fg, int bg) {
paint.setColor(bg);
g.drawText(strMsg, setx + 1, sety, paint);
g.drawText(strMsg, setx, sety – 1, paint);
g.drawText(strMsg, setx, sety + 1, paint);
g.drawText(strMsg, setx – 1, sety, paint);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值