完成粗略的头像挂件和更换背景页面

This commit is contained in:
张玉久
2020-09-17 18:08:01 +08:00
parent afff3446c4
commit 97ab6108cf
50 changed files with 2301 additions and 76 deletions

View File

@ -1,5 +1,6 @@
package com.gh.common.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
@ -14,9 +15,17 @@ import com.halo.assistant.HaloApp;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import androidx.annotation.IntRange;
import androidx.annotation.RequiresApi;
import androidx.renderscript.Allocation;
import androidx.renderscript.Element;
import androidx.renderscript.RenderScript;
import androidx.renderscript.ScriptIntrinsicBlur;
/**
* Created by LGT on 2016/10/21.
*/
@ -114,6 +123,14 @@ public class BitmapUtils {
return true;
}
public static Bitmap getBitmapByFile(String filepath, int w, int h) {
return getBitmapByFile(filepath, w, h, Bitmap.Config.RGB_565);
}
public static Bitmap getBitmapByFile(String filepath, Bitmap.Config config) {
return getBitmapByFile(filepath, 0, 0, config);
}
/**
* 根据文件路径返回bitmap
*
@ -122,12 +139,12 @@ public class BitmapUtils {
* @param h 高
* @return bitmap
*/
public static Bitmap getBitmapByFile(String filepath, int w, int h) {
public static Bitmap getBitmapByFile(String filepath, int w, int h, Bitmap.Config config) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
// 设置为ture只获取图片大小
options.inJustDecodeBounds = true;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inPreferredConfig = config;
// 返回为空
BitmapFactory.decodeFile(filepath, options);
int width = options.outWidth;
@ -135,8 +152,8 @@ public class BitmapUtils {
float scaleWidth = 0.f, scaleHeight = 0.f;
if (width > w || height > h) {
// 缩放
scaleWidth = ((float) width) / w;
scaleHeight = ((float) height) / h;
if (w > 0) scaleWidth = ((float) width) / w;
if (h > 0) scaleHeight = ((float) height) / h;
}
options.inJustDecodeBounds = false;
int scale = (int) Math.ceil(Math.max(scaleWidth, scaleHeight));
@ -189,6 +206,7 @@ public class BitmapUtils {
/**
* Drawable转Bitmap
*
* @param isSquare 是否是正方形
*/
public static Bitmap drawableToBitmap(Drawable drawable, boolean isSquare) {
@ -196,7 +214,7 @@ public class BitmapUtils {
return null;
}
int w,h;
int w, h;
// 取 drawable 的长宽
w = drawable.getIntrinsicWidth();
@ -227,4 +245,108 @@ public class BitmapUtils {
return bitmap;
}
/**
* 修改bitmap透明度
*
* @param sourceImg
* @param config
* @param number
* @return
*/
public static Bitmap getTransparentBitmap(Bitmap sourceImg, Bitmap.Config config, int number) {
int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];
sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0, sourceImg
.getWidth(), sourceImg.getHeight());// 获得图片的ARGB值
number = number * 255 / 100;
for (int i = 0; i < argb.length; i++) {
argb[i] = (number << 24) | (argb[i] & 0x00FFFFFF);
}
sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg
.getHeight(), config);
return sourceImg;
}
/**
* 高斯模糊
*
* @param context
* @param image
* @param radius
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap getBlurBitmap(Context context, Bitmap image, @IntRange(from = 1, to = 25) int radius) {
// 计算图片缩小后的长宽
int width = Math.round(image.getWidth() * 0.8f);
int height = Math.round(image.getHeight() * 0.8f);
// 将缩小后的图片做为预渲染的图片。
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
// 创建一张渲染后的输出图片。
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
// 创建RenderScript内核对象
RenderScript rs = RenderScript.create(context);
// 创建一个模糊效果的RenderScript的工具对象
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
// 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间。
// 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去。
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createTyped(rs, tmpIn.getType());
// 设置渲染的模糊程度, 25f是最大模糊度
blurScript.setRadius(radius);
// 设置blurScript对象的输入内存
blurScript.setInput(tmpIn);
// 将输出数据保存到输出内存中
blurScript.forEach(tmpOut);
// 将数据填充到Allocation中
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
/**
* 保存图片
*
* @param bitmap
* @param path
*/
public static void saveBitmap(Bitmap bitmap, String path) {
File file = new File(path);
if (file.exists()) {
file.delete();
}
FileOutputStream out;
try {
out = new FileOutputStream(file);
if (bitmap.compress(Bitmap.CompressFormat.PNG, 80, out)) {
out.flush();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Bitmap compressBitmap(Bitmap bitmap) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Matrix matrix = new Matrix();
Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
result.compress(Bitmap.CompressFormat.WEBP, 100, bos);
while (bos.toByteArray().length > 400 * 1024) {
System.out.println(bos.toByteArray().length);
matrix.setScale(0.9f, 0.9f);
result = Bitmap.createBitmap(result, 0, 0, result.getWidth(), result.getHeight(), matrix, true);
bos.reset();
result.compress(Bitmap.CompressFormat.WEBP, 100, bos);
}
return result;
}
}