Android BitmapUtils工具类

本文介绍了Android开发中的图片处理技术,包括按比例裁剪、生成带倒影的图片、高质量压缩方法以及图片转换和保存。还强调了合理学习路径和资源的重要性,如学习资料、系统学习和解决问题的途径。
摘要由CSDN通过智能技术生成

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);

}

/**

  • 将图片路径转Bitmap

  • @Param path 图片路径

  • @return Bitmap

*/

public static Bitmap getBitmapPath(String path){

return BitmapFactory.decodeFile(path);

}

/**

  • bitmap保存到指定路径

  • @param file 图片的绝对路径

  • @param file 位图

  • @return bitmap

*/

public static boolean saveFile(String file, Bitmap bmp) {

if(TextUtils.isEmpty(file) || bmp == null) return false;

File f = new File(file);

if (f.exists()) {

f.delete();

}else {

File p = f.getParentFile();

if(!p.exists()) {

p.mkdirs();

}

}

try {

FileOutputStream out = new FileOutputStream(f);

bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);

out.flush();

out.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

return false;

}

return true;

}

/**

  • 回收一个未被回收的Bitmap

*@param bitmap

*/

public static void doRecycledIfNot(Bitmap bitmap) {

if (!bitmap.isRecycled()) {

bitmap.recycle();

}

}

/**

  • 将图片转换成Base64编码的字符串

*/

public static String imageToBase64(String path){

if(TextUtils.isEmpty(path)){

return null;

}

InputStream is = null;

byte[] data = null;

String result = null;

try{

is = new FileInputStream(path);

//创建一个字符流大小的数组。

data = new byte[is.available()];

//写入数组

is.read(data);

//用默认的编码格式进行编码

result = Base64.encodeToString(data,Base64.DEFAULT);

}catch (Exception e){

e.printStackTrace();

}finally {

if(null !=is){

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}
}
return result;
}
}

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

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

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

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

那我们该怎么做才能做到年薪60万+呢,对于程序员来说,只有不断学习,不断提升自己的实力。我之前有篇文章提到过,感兴趣的可以看看,到底要学习哪些知识才能达到年薪60万+。

通过职友集数据可以查看,以北京 Android 相关岗位为例,其中 【20k-30k】 薪酬的 Android 工程师,占到了整体从业者的 30.8%!

北京 Android 工程师「工资收入水平 」

今天重点内容是怎么去学,怎么提高自己的技术。

1.合理安排时间

2.找对好的系统的学习资料

3.有老师带,可以随时解决问题

4.有明确的学习路线

当然图中有什么需要补充的或者是需要改善的,可以在评论区写下来,一起交流学习。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

那我们该怎么做才能做到年薪60万+呢,对于程序员来说,只有不断学习,不断提升自己的实力。我之前有篇文章提到过,感兴趣的可以看看,到底要学习哪些知识才能达到年薪60万+。

通过职友集数据可以查看,以北京 Android 相关岗位为例,其中 【20k-30k】 薪酬的 Android 工程师,占到了整体从业者的 30.8%!

北京 Android 工程师「工资收入水平 」

[外链图片转存中…(img-8vlireqb-1712503129726)]

今天重点内容是怎么去学,怎么提高自己的技术。

1.合理安排时间

2.找对好的系统的学习资料

3.有老师带,可以随时解决问题

4.有明确的学习路线

当然图中有什么需要补充的或者是需要改善的,可以在评论区写下来,一起交流学习。

[外链图片转存中…(img-0c5A01A0-1712503129727)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值