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 }