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

移动开发 作者: 2024-08-25 03:25:01
一、知识介绍 ①res资源图片是放在项目res文件下的资源图片 ②BitMap位图,一般文件后缀为BMP,需要编码器编码,如RGB565,RGB8888等。一种逐像素的显示对象,其执行效率高,但缺点也

一、知识介绍

二、项目案例

 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.OPAQUE
26                         ?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 }
<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     Button
10         android:id="@+id/btnBitmapFormRes"
11         android:text="Bitmap  form res"
12         android:layout_width13         android:layout_height="wrap_content" />
ImageView
16         ="@+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>
 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     @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( View.OnClickListener() {
24             @Override
25              onClick(View view) {
26                 Bitmap bitmapFormResources = ImgHelper.getBitmapFormResources(MainActivity.this27                 iv.setImageBitmap(bitmapFormResources);     资源图片转BitMap
28 
29                 Drawable drawableFromResources = ImgHelper.getDrawableFromResources(MainActivity.                iv.setImageDrawable(drawableFromResources); 资源图片转drawable
31 
32                 Bitmap bitmapFormDrawable = ImgHelper.getBitmapFormDrawable(MainActivity.33                 iv.setImageBitmap(bitmapFormDrawable);      ////drawable转BitMap
34 
35                 Drawable drawbleFormBitmap = ImgHelper.getDrawbleFormBitmap(MainActivity.36                 iv.setImageDrawable(drawbleFormBitmap);      BitMap转drawable
37             }
38         });
39 40 }
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68175.html