Adnroid Multimedia---Pictures

Source: Internet
Author: User
Tags gety

The way the computer represents graphics BMP preserves all types of pictures at high quality for the computer.The computer uses pixel dots to represent the graphic, each pixel is a color, 24-bit depth (24bit).
The color of each pixel is an RGB, expressed in 6 hexadecimal digits.
The size of the data that the file header information occupies. The volume is relatively large.
JPG saves pictures in good quality for your computer, email, or network. JPG Industrial Graphics compression algorithm (distortion), similar to the RAR algorithm. The accuracy of human eye recognition is limited, and the same color is used to denote similar colors in adjacent spaces. Reduced volume, distortion.
PNG saves pictures or drawings at high quality for use in computers or networks.Non-distortion compression algorithm, volume reduction

To load a large picture of Oom exception Java.lang.OutOfMemoryError:

Heap memory space: Allocates memory space for class instances and arrays.

RGB 24

ARGB 32

The application creates a process for it at startup, and the system creates a Dalvik virtual machine (the VM heap set by the emulator) for each process, which is a memory overflow (memory leak) when the size of the picture is larger than the heap memory space of the virtual machine;
Workaround: Scale the picture to load into memory
Steps:
1, to obtain the resolution of the device screen;
2, get the resolution of the original image;
3, by comparison to obtain a suitable proportion value;
4. Scale the picture according to the proportional value
5. Display the picture in ImageView

Scale the picture and load it into memorySteps:1, to obtain the resolution of the device screen;
2, get the resolution of the original image;
3, by comparison to obtain a suitable proportion value;
4. Scale the picture according to the proportional value
5. Display the picture in ImageView
Code:
Package Com.itheima.loadbigpic;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.bitmapfactory.options;import Android.os.Bundle;import Android.os.environment;import Android.view.display;import Android.view.windowmanager;import    Android.widget.imageview;public class Mainactivity extends Activity {private ImageView IV;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main);        IV = (ImageView) Findviewbyid (R.ID.IV);         1. Obtain the resolution of the device screen;//Get the window manager through the service WindowManager WM = (WindowManager) getsystemservice (Window_service);                Get device Resolution Object Display display = Wm.getdefaultdisplay ();        Api13 before using this two method, 13 is set to expire int screenwidth = Display.getwidth ();                int screenheight = Display.getheight (); 2, get the resolution of the original image; Options opts = New Options ();        Opts.injustdecodebounds = true; OPTs is null to return a picture object, or null if NOT NULL bitmapfactory.decodefile (Environment.getexternalstoragedirectory () + "/LP        . jpg ", opts);         Get the original image width and height int srcwidth = opts.outwidth;                  int srcheight = Opts.outheight;         3. A suitable proportional value is obtained by comparison;//3000/320 = 9 2262/480 =5 int sx = Srcwidth/screenwidth;         int sy = srcheight/screenheight;         int scale = 0;         if (SX >= 1 && SX > Sy) {scale = SX;         }else if (Sy >= 1 && sy > SX) {scale = SY;           }//4, scale the picture by proportional value opts.injustdecodebounds = false; Opts.insamplesize = scale; 1/scale * 1/scale Bitmap BM = bitmapfactory.decodefile (environment.getexternalstoragedirectory () + "/lp.jpg", O          pts);    5, the picture displayed in ImageView iv.setimagebitmap (BM); }}
Memory space Create a copy of the original image step1 I'm going to create a copy of the original. He's just an empty bitmap. Understood to be a blank sheet of paper
Bitmap Copybbitmap = Bitmap.createbitmap (Srcbitmap.getwidth (), Srcbitmap.getheight (), Srcbitmap.getconfig ());

2 I need a paintbrush to paint the white paper.
Paint paint = new paint (); Create a brush
3 need a canvas to get a canvas to put the paper on the canvas
Canvas canvas = new canvas (COPYBBITMAP);
4 Start painting Bitmap: Refer to Who to draw matrix matrices
Canvas.drawbitmap (Srcbitmap, New Matrix (), paint);
Copybbitmap.setpixel (ten, color.red);
5 copies of copies displayed on Copyimageview

