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

一、知识介绍

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

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

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

27862a2ad751ee6dcbfa4066ea7c4c81.png

二、项目案例

【步骤】

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

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

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

【项目结构】

d7b8c75fb23605d4519a63b5d1804149.png

【ImgHelper】

1 import android.content.Context;

2 import android.graphics.Bitmap;

3 import android.graphics.BitmapFactory;

4 import android.graphics.Canvas;

5 import android.graphics.PixelFormat;

6 import android.graphics.drawable.BitmapDrawable;

7 import android.graphics.drawable.Drawable;

8

9 public class ImgHelper {

10

11 public static Bitmap getBitmapFormResources(Context context,int resId){

12 return BitmapFactory.decodeResource(context.getResources(),resId);

13 }

14

15 public static Drawable getDrawableFromResources(Context context,int resId){

16 return context.getResources().getDrawable(resId);

17 }

18

19 public static Drawable getDrawbleFormBitmap(Context context,Bitmap bitmap){

20 return new BitmapDrawable(context.getResources(),bitmap);

21 }

22

23 public static Bitmap getBitmapFormDrawable(Context context,Drawable drawable){

24 Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),

25 drawable.getIntrinsicHeight(),drawable.getOpacity()!= PixelFormat.OPAQUE

26 ?Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565);

27 Canvas canvas = new Canvas(bitmap);

28 drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());

29 //设置绘画的边界,此处表示完整绘制

30 drawable.draw(canvas);

31 return bitmap;

32 }

33 }

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

【layout_main】

1

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="match_parent"

6 android:orientation="vertical"

7 tools:context=".MainActivity">

8

9

10 android:id="@+id/btnBitmapFormRes"

11 android:text="Bitmap form res"

12 android:layout_width="match_parent"

13 android:layout_height="wrap_content" />

14

15

16 android:id="@+id/iv"

17 android:layout_width="match_parent"

18 android:layout_height="wrap_content"

19 android:layout_marginEnd="8dp"

20 android:layout_marginLeft="8dp"

21 android:layout_marginRight="8dp"

22 android:layout_marginStart="8dp"

23 android:layout_marginTop="8dp"

24 app:layout_constraintEnd_toEndOf="parent"

25 app:layout_constraintStart_toStartOf="parent"

26 app:layout_constraintTop_toBottomOf="@+id/btnBitmapFormRes" />

27

28

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

【Main_Activity】

1 import android.graphics.Bitmap;

2 import android.graphics.drawable.Drawable;

3 import android.support.v7.app.AppCompatActivity;

4 import android.os.Bundle;

5 import android.view.View;

6 import android.widget.Button;

7 import android.widget.ImageView;

8

9 import com.example.administrator.myapplication.utils.ImgHelper;

10

11 public class MainActivity extends AppCompatActivity {

12

13 Button btnBitmapFormRes;

14 ImageView iv;

15

16 @Override

17 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(new View.OnClickListener() {

24 @Override

25 public void onClick(View view) {

26 Bitmap bitmapFormResources = ImgHelper.getBitmapFormResources(MainActivity.this, R.drawable.img1);

27 // iv.setImageBitmap(bitmapFormResources); //资源图片转BitMap

28

29 Drawable drawableFromResources = ImgHelper.getDrawableFromResources(MainActivity.this, R.drawable.img1);

30 // iv.setImageDrawable(drawableFromResources); //资源图片转drawable

31

32 Bitmap bitmapFormDrawable = ImgHelper.getBitmapFormDrawable(MainActivity.this, drawableFromResources);

33 iv.setImageBitmap(bitmapFormDrawable); drawable转BitMap

34

35 Drawable drawbleFormBitmap = ImgHelper.getDrawbleFormBitmap(MainActivity.this, bitmapFormResources);

36 // iv.setImageDrawable(drawbleFormBitmap); //BitMap转drawable

37 }

38 });

39 }

40 }

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

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

ea0325d07eb672c08caf0007458c4b54.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,可以使用多种方式来读取图片并生成Bitmap对象。以下是几种常见的方法: 1. 使用文件流方式读取图片: ``` FileInputStream fis = new FileInputStream("/sdcard/test.png"); Bitmap bitmap = BitmapFactory.decodeStream(fis); ``` 2. 使用R文件方式读取图片: ``` Bitmap bitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.test); ``` 3. 使用ResourceStream方式读取图片,但不使用R文件: ``` Bitmap bitmap = BitmapFactory.decodeStream(getClass().getResourceAsStream("/res/drawable/test.png")); ``` 在读取图片时,还可以通过设置BitmapFactory.Options来进行一些优化操作。例如,可以设置inSampleSize来减小图片的宽高,从而减少内存占用: ``` BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; // 图片宽高都为原来的二分之一,即图片为原来的四分之一 Bitmap bitmap = BitmapFactory.decodeStream(fis, null, options); ``` 需要注意的是,以上方法中的路径或资源ID需要根据实际情况进行修改。此外,在使用ImageView显示Bitmap时,可以通过开启视图缓存的方式来获取缓存的Bitmap对象: ``` imageView.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache()); imageView.setDrawingCacheEnabled(false); ``` 最后,根据需要对获取到的Bitmap对象进行进一步的处理,例如模糊处理等。 #### 引用[.reference_title] - *1* [安卓Bitmap读取图片](https://blog.csdn.net/zbuger/article/details/46895335)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Android中从ImageView中取出图片bitmap注意事项](https://blog.csdn.net/u013933272/article/details/50987092)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Android学习 | 10.使用位图工具Bitmap在存储卡上读写图片文件](https://blog.csdn.net/M_Nobody/article/details/126141636)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值