1. 程式人生 > >對圖像進行壓縮再進行圓形處理

對圖像進行壓縮再進行圓形處理

pic AS ant source ans ide 分享 float 畫布

①壓縮處理

②圓形處理

技術分享圖片

 1 <RelativeLayout
 2     android:id="@+id/rl_me_icon"
 3     android:layout_width="wrap_content"
 4     android:layout_height="wrap_content"
 5     android:layout_marginTop="4dp"
 6     android:layout_centerHorizontal="true">
 7 
 8     <ImageView
 9         android:layout_width="
64dp" 10 android:layout_height="64dp" 11 android:scaleType="fitXY" 12 android:src="@drawable/my_user_bg_icon" /> 13 14 <ImageView 15 android:id="@+id/iv_me_icon" 16 android:layout_width="62dp" 17 android:layout_height="62dp" 18 android:layout_centerInParent="
true" 19 android:scaleType="fitXY" 20 android:src="@drawable/my_user_default" /> 21 </RelativeLayout>

 1 @Bind(R.id.iv_me_icon)
 2 ImageView ivMeIcon;
 3 
 4 //1.讀取本地保存的用戶信息
 5 User user = ((BaseActivity) this.getActivity()).readUser();
 6 
 7 //使用Picasso聯網獲取圖片
 8 Picasso.with(this
.getActivity()).load(user.getImageurl()).transform(new Transformation() { 9 @Override 10 public Bitmap transform(Bitmap source) {//下載以後的內存中的bitmap對象 11 //壓縮處理 12 Bitmap bitmap = BitmapUtils.zoom(source, UIUtils.dp2px(62),UIUtils.dp2px(62)); 13 //圓形處理 14 bitmap = BitmapUtils.circleBitmap(bitmap); 15 //回收bitmap資源 16 source.recycle(); 17 return bitmap; 18 } 19 20 @Override 21 public String key() { 22 return "";//需要保證返回值不能為null。否則報錯 23 } 24 }).into(ivMeIcon);

 1 public class BitmapUtils {
 2 
 3     public static Bitmap circleBitmap(Bitmap source) {
 4         //獲取Bitmap的寬度
 5         int width = source.getWidth();
 6         //以Bitmap的寬度值作為新的bitmap的寬高值。
 7         Bitmap bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
 8         //以此bitmap為基準,創建一個畫布
 9         Canvas canvas = new Canvas(bitmap);
10         Paint paint = new Paint();
11         paint.setAntiAlias(true);
12         //在畫布上畫一個圓
13         canvas.drawCircle(width / 2, width / 2, width / 2, paint);
14 
15         //設置圖片相交情況下的處理方式
16         //setXfermode:設置當繪制的圖像出現相交情況時候的處理方式的,它包含的常用模式有:
17         //PorterDuff.Mode.SRC_IN 取兩層圖像交集部分,只顯示上層圖像
18         //PorterDuff.Mode.DST_IN 取兩層圖像交集部分,只顯示下層圖像
19         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
20         //在畫布上繪制bitmap
21         canvas.drawBitmap(source, 0, 0, paint);
22 
23         return bitmap;
24 
25     }
26 
27     //實現圖片的壓縮處理
28     //設置寬高必須使用浮點型,否則導致壓縮的比例:0
29     public static Bitmap zoom(Bitmap source,float width ,float height){
30 
31         Matrix matrix = new Matrix();
32         //圖片的壓縮處理
33         matrix.postScale(width / source.getWidth(),height / source.getHeight());
34 
35         Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, false);
36         return bitmap;
37     }
38 
39 }

對圖像進行壓縮再進行圓形處理