
Bitmap src = BitmapFactory.decodeResource(getResources(), imageId); //獲取Bitmap圖片RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), src); //創建RoundedBitmapDrawable對象roundedBitmapDrawable.setCornerRadius(100); //設置圓角半徑(根據實際需求)roundedBitmapDrawable.setAntiAlias(true); //設置反走樣image.setImageDrawable(roundedBitmapDrawable); //顯示圓角圖片
動態
生成圓形圖片
由于RoundedBitmapDrawable類沒有直接提供生成圓形圖片的方法,所以生成圓形圖片首先需要對原始圖片進行裁剪,將圖片裁剪成正方形,最后再生成圓形圖片,具體實現如下:
Bitmap src = BitmapFactory.decodeResource(getResources(), imageId);Bitmap dst;//將長方形圖片裁剪成正方形圖片if (src.getWidth() >= src.getHeight()){dst = Bitmap.createBitmap(src, src.getWidth()/2 - src.getHeight()/2, 0, src.getHeight(), src.getHeight());}else{dst = Bitmap.createBitmap(src, 0, src.getHeight()/2 - src.getWidth()/2, src.getWidth(), src.getWidth());}RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), dst);roundedBitmapDrawable.setCornerRadius(dst.getWidth() / 2); //設置圓角半徑為正方形邊長的一半roundedBitmapDrawable.setAntiAlias(true);image.setImageDrawable(roundedBitmapDrawable);以上所述是小編給大家介紹的使用RoundedBitmapDrawable生成圓角圖片的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答