Android BitmapUtils工具类,Android校招面试经验汇总

paint.setStrokeWidth(edge);

canvas.drawCircle(diameter / 2, diameter / 2, diameter / 2, paint);

}

return target;

}

/**

  • 圆角图片

  • @param bitmap 位图

  • @param rx x方向上的圆角半径

  • @param ry y方向上的圆角半径

  • @param bl 是否需要描边

  • @param bl 画笔粗细

  • @param bl 颜色代码

  • @return bitmap

*/

public static Bitmap createCornerBitmap(Bitmap bitmap,int rx,int ry,boolean bl,int edge,int color) {

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

Canvas canvas = new Canvas(output);//创建画布

Paint paint = new Paint();

paint.setAntiAlias(true);

Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

RectF rectF = new RectF(rect);

canvas.drawRoundRect(rectF, rx, ry, paint);//绘制圆角矩形

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));//取相交裁剪

canvas.drawBitmap(bitmap, rect, rect, paint);

if(bl) {

if (color == 0) color = 0xFFFEA248;//默认橘黄色

paint.setColor(color);

paint.setColor(color);

paint.setStyle(Paint.Style.STROKE);//描边

paint.setStrokeWidth(edge);

canvas.drawRoundRect(rectF, rx, ry, paint);

}

return output;

}

/**

  • 按比例裁剪图片

  • @param bitmap 位图

  • @param wScale 裁剪宽 0~100%

  • @param hScale 裁剪高 0~100%

  • @return bitmap

*/

public static Bitmap cropBitmap(Bitmap bitmap, float wScale, float hScale) {

int w = bitmap.getWidth();

int h = bitmap.getHeight();

int wh = (int) (w * wScale);

int hw = (int) (h * hScale);

int retX = (int) (w * (1 - wScale) / 2);

int retY = (int) (h * (1 - hScale) / 2);

return Bitmap.createBitmap(bitmap, retX, retY, wh, hw, null, false);

}

/**

  • 获得带倒影的图片方法

  • @param bitmap 位图

  • @param region 倒影区域 0.1~1

  • @return bitmap

*/

public static Bitmap createReflectionBitmap(Bitmap bitmap,float region) {

int width = bitmap.getWidth();

int height = bitmap.getHeight();

Matrix matrix = new Matrix();

matrix.preScale(1, -1);//镜像缩放

Bitmap reflectionBitmap = Bitmap.createBitmap(

bitmap,0

, (int)(height*(1-region))//从哪个点开始绘制

, width

,(int) (height*region)//绘制多高

, matrix, false);

Bitmap reflectionWithBitmap = Bitmap.createBitmap(width,height+ (int) (height*region),

Config.ARGB_8888);

Canvas canvas = new Canvas(reflectionWithBitmap);

canvas.drawBitmap(bitmap, 0, 0, null);

canvas.drawBitmap(reflectionBitmap, 0, height , null);

LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,

reflectionWithBitmap.getHeight()

, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);

Paint paint = new Paint();

paint.setShader(shader);

paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));//取两层绘制交集。显示下层。

canvas.drawRect(0, height, width, reflectionWithBitmap.getHeight() , paint);

return reflectionWithBitmap;

}

/**

  • 图片质量压缩

  • @param bitmap

  • @param many 百分比

  • @return

*/

public static Bitmap compressBitmap(Bitmap bitmap, float many){

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.JPEG, (int)many*100, baos);

ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());

return BitmapFactory.decodeStream(isBm, null, null);

}

/**

  • 高级图片质量压缩

*@param bitmap 位图

  • @param maxSize 压缩后的大小,单位kb

*/

public static Bitmap imageZoom(Bitmap bitmap, double maxSize) {

// 将bitmap放至数组中,意在获得bitmap的大小(与实际读取的原文件要大)

ByteArrayOutputStream baos = new ByteArrayOutputStream();

// 格式、质量、输出流

bitmap.compress(Bitmap.CompressFormat.PNG, 70, baos);

byte[] b = baos.toByteArray();

// 将字节换成KB

double mid = b.length / 1024;

// 获取bitmap大小 是允许最大大小的多少倍

double i = mid / maxSize;

// 判断bitmap占用空间是否大于允许最大空间 如果大于则压缩 小于则不压缩

doRecycledIfNot(bitmap);

if (i > 1) {

// 缩放图片 此处用到平方根 将宽带和高度压缩掉对应的平方根倍

// (保持宽高不变,缩放后也达到了最大占用空间的大小)

return scaleWithWH(bitmap,bitmap.getWidth() / Math.sqrt(i),

bitmap.getHeight() / Math.sqrt(i));

}

return null;

}

