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

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

概述一、知识介绍 ①res资源图片是放在项目res文件下的资源图片 ②BitMap位图,一般文件后缀为BMP,需要编码器编码,如RGB565,RGB8888等。一种逐像素的显示对象,其执行效率高,但缺点也 一、知识介绍

  ①res资源图片是放在项目res文件下的资源图片

  ②BitMap位图,一般文件后缀为BMP,需要编码器编码,如RGB565,RGB8888等。一种逐像素的显示对象,其执行效率高,但缺点也很明显,存储效率低。

  ③Drawable,通用的图形对象,它可以装载常用的图像,GIF,PNG,JPG,也支持BMP,提供一些高级的可视化的对象,如渐变,图形等。

    

二、项目案例【步骤】

  ①将图片放入res/drawable文件夹中,这里面的图片属于res资源图片

  ②将图片处理定义成工具类,方便使用,也可以不这么做。

  ③点击按钮,获取图片,显示出来。

【项目结构】

    

【imgHelper】
 1 import androID.content.Context; 2  androID.graphics.Bitmap; 3  androID.graphics.BitmapFactory; 4  androID.graphics.Canvas; 5  androID.graphics.PixelFormat; 6  androID.graphics.drawable.BitmapDrawable; 7  androID.graphics.drawable.Drawable; 8  9 public class imgHelper {10 11     static Bitmap getBitmapFormResources(Context context,int resID){12         return BitmapFactory.decodeResource(context.getResources(),resID);13     }14 15     static Drawable getDrawableFromresources(Context context,1)">16          context.getResources().getDrawable(resID);17 18 19     static Drawable getDrawbleFormBitmap(Context context,Bitmap bitmap){20         return new BitmapDrawable(context.getResources(),bitmap);21 22 23      Bitmap getBitmapFormDrawable(Context context,Drawable drawable){24         Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWIDth(),25                 drawable.getIntrinsicHeight(),drawable.getopacity()!= PixelFormat.OPAQUE26                         ?Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565);27         Canvas canvas =  Canvas(bitmap);28         drawable.setBounds(0,0,drawable.getIntrinsicWIDth(),drawable.getIntrinsicHeight());29         //设置绘画的边界,此处表示完整绘制30         drawable.draw(canvas);31          bitmap;32 33 }

  【提示】drawable转化成Bitmap时需要用到canvas(画布)进行绘制。设置绘制的大小,绘制的边界。

【layout_main】
<androID.support.constraint.ConstraintLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" 2     xmlns:app="http://schemas.androID.com/apk/res-auto" 3     xmlns:tools="http://schemas.androID.com/tools" 4     androID:layout_wIDth="match_parent" 5     androID:layout_height 6     androID:orIEntation="vertical" 7     tools:context=".MainActivity"> 9     button10         androID:ID="@+ID/btnBitmapFormRes"11         androID:text="Bitmap  form res"12         androID:layout_wIDth13         androID:layout_height="wrap_content" />ImageVIEw16         ="@+ID/iv"17 18 ="wrap_content"19         androID:layout_marginEnd="8dp"20         androID:layout_marginleft21         androID:layout_marginRight22         androID:layout_marginStart23         androID:layout_margintop24         app:layout_constraintEnd_toEndOf="parent"25         app:layout_constraintStart_toStartOf26         app:layout_constrainttop_toBottomOf="@+ID/btnBitmapFormRes" 27 28 </androID.support.constraint.ConstraintLayout>

  【提示】可以看到这里ImageVIEw没有设置图片

【Main_Activity】
 androID.support.v7.app.AppCompatActivity; androID.os.Bundle; androID.vIEw.VIEw; androID.Widget.button; androID.Widget.ImageVIEw; com.example.administrator.myapplication.utils.imgHelper;11 class MainActivity extends AppCompatActivity {12     button btnBitmapFormRes;14     ImageVIEw iv;15 16     @OverrIDe17     protected voID onCreate(Bundle savedInstanceState) {18         super.onCreate(savedInstanceState);19         setContentVIEw(R.layout.activity_main);20 21         btnBitmapFormRes = findVIEwByID(R.ID.btnBitmapFormRes);22         iv = findVIEwByID(R.ID.iv);23         btnBitmapFormRes.setonClickListener( VIEw.OnClickListener() {24             @OverrIDe25              onClick(VIEw vIEw) {26                 Bitmap bitmapFormResources = imgHelper.getBitmapFormResources(MainActivity.this27                 iv.setimageBitmap(bitmapFormResources);     资源图片转BitMap28 29                 Drawable drawableFromresources = imgHelper.getDrawableFromresources(MainActivity.                iv.setimageDrawable(drawableFromresources); 资源图片转drawable31 32                 Bitmap bitmapFormDrawable = imgHelper.getBitmapFormDrawable(MainActivity.33                 iv.setimageBitmap(bitmapFormDrawable);      ////drawable转BitMap34 35                 Drawable drawbleFormBitmap = imgHelper.getDrawbleFormBitmap(MainActivity.36                 iv.setimageDrawable(drawbleFormBitmap);      BitMap转drawable37             }38         });39 40 }

  【提示】为了方便我这里就写了一个按钮,四种方式,相互配合,三种形式相互转化

【效果】点击按钮后都将显示如下效果

    

 

总结

以上是内存溢出为你收集整理的Android 图片Bitmap,drawable,res资源图片之间转换全部内容,希望文章能够帮你解决Android 图片Bitmap,drawable,res资源图片之间转换所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1121262.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存