Android 图片Bitmap,drawable,res资源图片之间转换
一、知识介绍
①res资源图片是放在项目res文件下的资源图片
②BitMap位图,一般文件后缀为BMP,需要编码器编码,如RGB565,RGB8888等。一种逐像素的显示对象,其执行效率高,但缺点也很明显,存储效率低。
③Drawable,通用的图形对象,它可以装载常用的图像,GIF,PNG,JPG,也支持BMP,提供一些高级的可视化的对象,如渐变,图形等。

二、项目案例
【步骤】
①将图片放入res/drawable文件夹中,这里面的图片属于res资源图片
②将图片处理定义成工具类,方便使用,也可以不这么做。
③点击按钮,获取图片,显示出来。
【项目结构】

【ImgHelper】
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; public class ImgHelper { public static Bitmap getBitmapFormResources(Context context,int resId){
return BitmapFactory.decodeResource(context.getResources(),resId);
} public static Drawable getDrawableFromResources(Context context,int resId){
return context.getResources().getDrawable(resId);
} public static Drawable getDrawbleFormBitmap(Context context,Bitmap bitmap){
return new BitmapDrawable(context.getResources(),bitmap);
} public static Bitmap getBitmapFormDrawable(Context context,Drawable drawable){
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),drawable.getOpacity()!= PixelFormat.OPAQUE
?Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
//设置绘画的边界,此处表示完整绘制
drawable.draw(canvas);
return bitmap;
}
}
【提示】drawable转化成Bitmap时需要用到canvas(画布)进行绘制。设置绘制的大小,绘制的边界。
【layout_main】
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"> <Button
android:id="@+id/btnBitmapFormRes"
android:text="Bitmap form res"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnBitmapFormRes" /> </android.support.constraint.ConstraintLayout>
【提示】可以看到这里ImageView没有设置图片
【Main_Activity】
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView; import com.example.administrator.myapplication.utils.ImgHelper; public class MainActivity extends AppCompatActivity { Button btnBitmapFormRes;
ImageView iv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btnBitmapFormRes = findViewById(R.id.btnBitmapFormRes);
iv = findViewById(R.id.iv);
btnBitmapFormRes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bitmap bitmapFormResources = ImgHelper.getBitmapFormResources(MainActivity.this, R.drawable.img1);
// iv.setImageBitmap(bitmapFormResources); //资源图片转BitMap Drawable drawableFromResources = ImgHelper.getDrawableFromResources(MainActivity.this, R.drawable.img1);
// iv.setImageDrawable(drawableFromResources); //资源图片转drawable Bitmap bitmapFormDrawable = ImgHelper.getBitmapFormDrawable(MainActivity.this, drawableFromResources);
iv.setImageBitmap(bitmapFormDrawable); ////drawable转BitMap Drawable drawbleFormBitmap = ImgHelper.getDrawbleFormBitmap(MainActivity.this, bitmapFormResources);
// iv.setImageDrawable(drawbleFormBitmap); //BitMap转drawable
}
});
}
}
【提示】为了方便我这里就写了一个按钮,四种方式,相互配合,三种形式相互转化
【效果】点击按钮后都将显示如下效果

Android 图片Bitmap,drawable,res资源图片之间转换的更多相关文章
- Android中 Bitmap Drawable Paint的获取、转换以及使用
比如Drawable中有一系列连续的图片,img_0.png, img_1.png, img_2.png ... 如果要动态获取这些图片,通过"R.drawable.img_x"的 ...
- Android中Bitmap, Drawable, Byte,ID之间的转化
Android中Bitmap, Drawable, Byte,ID之间的转化 1. Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArray ...
- Android中Bitmap, Drawable, Byte之间的转化
1. Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap.compress(Bitmap. ...
- WPF调用图片路径,或资源图片
一.加载本项目的图片WPF引入了统一资源标识Uri(Unified Resource Identifier)来标识和访问资源.其中较为常见的情况是用Uri加载图像.Uri表达式的一般形式为:协议+授权 ...
- Android 控件背景选择图片还是drawable XML资源
决定一个控件应该是否用Drawable XML渲染,应考虑以下几个因素: * App是否要支持多分辨率: * App是否有瘦身的需要: * 图案是否足够简单: * 图案需要自由缩放: * 设计开发工作 ...
- Android实战简易教程-第九枪(BitmapFactory.Options对资源图片进行缩放)
我们知道,我们编写的应用程序都是有一定内存限制的.程序占用了过高的内存就easy出现OOM(OutOfMemory)异常.因此在展示高分辨率图片的时候,最好先将图片进行压缩,压缩后的图片大小应该和用来 ...
- Android 将drawable下的图片转换成bitmap、Drawable
将drawable下的图片转换成bitmap . Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xxx ...
- android将drawable下的图片转换成bitmap
将drawable下的图片转换成bitmap 1. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xx ...
- 【Android 界面效果30】Android中ImageSwitcher结合Gallery展示SD卡中的资源图片
本文主要是写关于ImageSwitcher结合Gallery组件如何展示SDCard中的资源图片,相信大家都看过API Demo 中也有关于这个例子的,但API Demo 中的例子是展示工程中Draw ...
随机推荐
- 打个响指Selenium自动化开启
最近斗哥在朋友的影响下,接触了自动化测试工具中的一个项目:appium自动化测试脚本. appium类库封装了标准Selenium客户端类库,为用户提供所有常见的JSON格式selenium命令以及额 ...
- [Swift]LeetCode33. 搜索旋转排序数组 | Search in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- Maven项目执行java入口main方法
在Maven项目中配置pom.xml文件加载maven-surefire-plugin插件来执行testng.xml,相信大家对此种用法已经非常熟悉了.但是有些场景可能需要我们去加载执行java的ma ...
- 洛谷P2089烤鸡
题目链接:https://www.luogu.org/problemnew/show/P2089 题目详情: 题目背景 猪猪hanke得到了一只鸡 题目描述 猪猪Hanke特别喜欢吃烤鸡(本是同畜牲, ...
- python—day15 包的认识、执行顺序、执行流程、循环导入、包的导入、绝对、相对导入
一.包的认识 包通过文件夹来管理一系列功能相近的模块 包:一系列模块的集合体 重点:包中一定有一个专门用来管理包中所有模块的文件 包名:存放一系列模块的文件夹名字 包名(包对象)存放的是管理模 ...
- partition分区
1.按id拆分 mysql -uroot mysql>use test; mysql>create table topic ( tid ) not ),partition t1 value ...
- scala的input
获取用户输入的信息,一般使用 input 函数,但是 scala 里面是没有 input 这个方法的,为了获取控制台的输入操作, scala 定义的方法为: val Inputcontent = Co ...
- Nginx篇--解读nginx配置
一.前述 之前讲解了Nginx的源码安装与加载到系统服务中去,http://www.cnblogs.com/LHWorldBlog/p/8298226.html今天详细讲解Nginx中的具体配置. 二 ...
- 【JMeter】(2)---HTTP压测
JMeter---HTTP压测 一.创建线程组 右击-->添加-->Threads(Users)-->线程组 下面对比较重要的几个参数,讲解下: 名称: 就是给你这个线程组起名字. ...
- Zabbix Server端配置文件说明
zabbix作为运维邻域不可缺少的一员,它的各种文档可是数不胜数啊,但是关于配置文件的解释与说明就有点少.这里列出zabbix配置文件篇之zabbix_server. Zabbix Server端配置 ...