一、知识介绍

  ①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. Servlet, Struts2和SpringMVC 并发访问线程安全问题

    第一部分: Servlet不是线程安全的. 要解释Servlet为什么不是线程安全的,需要了解Servlet容器(即Tomcat)使如何响应HTTP请求的. 当Tomcat接收到Client的HTTP ...

  2. Thread.join(), CountDownLatch、CyclicBarrier和 Semaphore区别,联系及应用

    在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法, 由于 ...

  3. LeetCode题解38.Count and Say

    38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...

  4. [Swift]LeetCode509. 斐波那契数 | Fibonacci Number

    The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...

  5. java中过多if-else分支语句的优化方案

    利用Map优化过的的if-else分支 package com.taiping.test; import java.util.HashMap;import java.util.Map; public ...

  6. php内核一些常识

    整个PHP环境和Zend环境会涉及多个全局变量,下面是几个比较重要的: php_core_globals core_globals(main/php_globals.h) ==> PG PHP调 ...

  7. 死磕 java集合之WeakHashMap源码分析

    欢迎关注我的公众号"彤哥读源码",查看更多源码系列文章, 与彤哥一起畅游源码的海洋. 简介 WeakHashMap是一种弱引用map,内部的key会存储为弱引用,当jvm gc的时 ...

  8. C++ 断言

    assert宏 (基本概念与用法整理) assert宏的深入学习 1.运行时断言 1.1.assert属于运行时断言,可以在运行时判断给定条件是否为真,如果为真则什么也不做,否则打印一跳错误信息,然后 ...

  9. android studio 调用c/c++工程

    首先新建一个工程 这个工程主要测试通过C程序返回一个字符串显示在界面

  10. 一纸理解JVM

    JVM,JDK,JRE定义 JVM是Java Virtual Machine(Java虚拟机)的缩写. JDK是Java Development Kit JAVA语言开发工具箱(JAVA核心) JRE ...