我想從原始圖像裁剪一個圓形圖像。我正在使用Picasso庫進行圖像顯示。試過http://yasiradnan.com/circle-transformation-with-android-image-downloading-and-caching-library-picasso/,但它只是將整個圖像轉換成一個圓形,所以圖像變形了。我不想轉換圖像,我只想用圓形裁剪圖像。將圖像裁剪成圓形
Q
將圖像裁剪成圓形
1
A
回答
4
要完成您想要做的事情,您可以繼承ImageView
並使其實現Picasso
的Target
接口。加載位圖時,只需使用將位圖居中放置爲正方形的方法,然後將圖像着色爲圓形。例如:
public class ImageViewTarget extends ImageView implements Target {
//constructors
@Override
public void onBitmapFailed(Drawable drawable) {
//TODO
}
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom loadFrom) {
bitmap = cropCircle(bitmap.isMutable() ? bitmap : bitmap.copy(Config.ARGB_8888, true));
setImageBitmap(bitmap);
}
@Override
public void onPrepareLoad(Drawable arg0) {
//TODO
}
public Bitmap cropCricle(Bitmap bm){
int width = bm.getWidth();
int height = bm.getHeight();
Bitmap cropped_bitmap;
/* Crop the bitmap so it'll display well as a circle. */
if (width > height) {
cropped_bitmap = Bitmap.createBitmap(bm,
(width/2) - (height/2), 0, height, height);
} else {
cropped_bitmap = Bitmap.createBitmap(bm, 0, (height/2)
- (width/2), width, width);
}
BitmapShader shader = new BitmapShader(cropped_bitmap, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
height = cropped_bitmap.getHeight();
width = cropped_bitmap.getWidth();
Bitmap mCanvasBitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mCanvasBitmap);
canvas.drawCircle(width/2, height/2, width/2, paint);
return mCanvasBitmap;
}
}
有可能是一個更好的,爲什麼來處理cropCircle(Bitmap bitmap);
方法,但上述作品有時優化/蓄客的。
2
您可以使用下面的代碼獲得圓角位圖...這可能對你有幫助....
private Bitmap getRoundedCroppedImage(Bitmap bmp) {
int widthLight = bmp.getWidth();
int heightLight = bmp.getHeight();
Bitmap output = Bitmap.createBitmap(widthLight, heightLight,Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Paint paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
RectF rectF = new RectF(new Rect(0, 0, widthLight, heightLight));
canvas.drawRoundRect(rectF, widthLight/2 ,heightLight/2,paint);
Paint paintImage = new Paint();
paintImage.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
canvas.drawBitmap(bmp, 0, 0, paintImage);
return output;
}
謝謝...
+0
謝謝...親愛的...這就是減少我的努力... –
0
有一些決定,我做了基於在that answer 您可以自定義的ImageView沒有庫
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class CircleImageView extends ImageView {
public CircleImageView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
}
@Override
public void setImageDrawable(Drawable aDrawable) {
Bitmap bitmap=getCircleCroppedBitmap(((BitmapDrawable) aDrawable).getBitmap());
super.setImageDrawable(new BitmapDrawable(getResources(),bitmap));
}
private static Bitmap getCircleCroppedBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
canvas.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2,
bitmap.getWidth()/2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
//Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
//return _bmp;
return output;
}
}
相關問題
- 1. 如何將圖像裁剪成圓形?
- 2. 圓形裁剪圖像
- 3. 將方形圖像裁剪成圓形 - 以編程方式
- 4. CSS圓形裁剪矩形圖像
- 5. Bootstrap 4 - 將圖像裁剪成圓形 - 風景照片未被正確裁剪
- 6. SVG - 如何將圖像裁剪成圓形?
- 7. 將圖像裁剪成圓形並添加邊框
- 8. 不能將圖像裁剪成真實的圓形
- 9. 如何裁剪圓形圖像openvc android
- 10. imagemagick中的圖像圓形裁剪
- 11. 從原始UIImage中裁剪圓形或橢圓形圖像
- 12. 在Android中將圖像裁剪爲圓形
- 13. Android位圖裁剪橢圓形
- 14. 將圖像裁剪爲正方形 - Android
- 15. OpenCV裁剪圖像與橢圓
- 16. 圓角裁剪圖像使用jquery
- 17. 將圖像製成圓形
- 18. 在iOS中將視頻裁剪成圓形?
- 19. 如何執行圓形圖像裁剪功能
- 20. 圖像剪裁以適應圓形容器
- 21. 如何在Titanium中以圓形裁剪圖像?
- 22. Node-gm圓形圖像裁剪使用Imagemagick
- 23. 如何在Android中以圓形裁剪圖像?
- 24. 如何爲滑動的圓形裁剪圖像製作邊框
- 25. CSS/JS用於圖像的圓形裁剪
- 26. 如何將圖像裁剪成正方形
- 27. 圓角裁剪
- 28. 裁剪圖像
- 29. 裁剪圖像
- 30. 裁剪圖像
我不知道如何使用畢加索,但是這可能會幫助您: http://stackoverflow.com/questions/12944275/crop-image-as-circle-in-android – raybaybay