一、知识介绍

  ①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资源图片之间转换的更多相关文章

  1. Android中 Bitmap Drawable Paint的获取、转换以及使用

    比如Drawable中有一系列连续的图片,img_0.png, img_1.png, img_2.png ... 如果要动态获取这些图片,通过"R.drawable.img_x"的 ...

  2. Android中Bitmap, Drawable, Byte,ID之间的转化

    Android中Bitmap, Drawable, Byte,ID之间的转化 1.  Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArray ...

  3. Android中Bitmap, Drawable, Byte之间的转化

    1.  Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap.compress(Bitmap. ...

  4. WPF调用图片路径,或资源图片

    一.加载本项目的图片WPF引入了统一资源标识Uri(Unified Resource Identifier)来标识和访问资源.其中较为常见的情况是用Uri加载图像.Uri表达式的一般形式为:协议+授权 ...

  5. Android 控件背景选择图片还是drawable XML资源

    决定一个控件应该是否用Drawable XML渲染,应考虑以下几个因素: * App是否要支持多分辨率: * App是否有瘦身的需要: * 图案是否足够简单: * 图案需要自由缩放: * 设计开发工作 ...

  6. Android实战简易教程-第九枪(BitmapFactory.Options对资源图片进行缩放)

    我们知道,我们编写的应用程序都是有一定内存限制的.程序占用了过高的内存就easy出现OOM(OutOfMemory)异常.因此在展示高分辨率图片的时候,最好先将图片进行压缩,压缩后的图片大小应该和用来 ...

  7. Android 将drawable下的图片转换成bitmap、Drawable

    将drawable下的图片转换成bitmap . Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xxx ...

  8. android将drawable下的图片转换成bitmap

    将drawable下的图片转换成bitmap 1. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xx ...

  9. 【Android 界面效果30】Android中ImageSwitcher结合Gallery展示SD卡中的资源图片

    本文主要是写关于ImageSwitcher结合Gallery组件如何展示SDCard中的资源图片,相信大家都看过API Demo 中也有关于这个例子的,但API Demo 中的例子是展示工程中Draw ...

随机推荐

  1. MySQL数据备份方法

    MySQL的备份和还原 备份:副本    RAID1,RAID10:保证硬件损坏而不会业务中止:        DROP TABLE mydb.tb1; 备份类型:        热备份.温备份和冷备 ...

  2. [Swift]LeetCode87. 扰乱字符串 | Scramble String

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  3. [Swift]LeetCode303. 区域和检索 - 数组不可变 | Range Sum Query - Immutable

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  4. linux入门--Linux和UNIX的关系及区别

    UNIX 与 Linux 之间的关系是一个很有意思的话题.在目前主流的服务器端操作系统中,UNIX 诞生于 20 世纪 60 年代末,Windows 诞生于 20 世纪 80 年代中期,Linux 诞 ...

  5. 学习HTML5 canvas遇到的问题

    学习HTML5 canvas遇到的问题 1. 非零环绕原则(nonzZero rule) 非零环绕原则是canvas在进行填充的时候是否要进行填充的判断依据. 在判断填充的区域拉一条线出来,拉到图形的 ...

  6. 使用google搜索时的10个小技巧!

    为大家分享一些google的技巧,很多工作了好几年的同学还不知道如何高效的利用这些技巧,希望同学们掌握!此为google的技巧,百度现在也基本上都实现了这些功能.   使用搜索引擎的10个搜索技巧   ...

  7. 剖析项目多个logback配置(上)

    来源:http://www.cnblogs.com/guozp/p/5949744.html 以下两个是我在使用slf4j + logback时候日志提示的问题,问题不大,都是WARN,并不真正影响运 ...

  8. 程序导致IIS服务器应用程序池停止

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0sAAAC6CAIAAADwAEEAAAAQMElEQVR4nO3dz4scV2IH8Ma7EB9CQC

  9. JS 中 原生方法 (二) --- 数组 (修---添加ES6新增)

    const arr = [1, 2, 3, 5, 'a', 'b'] /** * * length * 这个只能被 称之为 数组的原生属性, 返回 一个 number * arr.length */ ...

  10. pymysql.err.InternalError: (1205, 'Lock wait timeout exceeded; try restarting transaction')错误处理

    问题描述: 在使用pymysql库时,利用游标执行插入操作,产生错误,会出现pymysql.err.InternalError: (1205, 'Lock wait timeout exceeded; ...