/***

  • 图片缩放

*@param bitmap 位图

  • @param w 新的宽度

  • @param h 新的高度

  • @return Bitmap

*/

public static Bitmap scaleWithWH(Bitmap bitmap, double w, double h) {

if (w == 0 || h == 0 || bitmap == null) {

return bitmap;

} else {

int width = bitmap.getWidth();

int height = bitmap.getHeight();

Matrix matrix = new Matrix();

float scaleWidth = (float) (w / width);

float scaleHeight = (float) (h / height);

matrix.postScale(scaleWidth, scaleHeight);

return Bitmap.createBitmap(bitmap, 0, 0, width, height,

matrix, true);

}

}

/**

  • YUV视频流格式转bitmap

  • @param data YUV视频流格式

  • @return width 设置宽度

  • @return width 设置高度

*/

public static Bitmap getBitmap(byte[] data, int width, int height) {

Bitmap bitmap;

YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21, width, height, null);

//data是onPreviewFrame参数提供

ByteArrayOutputStream baos = new ByteArrayOutputStream();

yuvimage.compressToJpeg(new Rect(0, 0, yuvimage.getWidth(), yuvimage.getHeight()), 100, baos);//

// 80–JPG图片的质量[0-100],100最高

byte[] rawImage = baos.toByteArray();

BitmapFactory.Options options = new BitmapFactory.Options();

SoftReference softRef = new SoftReference(BitmapFactory.decodeByteArray(rawImage, 0, rawImage

.length, options));

bitmap = softRef.get();

return bitmap;

}

/**

  • 图片资源文件转bitmap

  • @param file 图片的绝对路径

  • @return bitmap

*/

public static Bitmap getBitmapResources(Context context,int resId){

return BitmapFactory.decodeResource(context.getResources(),resId);

}

