这篇文章主要介绍了Android处理图像数据转换的各种方法,本文讲解了RGB值转Bitmap、Color值转Bitmap、字节数组转Bitmap、读取文件转Bitmap、读取资源转Bitmap、输入流转Bitmap等内容,需要的朋友可以参考下
private Bitmap createColorBitmap(String rgb, int width, int height) {
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int color = Color.parseColor(rgb);
bmp.eraseColor(color);
return bmp;
//Usage
Bitmap bmp = createColorBitmap("#cce8cf", 200, 50);
Color值转Bitmap
private Bitmap createColorBitmap(int color, int width, int height) {
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bmp.eraseColor(color);
return bmp;
//Usage
Bitmap bmp = createColorBitmap(Color.BLUE, 200, 50);
字节数组转Bitmap
private Bitmap getBitmapFromByteArray(byte[] array) {
return BitmapFactory.decodeByteArray(array, 0, array.length);
读取文件转Bitmap
private Bitmap getBitmapFromFile(String pathName) {
return BitmapFactory.decodeFile(pathName);
读取资源转Bitmap
private Bitmap getBitmapFromResource(Resources res, int resId) {
return BitmapFactory.decodeResource(res, resId);
输入流转Bitmap
private Bitmap getBitmapFromStream(InputStream inputStream) {
return BitmapFactory.decodeStream(inputStream);
Drawable转Bitmap
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_resource);
转为Drawable
资源转Drawable
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
Bitmap转Drawable
Drawable d = new BitmapDrawable(getResources(),bitmap);
图片圆角展示
通过对图片数据bitmap进行处理即可,其中pixels为边角的半径。
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
您可能感兴趣的文章:
Android数据转移之Launcher导出数据库给另一台机器加载
2021-11-11
Android 滑动小圆点ViewPager的两种设置方法详解流程
2021-11-11
Android实现渐变色水波纹效果
2021-11-11
Android Studio设置绘制布局时的视图
2021-11-11
通俗易通讲解Android蓝牙键值适配
2021-11-11
Windows下Flutter+Idea环境搭建及配置
2021-11-11
图文详解Flutter单例的实现
2021-11-11
Android CameraX结合LibYUV和GPUImage自
2021-11-11
美国设下计谋,用娘炮文化重塑日本,已影响至中国
2021-11-19
时空伴随者是什么意思?时空伴随者介绍
2021-11-09
工信部称网盘企业免费用户最低速率应满足基本下载需求,天翼云盘回应:坚决支持,始终
2021-11-05
2022年放假安排出炉:五一连休5天 2022年所有节日一览表
2021-10-26
电脑版
-
返回首页
2006-2023 脚本之家 JB51.Net , All Rights Reserved.
苏ICP备14036222号