Bitmap

在Android SDK中说明可以支持的图片格式如下:png , jpg , gif,和bmp。

1.Bitmap的创建

借助于BitmapFactory。
1)资源中的图片
使用BitmapFactory获取位图
Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.testImg);
或者是
Resources res=getResources();
//使用BitmapDrawable获取位图
//使用BitmapDrawable (InputStream is)构造一个BitmapDrawable;
//使用BitmapDrawable类的getBitmap()获取得到位图;
// 读取InputStream并得到位图
InputStream is=res.openRawResource(R.drawable.testImg);
BitmapDrawable bmpDraw=new BitmapDrawable(is);
Bitmap bmp=bmpDraw.getBitmap();

2)SD卡中的图片
Bitmap bmp = BitmapFactory.decodeFile("/sdcard/testBitmap/testImg.png")

2. 把 Bitmap 保存在sdcard中
File fImage = new File("/sdcard/testBitmap/testImg.png");
fImage.createNewFile();
FileOutputStream iStream = new FileOutputStream(fImage);
bmp.compress(CompressFormat.PNG, 100, iStream);
iStream.close();
fImage.close();
iStream =null;
fImage =null;
//写到输出流里,就保存到文件了。

3.使用网络中的图片

//图片的链接地址
String imgURLStr = "http://tx.bdimg.com/sys/portrait/item/990e6271796a7a6c170c.jpg";
URL imgURL = new URL(imgURLStr);
URLConnection conn = imgURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);


//下载图片
Bitmap bmp = BitmapFactory.decodeStream(bis);

//关闭Stream
bis.close();
is.close();
imgURL =null;

4.显示图片
1)转换为BitmapDrawable对象显示位图
// 转换为BitmapDrawable对象
BitmapDrawable bmpDraw=new BitmapDrawable(bmp);
// 显示位图
ImageView iv2 = (ImageView)findViewById(R.id.ImageView02);
iv2.setImageDrawable(bmpDraw);
2)使用Canvas类显示位图
canvas.drawBitmap(bmp, 0, 0, null);

5.缩放位图
1)将一个位图按照需求重画一遍,画后的位图就是我们需要的了,与位图的显示几乎一样:drawBitmap(Bitmap bitmap, Rect src, Rect

dst, Paint paint)。

2)在原有位图的基础上,缩放原位图,创建一个新的位图:CreateBitmap(Bitmap source, int x, int y, int width, int height,

Matrix m, boolean filter)

3)借助Canvas的scale(float sx, float sy) ,不过要注意此时整个画布都缩放了。

4)借助Matrix:

Matrix matrix=new Matrix();
matrix.postScale(0.2f, 0.2f);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,true);
canvas.drawBitmap(dstbmp, 10, 10, null);
6.旋转位图
借助Matrix或者Canvas来实现。

Matrix matrix=new Matrix();
matrix.postRotate(45);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(), bmp.getHeight(),matrix,true);
canvas.drawBitmap(dstbmp, 10, 10, null);


### Bitmap 技术与使用方法 Bitmap 是一种常见的图像表示方式,它将图像存储为像素矩阵,每个像素点包含颜色信息。以下是关于 Bitmap 的技术细节和使用方法的详细说明。 #### 1. Bitmap 的基本概念 Bitmap 图像由一组像素组成,每个像素的颜色值通过 RGB 或 ARGB 表示[^2]。在 Android 开发中,Bitmap 常用于加载、处理和显示图片。例如,可以通过 `BitmapFactory` 类提供的方法从资源文件、文件系统或网络流中解码图片。 #### 2. 创建和加载 Bitmap 在 Android 中,可以使用以下方法创建和加载 Bitmap: ```java // 从资源文件加载 Bitmap Bitmap bitmapFromResource = BitmapFactory.decodeResource(getResources(), R.drawable.image); // 从文件路径加载 Bitmap Bitmap bitmapFromFile = BitmapFactory.decodeFile("/path/to/image.jpg"); // 从字节数组加载 Bitmap byte[] imageData = ...; // 图像数据 Bitmap bitmapFromByteArray = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); ``` 这些方法允许开发者根据需求选择不同的数据源来生成 Bitmap 对象[^2]。 #### 3. Bitmap 的压缩与优化 为了减少内存占用,可以对 Bitmap 进行压缩。以下是一个示例代码,展示如何通过设置 `BitmapFactory.Options` 来调整图片大小和质量: ```java BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; // 缩小图片尺寸为原来的 1/2 Bitmap compressedBitmap = BitmapFactory.decodeFile("/path/to/image.jpg", options); ``` 上述代码通过设置 `inSampleSize` 参数实现图片的缩放,从而降低内存消耗。 #### 4. Bitmap 在用户人群分析中的应用 在用户数据分析领域,Bitmap 可以用于高效存储和操作大规模布尔型数据集。例如,在 Starrocks 数据库中,Bitmap 被广泛应用于用户群体的交集、并集和差集计算。这种技术能够显著提高查询性能,尤其是在处理海量数据时[^1]。 #### 5. RoaringBitmap 扩展 RoaringBitmap 是一种高效的 Bitmap 实现方案,特别适用于稀疏数据集的压缩和操作。在 PostgreSQL 中,`pg_roaringbitmap` 扩展提供了对 RoaringBitmap 的支持,允许开发者在数据库层面进行复杂的集合运算[^4]。 #### 6. BitmapFont 渲染技术 BitmapFont 是一种用于游戏开发和其他图形界面的字体渲染技术。它通过预渲染的字符图像和 `.fnt` 文件定义字符位置,实现高效的文字绘制。以下是加载 BitmapFont 的示例代码: ```java BitmapFont font = new BitmapFont(Gdx.files.internal("font.fnt"), Gdx.files.internal("font.png"), false); font.draw(batch, "Hello World", 100, 100); ``` 这段代码展示了如何加载一个 BitmapFont 并将其绘制到屏幕上[^5]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值