How to use Picasso to download cache for Android images

Source: Internet
Author: User
<span id="Label3"></p><p><p>Compared to other, Picasso's picture cache is simpler, He only needs a line of code to express: <span style="color: #ff0000;">import the relevant jar package</span></p></p><pre><pre>Picasso.with (context). Load (<span style="color: #800000;">"</span> <span style="color: #800000;">picture path</span> <span style="color: #800000;">"</span>). into (imageview control);</pre></pre><p><p>ListView loading pictures are very common in the current app, we often have to deal with pictures loading slowly, dislocation and other common problems. Picasso not only realizes the asynchronous loading of images, but also solves and optimizes some problems.</p></p><p><p>For Example: 1. When in the listview, sliding, we need to adapter in the field is no longer the view of the ImageView image resources to recycle, or there will be a picture Dislocation.</p></p><p><p>2. Convert picture compression to reduce memory consumption,</p></p><pre><pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">void</span> getView (<span style="color: #0000ff;">int</span> <span style="color: #000000;"> position, View convertview, viewgroup parent) { </span>=<span style="color: #000000;"> ( Squaredimageview) convertview; </span> <span style="color: #0000ff;">if</span> <span style="color: #0000ff;">NULL</span> <span style="color: #000000;">) { </span><span style="color: #0000ff;">new</span> <span style="color: #000000;"> squaredimageview (context); } </span> =<span style="color: #000000;"> GetItem (position); Picasso.with (context). load (url). into (view);</span></pre></pre><p><p>It's also possible to do a picture conversion, which is actually cropped, and I think</p></p><pre><pre><span style="color: #000000;">picasso.with (context). Load (url). <span style="color: #ff0000;">Resize (</span> </span> <span style="color: #ff0000;">the</span> <span style="color: #000000;"> <span style="color: #ff0000;">)</span>. Centercrop (). into (imageView);</span></pre></pre><pre><span style="color: #000000;"><span style="color: #000000;">Picasso. </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create a Picasso object</span></span><span style="color: #000000;">with <span style="color: #000000;">(context). </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">load url, This method has many overloads, you can try it yourself</span></span><span style="color: #000000;"><span style="color: #000000;">Load (url). </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">read local file not cached</span></span><span style="color: #000000;"><span style="color: #000000;">Memorypolicy (memorypolicy.no_cache). </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">read network files are not cached</span></span><span style="color: #000000;"><span style="color: #000000;">Networkpolicy (networkpolicy.no_cache). </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">placeholder picture before the picture is displayed</span></span><span style="color: #000000;"><span style="color: #000000;">placeholder (r.drawable.drawer_avatar_default). </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Reset the width, used here from the Dimen file, recommended this way, you can also set the PX value directly</span></span><span style="color: #000000;"><span style="color: #000000;">Resize (context.getresources (). getdimensionpixelsize (r.dimen.space_64dp), context.getresources (). Getdimensionpixelsize (r.dimen.space_64dp)). </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Center display with wide height reset by previous step</span></span><span style="color: #000000;"><span style="color: #000000;">Centercrop (). </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">use a custom transform, here is the round avatar effect</span></span>Transform<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">circletransformation ()). </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">do not fade in, direct display</span></span><span style="color: #000000;"><span style="color: #000000;">Nofade (). </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Target ImageView</span></span>Into (target);</pre><p class="p"><p class="p">Conversion class for custom circles</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Circletransformation implements Transformation {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span>Final<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Stroke_width =<span style="color: #800080;"><span style="color: #800080;">6</span></span><span style="color: #000000;"><span style="color: #000000;">; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Bitmap Transform (Bitmap source) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Size =<span style="color: #000000;"><span style="color: #000000;">math.min (source.getwidth (), source.getheight ()); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>x = (source.getwidth ()-size)/<span style="color: #800080;"><span style="color: #800080;">2</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>y = (source.getheight ()-size)/<span style="color: #800080;"><span style="color: #800080;">2</span></span><span style="color: #000000;"><span style="color: #000000;">; Bitmap Squaredbitmap</span></span>=<span style="color: #000000;"><span style="color: #000000;">bitmap.createbitmap (source, x, y, size, size); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(squaredbitmap! =<span style="color: #000000;"><span style="color: #000000;">Source) {source.recycle (); } Bitmap Bitmap</span></span>=<span style="color: #000000;"><span style="color: #000000;">bitmap.createbitmap (size, size,source.getconfig ()); Canvas Canvas</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Canvas (bitmap); Paint Avatarpaint</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Paint (); Bitmapshader Shader</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Bitmapshader (squaredbitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); Avatarpaint.setshader (shader); Paint Outlinepaint</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Paint (); Outlinepaint.setcolor (color.white); Outlinepaint.setstyle (Paint.Style.STROKE); Outlinepaint.setstrokewidth (stroke_width); Outlinepaint.setantialias (<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>r = size/<span style="color: #000000;"><span style="color: #000000;">2f; Canvas.drawcircle (r, r, r, avatarpaint); Canvas.drawcircle (r, r, R</span></span>-stroke_width/<span style="color: #800080;"><span style="color: #800080;">2</span></span><span style="color: #000000;"><span style="color: #000000;">, outlinepaint); Squaredbitmap.recycle (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">bitmap; } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">String key () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">circletransformation ()</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>; }</pre><p><p></p></p><span style="font-size: 14px; font-family: ‘宋体‘; color: #000000;"><span style="font-size: 14px; font-family: ‘宋体‘; color: #000000;"><br></span></span><p><p>How to use Picasso to download cache for Android images</p></p></span>

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.