1. 程式人生 > >Android 圖片Bitmap,drawable,res資源圖片之間轉換

Android 圖片Bitmap,drawable,res資源圖片之間轉換

圖形 oid ott tor new phi mpat from vertical

一、知識介紹

  ①res資源圖片是放在項目res文件下的資源圖片

  ②BitMap位圖,一般文件後綴為BMP,需要編碼器編碼,如RGB565,RGB8888等。一種逐像素的顯示對象,其執行效率高,但缺點也很明顯,存儲效率低。

  ③Drawable,通用的圖形對象,它可以裝載常用的圖像,GIF,PNG,JPG,也支持BMP,提供一些高級的可視化的對象,如漸變,圖形等。

    技術分享圖片

二、項目案例

【步驟】

  ①將圖片放入res/drawable文件夾中,這裏面的圖片屬於res資源圖片

  ②將圖片處理定義成工具類,方便使用,也可以不這麽做。

  ③點擊按鈕,獲取圖片,顯示出來。

【項目結構】

    技術分享圖片

【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 <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="match_parent"
 6     android:orientation="vertical"
 7     tools:context=".MainActivity">
 8 
 9     <Button
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     <ImageView
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 </android.support.constraint.ConstraintLayout>

  【提示】可以看到這裏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 }

  【提示】為了方便我這裏就寫了一個按鈕,四種方式,相互配合,三種形式相互轉化

【效果】點擊按鈕後都將顯示如下效果

    技術分享圖片

Android 圖片Bitmap,drawable,res資源圖片之間轉換