Android Picture Bitmap,drawable,res Resource Picture Conversion

Source: Internet
Author: User

First, Knowledge introduction

①res resource picture is a resource picture placed under the project res file

②bitmap Bitmap, the general file suffix is BMP, need encoder coding, such as rgb565,rgb8888. A pixel-wise display object with high execution efficiency, but with obvious drawbacks and low storage efficiency.

③drawable, a general-purpose graphical object that can load commonly used images, gif,png,jpg, also supports BMP, providing some advanced visual objects such as gradients, graphics, etc.

    

second, the project case"Step"

① put the picture in the Res/drawable folder, the picture belongs to res resource picture

② defines image processing as a tool class that is easy to use or not.

③ Click on the button to get the picture and show it.

"Project Structure"

    

"Imghelper"
1 ImportAndroid.content.Context;2 ImportAndroid.graphics.Bitmap;3 Importandroid.graphics.BitmapFactory;4 ImportAndroid.graphics.Canvas;5 ImportAndroid.graphics.PixelFormat;6 Importandroid.graphics.drawable.BitmapDrawable;7 Importandroid.graphics.drawable.Drawable;8 9  Public classImghelper {Ten  One      Public StaticBitmap getbitmapformresources (Context context,intresId) { A         returnBitmapfactory.decoderesource (Context.getresources (), resId); -     } -  the      Public StaticDrawable getdrawablefromresources (Context context,intresId) { -         returncontext.getresources (). getdrawable (resId); -     } -  +      Public Staticdrawable getdrawbleformbitmap (Context context,bitmap Bitmap) { -         return Newbitmapdrawable (Context.getresources (), bitmap); +     } A  at      Public StaticBitmap getbitmapformdrawable (Context context,drawable drawable) { -Bitmap Bitmap =Bitmap.createbitmap (Drawable.getintrinsicwidth (), -Drawable.getintrinsicheight (), drawable.getopacity ()! =Pixelformat.opaque -?Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565); -Canvas Canvas =NewCanvas (bitmap); -Drawable.setbounds (0,0, Drawable.getintrinsicwidth (), Drawable.getintrinsicheight ()); in         //sets the bounds of the painting, which represents the complete drawing - Drawable.draw (canvas); to         returnbitmap; +     } -}

"Tip" the canvas (canvas) is used to draw drawable when converted to bitmap. Sets the size of the drawing, drawing the bounds.

"Layout_main"
1 <Android.support.constraint.ConstraintLayoutxmlns: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     <ButtonTen         Android:id= "@+id/btnbitmapformres" One Android:text= "Bitmap form res" A Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" /> -  the     <ImageView -         Android:id= "@+id/iv" - Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" + Android:layout_marginend= "8DP" - Android:layout_marginleft= "8DP" + Android:layout_marginright= "8DP" A Android:layout_marginstart= "8DP" at Android:layout_margintop= "8DP" - app:layout_constraintend_toendof= "Parent" - App:layout_constraintstart_tostartof= "Parent" - App:layout_constrainttop_tobottomof= "@+id/btnbitmapformres" /> -  - </Android.support.constraint.ConstraintLayout>

"Hint" can see here ImageView not set picture

"Main_activity"
1 ImportAndroid.graphics.Bitmap;2 Importandroid.graphics.drawable.Drawable;3 Importandroid.support.v7.app.AppCompatActivity;4 ImportAndroid.os.Bundle;5 ImportAndroid.view.View;6 ImportAndroid.widget.Button;7 ImportAndroid.widget.ImageView;8 9 ImportCom.example.administrator.myapplication.utils.ImgHelper;Ten  One  Public classMainactivityextendsappcompatactivity { A  - Button btnbitmapformres; - ImageView IV; the  - @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); -  +Btnbitmapformres =Findviewbyid (r.id.btnbitmapformres); AIV =Findviewbyid (R.ID.IV); atBtnbitmapformres.setonclicklistener (NewView.onclicklistener () { - @Override -              Public voidOnClick (view view) { -Bitmap bitmapformresources = imghelper.getbitmapformresources (mainactivity. This, R.DRAWABLE.IMG1); - //Iv.setimagebitmap (bitmapformresources); //Resources Picture Turn bitmap -  indrawable drawablefromresources = imghelper.getdrawablefromresources (mainactivity. This, R.DRAWABLE.IMG1); - //iv.setimagedrawable (drawablefromresources);//Resources Picture Turn drawable to  +Bitmap bitmapformdrawable = imghelper.getbitmapformdrawable (mainactivity. This, drawablefromresources); -Iv.setimagebitmap (bitmapformdrawable);////drawable turn Bitmap the  *drawable Drawbleformbitmap = Imghelper.getdrawbleformbitmap (mainactivity. This, bitmapformresources); $ //iv.setimagedrawable (DRAWBLEFORMBITMAP); //Bitmap Turn drawablePanax Notoginseng             } -         }); the     } +}

"Hint" in order to facilitate I wrote a button here, four ways, mutual cooperation, three forms of mutual transformation

the effects button will show the following effects when clicked

    

Convert Android pictures bitmap,drawable,res resource pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.