ANDROID95 Zoom Load Large picture
Last Update:2015-11-13
Source: Internet
Author: User
<span id="Label3"></p><p><p>Mainactivity:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">com.itheima.loadimage;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.os.Bundle;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.app.Activity;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.graphics.Bitmap;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.graphics.BitmapFactory;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.graphics.BitmapFactory.Options;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.graphics.Point;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.Display;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.Menu;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.View;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.widget.ImageView;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mainactivity<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Activity {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); } </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">#加载大图片 * The computer resolves all pixel information in the image, <span style="color: #ff0000;">saves it to memory, and overflows the memory, so it needs to be scaled. </span>* Android Save picture Pixel information is saved with ARGB * Phone screen 320*480, total pixels: 153600 * Picture width high 2400*3200, total pixel 7680000 picture total pixels larger than the total pixels of the screen, so you want to scale the picture , or the memory overflows. * 2400/320 = 7, Wide scale scale * 3200/480 = 6, High zoom <span style="color: #ff0000;">picture width and height scale to the same size, select Large as the zoom Ratio. </span></span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Click (View v) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">parameters that need to be used to parse the image are encapsulated in this object.</span></span>Options opt =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Options (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">do not return picture does not load picture to memory only get picture width high, Avoid memory overflow. </span></span>Opt.injustdecodebounds =<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">; Bitmapfactory.decodefile (</span></span>"sdcard/dog.jpg", opt);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the method returns null</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get the picture width high</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>ImageWidth =<span style="color: #000000;"><span style="color: #000000;">opt.outwidth; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>ImageHeight =<span style="color: #000000;"><span style="color: #000000;">opt.outheight; Display DP</span></span>=<span style="color: #000000;"><span style="color: #000000;">Getwindowmanager (). Getdefaultdisplay (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get the screen width high</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>ScreenWidth =<span style="color: #000000;"><span style="color: #000000;">Dp.getwidth (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>ScreenHeight =<span style="color: #000000;"><span style="color: #000000;">Dp.getheight (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Calculating the zoom ratio</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Scale = 1<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>ScaleWidth = imagewidth/screenwidth;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the width of the picture divided by the width of the screen</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>ScaleHeight = imageheight/screenheight;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the height of the picture divided by the height of the screen</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(scalewidth >= scaleheight && scalewidth >= 1<span style="color: #000000;"><span style="color: #000000;">) {</span> scale</span>= scalewidth;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">which</span> one is the largest scale?</span><span style="color: #000000;"><span style="color: #000000;"> } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(scalewidth < ScaleHeight && ScaleHeight >= 1<span style="color: #000000;"><span style="color: #000000;">) {</span> scale</span>=<span style="color: #000000;"><span style="color: #000000;">scaleheight; } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set the zoom ratio</span></span>Opt.insamplesize =<span style="color: #000000;">scale <span style="color: #000000;">; Opt.injustdecodebounds</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span>;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">setting false will load the image into memory instead of just loading the image information,</span></span>Bitmap BM = Bitmapfactory.decodefile ("sdcard/dog.jpg", opt);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">BM is the image after zooming</span></span><span style="color: #000000;"><span style="color: #000000;">ImageView IV</span></span>=<span style="color: #000000;"><span style="color: #000000;">(ImageView) Findviewbyid (r.id.iv); Iv.setimagebitmap (bm); } }</span></span></pre><p><p></p></p><pre><pre><span style="color: #000000;">#多媒体概念</span>*<span style="color: #000000;"> text, pictures, audio, Video # computer picture size calculation</span> <span style="color: #ff0000;">picture size = Total pixels of picture * the size of each pixel</span> * Monochrome graph: each pixel represents a color, then 1 bits can represent 0-1, Each pixel occupies 1/<span style="color: #000000;">8 bytes, and the picture size equals the number of pixels of the picture multiplied by the size of each pixel. </span> <span style="color: #ff0000;">16 Color map: each pixel has 16 colors, then 4 bits 0000-1111 can be represented, that is, 4 bits, a byte 8 bits, each pixel occupies 1/2 bytes, the picture size is equal to the image number of pixels multiplied by the size of each pixel. </span>* 256 color map: each pixel has 256 colors, then 8 bits can represent the 00000000-11111111<span style="color: #000000;">, each pixel occupies 1 bytes, the picture size is equal to the image number of pixels multiplied by the size of each pixel. </span>* 24-bit graphs: each pixel occupies 24 bits and 3 bytes, and the picture size equals the number of pixels of the picture multiplied by the size of each pixel.</pre></pre><p><p></p></p><p><p>ANDROID95 Zoom Load Large picture</p></p></span>