Android 图片Bitmap,drawable,res资源图片之间转换

作者 : 开心源码 本文共4077个字,预计阅读时间需要11分钟 发布时间: 2022-05-12 共205人阅读

一、知识详情

①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进阶技术大纲及进阶资料,及面试题集

想学习更多Android知识,请加入Android技术开发交流 7520 16839

进群与大牛们一起探讨,还可获取Android高级架构资料、源码、笔记、视频

包括 高级UI、Gradle、RxJava、小程序、Hybrid、移动架构、React Native、性能优化等全面的Android高级实践技术讲解性能优化架构思维导图,和BATJ面试题及答案!

群里免费分享给有需要的朋友,希望能够帮助少量在这个行业发展迷茫的,或者者想系统深入提升以及困于瓶颈的
朋友,在网上博客论坛等地方少花些时间找资料,把有限的时间,真正花在学习上,所以我在这免费分享少量架构资料及给大家。希望在这些资料中都有你需要的内容。

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » Android 图片Bitmap,drawable,res资源图片之间转换

发表回复