O2-開源框架使用之Picasso
零、前言:
個人感覺Picasso還是非常好用的,它是圖片載入的類庫
1.依賴
implementation 'com.squareup.picasso:picasso:2.71828'
2.如果是請求網路圖片:記得許可權
<uses-permission android:name="android.permission.INTERNET"/>
3.如果是SD卡圖片:記得許可權、及執行時處理
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
一、簡單使用:

載入圖片.png
1.載入網路圖片資源: ofollow,noindex">http://www.toly1994.com:8080/imgs/HXqqASHJETSlvpnc.jpg
Picasso.get() .load("http://www.toly1994.com:8080/imgs/HXqqASHJETSlvpnc.jpg") .into(mIdIdImg);
這裡順便說一句:Picasso網路載入預設是快取到本地的:在data/data/本包/cache下

圖片快取.png
2.左上角指示器:
怎麼知道圖片從哪載入的呢?
Picasso.get().setIndicatorsEnabled(true);
圖片的左上角三角形顏色:
藍色,磁碟載入---紅色,網路載入---綠色,記憶體載入
在載入這張圖片,可見是使用快取載入的,也就是磁碟。
如果把快取檔案刪掉,就是紅色。別關閉在點一下載入就是綠色,也就是快取
Picasso預設實現三級快取,真的很棒的感覺。

載入圖片2.png
3.載入res圖片資源:
這個應該很熟了:
Picasso.get().setIndicatorsEnabled(true); Picasso.get() .load(R.mipmap.wql)//資原始檔 .into(mIdIdImg);
4.載入assets圖片資源
Picasso.get().setIndicatorsEnabled(true); Picasso.get() .load("file:///android_asset/wql.jpg")//assets .into(mIdIdImg);
5.載入檔案圖片
Picasso.get().setIndicatorsEnabled(true); Picasso.get() .load(new File("/data/data/com.toly1994.picassotest/cache/picasso-cache/8da4f93c94072639f4e0ea4a8b6c2df1.1"))//檔案 .into(mIdIdImg);
二、Picasso圖片處理
1.重置大小:resize(W, H)
預設單位是px
Picasso.get().setIndicatorsEnabled(true); Picasso.get() .load(R.mipmap.wql)//資原始檔 .resize(200,150) .into(mIdIdImg);

載入圖片3.png
2.自定義裁切:
原型剪裁沒有什麼難度:搞個星星剪裁吧:更多形狀,可見 我的LogicCanvas庫簡單使用:

剪裁圖片.png
設定剪裁:
Picasso.get().setIndicatorsEnabled(true); Picasso.get() .load(R.mipmap.wql)//資原始檔 .transform(new StarTransformation(8,500,300)) .into(mIdIdImg);
裁剪類:StarTransformation
/** * 作者:張風捷特烈<br/> * 時間:2018/8/30 0030:21:44<br/> * 郵箱:[email protected]<br/> * 說明:星星裁剪類 */ public class StarTransformation implements Transformation { private static final int STROKE_WIDTH = 6; private int num; private float R; private float r; public StarTransformation(int num, float R, float r) { this.num = num; this.R = R; this.r = r; } @Override public Bitmap transform(Bitmap source) { int size = Math.min(source.getWidth(), source.getHeight()); int x = (source.getWidth() - size) / 2; int y = (source.getHeight() - size) / 2; Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size); if (squaredBitmap != source) { source.recycle(); } Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig()); Canvas canvas = new Canvas(bitmap); Paint avatarPaint = new Paint(); BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); avatarPaint.setShader(shader); Paint outlinePaint = new Paint(); outlinePaint.setColor(Color.WHITE); outlinePaint.setStyle(Paint.Style.STROKE); outlinePaint.setStrokeWidth(STROKE_WIDTH); outlinePaint.setAntiAlias(true); //float r = size / 2f; //canvas.drawCircle(r, r, r, avatarPaint); canvas.drawPath(nStarPath(num,R,r), avatarPaint); canvas.drawPath(nStarPath(num,R,r), outlinePaint); squaredBitmap.recycle(); return bitmap; } private Path nStarPath(int num,float R,float r) { Path path = new Path(); float perDeg = 360 / num; float degA = perDeg / 2 / 2; float degB = 360 / (num - 1) / 2 - degA / 2 + degA; path.moveTo((float) (Math.cos((degA + perDeg * 0) / 180 * Math.PI) * R + R * Math.cos(degA / 180 * Math.PI)), (float) (-Math.sin((degA + perDeg * 0) / 180 * Math.PI) * R + R)); for (int i = 0; i < num; i++) { path.lineTo((float) (Math.cos((degA + perDeg * i) / 180 * Math.PI) * R + R * Math.cos(degA / 180 * Math.PI)), (float) (-Math.sin((degA + perDeg * i) / 180 * Math.PI) * R + R)); path.lineTo((float) (Math.cos((degB + perDeg * i) / 180 * Math.PI) * r + R * Math.cos(degA / 180 * Math.PI)), (float) (-Math.sin((degB + perDeg * i) / 180 * Math.PI) * r + R)); } path.close(); return path; } @Override public String key() { return "circleTransformation()"; } }
3.其他配置:
.memoryPolicy(MemoryPolicy.NO_CACHE)//本地檔案不快取 .networkPolicy(NetworkPolicy.NO_CACHE)//讀取網路檔案不快取 .placeholder(R.drawable.ic_launcher_background)//圖片顯示之前的佔位圖片 .error(R.mipmap.ic_launcher)//錯誤圖片 .noFade()//不淡入淡出, 直接顯示
後記、
1.宣告:
[1]本文由張風捷特烈原創,轉載請註明
[2]歡迎廣大程式設計愛好者共同交流
[3]個人能力有限,如有不正之處歡迎大家批評指證,必定虛心改正
[4]你的喜歡與支援將是我最大的動力
2.連線傳送門:
更多安卓技術歡迎訪問:安卓技術棧 我的github地址:歡迎star 簡書首發,騰訊雲+社群同步更新 張風捷特烈個人網站,程式設計筆記請訪問: http://www.toly1994.com
3.聯絡我
QQ:1981462002
微信:zdl1994328
4.歡迎關注我的微信公眾號,最新精彩文章,及時送達:

公眾號.jpg