/**

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

作者2013年从java开发,转做Android开发,在小厂待过,也去过华为,OPPO等大厂待过,18年四月份进了阿里一直到现在。

参与过不少面试,也当面试官 面试过很多人。深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长,而且极易碰到天花板技术停滞不前!

我整理了一份阿里P7级别的最系统的Android开发主流技术,特别适合有3-5年以上经验的小伙伴深入学习提升。

主要包括阿里,以及字节跳动,腾讯,华为,小米,等一线互联网公司主流架构技术。如果你想深入系统学习Android开发,成为一名合格的高级工程师,可以收藏一下这些Android进阶技术选型

我搜集整理过这几年阿里,以及腾讯,字节跳动,华为,小米等公司的面试题,把面试的要求和技术点梳理成一份大而全的“ Android架构师”面试 Xmind(实际上比预期多花了不少精力),包含知识脉络 + 分支细节。

Java语言与原理;
大厂,小厂。Android面试先看你熟不熟悉Java语言

高级UI与自定义view;
自定义view,Android开发的基本功。

性能调优;
数据结构算法,设计模式。都是这里面的关键基础和重点需要熟练的。

NDK开发;
未来的方向,高薪必会。

前沿技术;
组件化,热升级,热修复,框架设计

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

我在搭建这些技术框架的时候,还整理了系统的高级进阶教程,会比自己碎片化学习效果强太多,GitHub可见;《Android架构视频+学习笔记》

当然,想要深入学习并掌握这些能力,并不简单。关于如何学习,做程序员这一行什么工作强度大家都懂,但是不管工作多忙,每周也要雷打不动的抽出 2 小时用来学习。

发;**
未来的方向,高薪必会。

[外链图片转存中…(img-3JJGQFYf-1711132235128)]

前沿技术;
组件化,热升级,热修复,框架设计

[外链图片转存中…(img-O0LYBWCf-1711132235129)]

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

我在搭建这些技术框架的时候,还整理了系统的高级进阶教程,会比自己碎片化学习效果强太多,GitHub可见;《Android架构视频+学习笔记》

当然,想要深入学习并掌握这些能力,并不简单。关于如何学习,做程序员这一行什么工作强度大家都懂,但是不管工作多忙,每周也要雷打不动的抽出 2 小时用来学习。

不出半年,你就能看出变化!

  • 30
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将 Android Bitmap 保存为 BMP 格式,可以使用以下步骤: 1. 创建一个 BITMAPFILEHEADER 结构体,用于保存 BMP 文件头信息。 2. 创建一个 BITMAPINFOHEADER 结构体,用于保存位图信息头。 3. 计算位图像素数据的大小,用于分配内存。 4. 将 Android Bitmap 转换为 RGB 格式的位图像素数据。 5. 将位图像素数据按照 BMP 文件格式存储到内存中。 6. 将 BITMAPFILEHEADER 和 BITMAPINFOHEADER 写入 BMP 文件开头。 7. 将位图像素数据写入 BMP 文件中。 以下是一个示例代码,用于将 Android Bitmap 保存为 BMP 格式: ```java import android.graphics.Bitmap; import android.graphics.Color; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; public class BitmapUtils { // BMP 文件头 private static class BITMAPFILEHEADER { public char bfType[] = {'B', 'M'}; public int bfSize; public short bfReserved1; public short bfReserved2; public int bfOffBits; } // BMP 位图信息头 private static class BITMAPINFOHEADER { public int biSize; public int biWidth; public int biHeight; public short biPlanes; public short biBitCount; public int biCompression; public int biSizeImage; public int biXPelsPerMeter; public int biYPelsPerMeter; public int biClrUsed; public int biClrImportant; } // 将 Android Bitmap 保存为 BMP 格式 public static boolean saveBitmapAsBmp(Bitmap bitmap, String filename) { // 获取位图信息 int width = bitmap.getWidth(); int height = bitmap.getHeight(); // 计算位图像素数据大小 int data_size = width * height * 3; // 创建 BMP 文件头 BITMAPFILEHEADER file_header = new BITMAPFILEHEADER(); file_header.bfSize = 14 + 40 + data_size; file_header.bfReserved1 = 0; file_header.bfReserved2 = 0; file_header.bfOffBits = 14 + 40; // 创建 BMP 位图信息头 BITMAPINFOHEADER info_header = new BITMAPINFOHEADER(); info_header.biSize = 40; info_header.biWidth = width; info_header.biHeight = height; info_header.biPlanes = 1; info_header.biBitCount = 24; info_header.biCompression = 0; info_header.biSizeImage = data_size; info_header.biXPelsPerMeter = 0; info_header.biYPelsPerMeter = 0; info_header.biClrUsed = 0; info_header.biClrImportant = 0; // 将 Android Bitmap 转换为 RGB 格式的位图像素数据 int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); byte[] data = new byte[data_size]; int i = 0; for (int y = height - 1; y >= 0; y--) { for (int x = 0; x < width; x++) { int pixel = pixels[y * width + x]; data[i++] = (byte) Color.blue(pixel); data[i++] = (byte) Color.green(pixel); data[i++] = (byte) Color.red(pixel); } } // 写入 BMP 文件 try { FileOutputStream fos = new FileOutputStream(new File(filename)); ByteBuffer buffer = ByteBuffer.allocate(14 + 40 + data_size); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.putChar('B'); buffer.putChar('M'); buffer.putInt(file_header.bfSize); buffer.putShort(file_header.bfReserved1); buffer.putShort(file_header.bfReserved2); buffer.putInt(file_header.bfOffBits); buffer.putInt(info_header.biSize); buffer.putInt(info_header.biWidth); buffer.putInt(info_header.biHeight); buffer.putShort(info_header.biPlanes); buffer.putShort(info_header.biBitCount); buffer.putInt(info_header.biCompression); buffer.putInt(info_header.biSizeImage); buffer.putInt(info_header.biXPelsPerMeter); buffer.putInt(info_header.biYPelsPerMeter); buffer.putInt(info_header.biClrUsed); buffer.putInt(info_header.biClrImportant); buffer.put(data); fos.write(buffer.array()); fos.close(); return true; } catch (IOException e) { e.printStackTrace(); return false; } } } ``` 这个函数需要传入一个 Android Bitmap 对象和一个保存文件名,函数会将 Android Bitmap 保存为 BMP 格式,并返回一个布尔值表示保存是否成功。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值