Android CreateBitmap

本文介绍了一个Android应用程序示例,该程序通过不同配置创建Bitmap,并使用颜色数组填充Bitmap。此外,还介绍了如何将Bitmap压缩为JPEG和PNG格式,以及如何直接绘制颜色数组。

 

Bitmap中setPiexls的解释:

 

public void setPixels (int[] pixels, int offset, int stride, int x, int y, int width, int height)
Since:  API Level 1

Replace pixels in the bitmap with the colors in the array. Each element in the array is a packed int prepresenting a Color

Parameters
pixelsThe colors to write to the bitmap
offsetThe index of the first color to read from pixels[]
strideThe number of colors in pixels[] to skip between rows. Normally this value will be the same as the width of the bitmap, but it can be larger (or negative).
xThe x coordinate of the first pixel to write to in the bitmap.
yThe y coordinate of the first pixel to write to in the bitmap.
widthThe number of colors to copy from pixels[] per row
heightThe number of rows to write to the bitmap
Throws
IllegalStateExceptionif the bitmap is not mutable
IllegalArgumentExceptionif x, y, width, height are outside of the bitmap's bounds.
ArrayIndexOutOfBoundsExceptionif the pixels array is too small to receive the specified number of pixels.
生成颜色值数组的函数:
 

 


### 安卓中 `createBitmap` 的使用 `createBitmap` 是 `Bitmap` 类中的一个重要方法,用于创建新的 `Bitmap` 对象。以下是几种常见的使用方式: - **从现有 `Bitmap` 裁剪**:可以从一个已有的 `Bitmap` 中裁剪出一部分来创建新的 `Bitmap`。示例代码如下: ```java import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.widget.ImageView; // ... ImageView imageView = findViewById(R.id.appIcon); // 使用 Bitmap 获取保存在 drawable 中的图片 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.head); int width = bitmap.getWidth(); int height = bitmap.getHeight(); Bitmap bitmap1 = Bitmap.createBitmap(bitmap, width / 3, height / 4, bitmap.getWidth() / 3, bitmap.getHeight() / 2); imageView.setImageBitmap(bitmap1); ``` - **创建指定宽度、高度和配置的 `Bitmap`**:可以创建一个新的 `Bitmap`,指定其宽度、高度和像素配置。示例代码如下: ```java Bitmap.Config config = Bitmap.Config.ARGB_8888; Bitmap newBitmap = Bitmap.createBitmap(200, 200, config); ``` ### 安卓中 `createBitmap` 的原理 `createBitmap` 方法在层会根据传入的参数分配相应的内存空间来存储 `Bitmap` 的像素数据。当创建 `Bitmap` 时,系统会根据指定的宽度、高度和像素配置计算所需的内存大小,并在堆内存中分配该空间。例如,对于 `ARGB_8888` 配置的 `Bitmap`,每个像素占用 4 个字节的内存空间。当调用 `createBitmap` 方法时,系统会根据传入的宽度和高度计算总像素数,然后乘以每个像素占用的字节数,得到所需的内存大小,最后在堆内存中分配该空间并返回一个 `Bitmap` 对象。 ### `createBitmap` 与 `ArgbEvaluator` 的关联 `ArgbEvaluator` 主要用于颜色值的插值计算,实现颜色的平滑过渡。而 `createBitmap` 用于创建 `Bitmap` 对象。两者可以结合使用来实现一些特效,例如根据颜色过渡效果生成渐变的 `Bitmap`。示例代码如下: ```java import android.animation.ArgbEvaluator; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; // 创建一个新的 Bitmap Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); ArgbEvaluator evaluator = new ArgbEvaluator(); int startColor = 0xFFFF0000; // 红色 int endColor = 0xFF0000FF; // 蓝色 for (int i = 0; i < 200; i++) { float fraction = (float) i / 200; int color = (int) evaluator.evaluate(fraction, startColor, endColor); paint.setColor(color); canvas.drawLine(i, 0, i, 200, paint); } ``` ### `createBitmap` 与 `Region` 的关联 `Region` 用于表示一个区域,`createBitmap` 可以结合 `Region` 来实现一些裁剪或绘制特定区域的效果。例如,可以根据 `Region` 的范围从一个大的 `Bitmap` 中裁剪出所需的部分。示例代码如下: ```java import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Region; // 创建一个大的 Bitmap Bitmap largeBitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888); // 创建一个 Region Rect rect = new Rect(100, 100, 200, 200); Region region = new Region(rect); // 创建一个新的 Bitmap,用于存储裁剪后的部分 Bitmap croppedBitmap = Bitmap.createBitmap(rect.width(), rect.height(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(croppedBitmap); // 将大 Bitmap 中 Region 范围内的部分绘制到新的 Bitmap 上 canvas.clipRect(rect); canvas.drawBitmap(largeBitmap, -rect.left, -rect.top, new Paint()); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值