Iv_copy.setimagebitmap (COPYBBITMAP);

Code
Package Com.itheima.copypic;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.matrix;import Android.graphics.paint;import Android.os.bundle;import Android.widget.ImageView; public class Mainactivity extends Activity {private ImageView IV; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); iv = (ImageView)        Findviewbyid (R.ID.IV);//1, get the original image, Bitmap srcpic = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.MEINV); Config includes bit depth//2, create a blank paper, reference original; Bitmap copypic = Bitmap.createbitmap (Srcpic.getwidth (), srcpic.getheight (), SRC   Pic.getconfig ());//3, create an artboard, reference blank paper; Canvas canvas = new canvas (copypic);   4. Create a brush; paint paint = new paint (); Paint.setcolor (Color.Black);//The default is to use black, followed by the original color painting//5, painting on the artboard; Canvas.drawbitmap (Srcpic, New Matrix (), PaiNT); Iv.setimagebitmap (Copypic);}}
Common API rotation for computer graphics processing
public class Mainactivity extends Activity {@SuppressLint ("Sdcardpath") @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// Find the 2 Imageviewimageview we care about IV_SRC = (ImageView) Findviewbyid (R.ID.IV_SRC); ImageView iv_copy = (ImageView) Findviewbyid (r.id.iv_copy);//display original//get to the original Bitmap he is read-only cannot be modified Bitmap Srcbitmap = Bitmapfactory.decodefile ("/mnt/ Sdcard/meinv.png "); Iv_src.setimagebitmap (srcbitmap);//1 I'm going to create a copy of the original. He was just an empty Bitmap understood to be a blank paper Bitmap Copybbitmap = Bitmap. CreateBitmap (Srcbitmap.getwidth () +40, Srcbitmap.getheight (), Srcbitmap.getconfig ());//2 I'm going to paint the white paper. Need Brush paint paint = new   Paint (); Create a brush//3 need a canvas to get a canvas to put the paper onto the canvas. Canvas canvas = new canvas (COPYBBITMAP);//4 began to paint bitmap: Refer to who draws matrix matrices Matri X matrix = new Matrix ();//rotation of the graph rotation 30//matrix.setrotate (+); Matrix.setrotate (Copybbitmap.getwidth ()/2, Copybbitmap.getheight ()/2); Canvas.drawbitmap (Srcbitmap, Matrix, paint);//5 copy copies to copYimageview on Iv_copy.setimagebitmap (COPYBBITMAP);}} 
Scaling
public class Mainactivity extends Activity {@SuppressLint ("Sdcardpath") @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// Find the 2 Imageviewimageview we care about IV_SRC = (ImageView) Findviewbyid (R.ID.IV_SRC); ImageView iv_copy = (ImageView) Findviewbyid (r.id.iv_copy);//display original//get to the original Bitmap he is read-only cannot be modified Bitmap Srcbitmap = Bitmapfactory.decodefile ("/mnt/ Sdcard/meinv.png "); Iv_src.setimagebitmap (srcbitmap);//1 I'm going to create a copy of the original. He was just an empty Bitmap understood to be a blank paper Bitmap Copybbitmap = Bitmap. CreateBitmap (Srcbitmap.getwidth () * *, Srcbitmap.getheight () * * *, srcbitmap.getconfig ());//2 I'm going to paint the white paper. Need Brush paint paint =   New Paint (); Create a brush//3 need a canvas to get a canvas to put the paper onto the canvas. Canvas canvas = new canvas (COPYBBITMAP);//4 began to paint bitmap: Refer to who draws matrix matrices Matri X matrix = new Matrix ();//Graphics Scaling processing Matrix.setscale (2, 2); Canvas.drawbitmap (Srcbitmap, Matrix, paint);//5 copy copies to Copyim Ageview on Iv_copy.setimagebitmap (COPYBBITMAP);}}


Translation

public class Mainactivity extends Activity {@SuppressLint ("Sdcardpath") @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// Find the 2 Imageviewimageview we care about IV_SRC = (ImageView) Findviewbyid (R.ID.IV_SRC); ImageView iv_copy = (ImageView) Findviewbyid (r.id.iv_copy);//display original//get to the original Bitmap he is read-only cannot be modified Bitmap Srcbitmap = Bitmapfactory.decodefile ("/mnt/ Sdcard/meinv.png "); Iv_src.setimagebitmap (srcbitmap);//1 I'm going to create a copy of the original. He was just an empty Bitmap understood to be a blank paper Bitmap Copybbitmap = Bitmap. CreateBitmap (Srcbitmap.getwidth () +40, Srcbitmap.getheight (), Srcbitmap.getconfig ());//2 I'm going to paint the white paper. Need Brush paint paint = new   Paint (); Create a brush//3 need a canvas to get a canvas to put the paper onto the canvas. Canvas canvas = new canvas (COPYBBITMAP);//4 began to paint bitmap: Refer to who draws matrix matrices Matri X matrix = new Matrix ();//translation of the graph Matrix.settranslate (canvas.drawbitmap), Srcbitmap, Matrix, paint)//5 copy copies to co Pyimageview on Iv_copy.setimagebitmap (COPYBBITMAP);}}

Mirror

public class Mainactivity extends Activity {@SuppressLint ("Sdcardpath") @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// Find the 2 Imageviewimageview we care about IV_SRC = (ImageView) Findviewbyid (R.ID.IV_SRC); ImageView iv_copy = (ImageView) Findviewbyid (r.id.iv_copy);//display original//get to the original Bitmap he is read-only cannot be modified Bitmap Srcbitmap = Bitmapfactory.decodefile ("/mnt/ Sdcard/meinv.png "); Iv_src.setimagebitmap (srcbitmap);//1 I'm going to create a copy of the original. He was just an empty Bitmap understood to be a blank paper Bitmap Copybbitmap = Bitmap. CreateBitmap (Srcbitmap.getwidth (), Srcbitmap.getheight (), Srcbitmap.getconfig ());//2 I'm going to paint the white paper. Need Brush paint paint = new   Paint (); Create a brush//3 need a canvas to get a canvas to put the paper onto the canvas. Canvas canvas = new canvas (COPYBBITMAP);//4 began to paint bitmap: Refer to who draws matrix matrices Matri X matrix = new Matrix ();//Mirror effect Matrix.setscale ( -1.0f, 1.0f); Matrix.posttranslate (Copybbitmap.getwidth (), 0); Canvas.drawbitmap (Srcbitmap, Matrix, paint);//5 copy copies to Copyimageview Iv_copy.setimagebItmap (Copybbitmap);}} 

Reflection
public class Mainactivity extends Activity {@SuppressLint ("Sdcardpath") @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// Find the 2 Imageviewimageview we care about IV_SRC = (ImageView) Findviewbyid (R.ID.IV_SRC); ImageView iv_copy = (ImageView) Findviewbyid (r.id.iv_copy);//display original//get to the original Bitmap he is read-only cannot be modified Bitmap Srcbitmap = Bitmapfactory.decodefile ("/mnt/ Sdcard/meinv.png "); Iv_src.setimagebitmap (srcbitmap);//1 I'm going to create a copy of the original. He was just an empty Bitmap understood to be a blank paper Bitmap Copybbitmap = Bitmap. CreateBitmap (Srcbitmap.getwidth (), Srcbitmap.getheight (), Srcbitmap.getconfig ());//2 I'm going to paint the white paper. Need Brush paint paint = new   Paint (); Create a brush//3 need a canvas to get a canvas to put the paper onto the canvas. Canvas canvas = new canvas (COPYBBITMAP);//4 began to paint bitmap: Refer to who draws matrix matrices Matri X matrix = new Matrix ();//Graphic Reflection effect Matrix.setscale (1.0f, -1.0f); matrix.posttranslate (0, Srcbitmap.getheight ()); Canvas.drawbitmap (Srcbitmap, Matrix, paint);//5 copy copies to Copyimageview Iv_copy.setimagebiTMap (Copybbitmap);}} 
Modify Color
public class Mainactivity extends Activity {@SuppressLint ("Sdcardpath") @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// Find the 2 Imageviewimageview we care about IV_SRC = (ImageView) Findviewbyid (R.ID.IV_SRC); ImageView iv_copy = (ImageView) Findviewbyid (r.id.iv_copy);//display original//get to the original Bitmap he is read-only cannot be modified Bitmap Srcbitmap = Bitmapfactory.decodefile ("/mnt/ Sdcard/meinv.png "); Iv_src.setimagebitmap (srcbitmap);//1 I'm going to create a copy of the original. He was just an empty Bitmap understood to be a blank paper Bitmap Copybbitmap = Bitmap. CreateBitmap (Srcbitmap.getwidth (), Srcbitmap.getheight (), Srcbitmap.getconfig ());//2 I'm going to paint the white paper. Need Brush paint paint = new   Paint (); Create a brush//3 need a canvas to get a canvas to put the paper onto the canvas. Canvas canvas = new canvas (COPYBBITMAP);//4 began to paint bitmap: Refer to who draws matrix color Matrix cm = new ColorMatrix (); Create a color matrix Cm.set (new float[] {2, 0, 0, 0, 0,0, 1, 0, 0, 0,0, 0, 1, 0, 0,0, 0, 0, 1, 0});p aint.setcolorfilter (New Colormat Rixcolorfilter (cm));/** New Red Value = 1*+ 0*128 + 0*128 + 0*0 + 0New Blue value = 0*128 + 1*128 + 0*128 + 0*0 + 0New Green Value = 0*128 + 0*128 + 1*128 + 0*0 + 0New Alpha Value = 0*128 + 0*128 + 0*128 + 1*0 + 0*/canvas.drawbitmap (srcbitmap, New Matrix (), paint);//5 display copies of copies to co Pyimageview on Iv_copy.setimagebitmap (COPYBBITMAP);}}
Example code: Drawing board
Package Com.itheima.painter;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.matrix;import Android.graphics.paint;import Android.os.bundle;import android.view.MotionEvent; Import Android.view.view;import Android.view.view.ontouchlistener;import Android.widget.imageview;public class Mainactivity extends Activity {private ImageView iv;private Bitmap srcpic;private Bitmap copypic;private Canvas CANVAS;PR ivate paint paint; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); iv = (ImageView) Findviewbyid (R.ID.IV); srcpic = Bitmapfactory.decoderesource    (Getresources (), r.drawable.bg); Copypic = Bitmap.createbitmap (Srcpic.getwidth (), Srcpic.getheight (), Srcpic.getconfig ()); canvas = new Canvas (copypic)   ;   Paint = new paint ();  Paint.setcolor (Color.Black);//The default is to use black, followed by the original color drawing// 5. Draw on the artboard; matrix matrix = new Matrix ();//Canvas.drawbitmap (Srcpic, Matrix, paint);      Iv.setimagebitmap (Copypic);   Iv.setontouchlistener (New Ontouchlistener () {int startX; int starty; @Overridepublic boolean OnTouch (View V, motionevent event) {int type = event.getaction (); switch (type) {case Mo tionevent.action_down://start coordinate point StartX = (int) event.getx (); Starty = (int) event.gety (); Break;case motionevent.action_move://Get end point coordinates int newx = (int) event.getx (); int newy = (int) E Vent.gety (); Canvas.drawline (StartX, Starty, newx, newy, paint); Iv.setimagebitmap (copypic);//Get new start coordinate point StartX = (int) Event.getx (); Starty = (int) event.gety (); break;}      Consumes the current listener return true;   }); }}




Adnroid Multimedia---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.