有誰知道如何將圖像\位圖裁剪成圓形? 我找不到任何解決方案,抱歉..在Android中將圖像裁剪爲圓形
回答
與下面的代碼試試:
public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
// TODO Auto-generated method stub
int targetWidth = 50;
int targetHeight = 50;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
targetHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1)/2,
((float) targetHeight - 1)/2,
(Math.min(((float) targetWidth),
((float) targetHeight))/2),
Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth,
targetHeight), null);
return targetBitmap;
}
不工作。請建議我。 – Arun
類:
public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
int targetWidth = 50;
int targetHeight = 50;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
targetHeight,Bitmap.Config.ARGB_8888);
canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1)/2,
((float) targetHeight - 1)/2,
(Math.min(((float) targetWidth),
((float) targetHeight))/2),
Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth,
targetHeight), null);
return targetBitmap;
}
查看:
<ImageView
android:id="@+id/imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btnEdit"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:background="@drawable/rounded"
android:adjustViewBounds="true"
android:gravity="center"
android:src="@drawable/happy"/>
其他樣式:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@android:color/white" />
<stroke
android:width="3dip"
android:color="#FF0000" />
<corners android:radius="10dp" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
Romain Guy,以前是谷歌Android團隊的工程師,發表了一篇關於drawing images with rounded corners的優秀文章。這個想法可以很容易地擴展到一個圓,例如,通過改變圓角的矩形半徑,以便創建一個完整的圓。
從文章:
要生成圓形圖片,我只是寫了一個定製
Drawable
是 繪製使用Canvas.drawRoundRect()
一個圓角矩形。竅門是 使用Paint
與BitmapShader
填充圓角矩形 紋理而不是簡單的顏色。下面是代碼的樣子:BitmapShader shader; shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setShader(shader); RectF rect = new RectF(0.0f, 0.0f, width, height); // rect contains the bounds of the shape // radius is the radius in pixels of the rounded corners // paint contains the shader that will texture the shape canvas.drawRoundRect(rect, radius, radius, paint);
Canvas也暴露了一個[drawCircle](http://developer.android.com/reference/android/graphics/Canvas.html)方法,它可能更適合這種情況。 – greg7gkb
finalBitmapShader shader = newBitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(shader);
mBitmapWidth=mBitmap.getWidth();
mBitmapHeight=mBitmap.getHeight();
}
@Override
public void draw(Canvas canvas{
canvas.drawOval(mRectF,mPaint);
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
mRectF.set(bounds);
}
在這裏我找到了一個示例教程在 http://androidgreeve.blogspot.in/2014/09/facebook-messanger-like-profile-image.html?m=1
懷斯曼設計,有一個開放源碼的通知ImageView的準備使用
https://github.com/wisemandesigns/CircularImageView
這將在您的佈局中使用XML,它讓生活更輕鬆。你可以用XML來設置源代碼,或者稍加修改就可以很容易地使用Bitmap。
聲明:我懷斯曼工作設計
對於有圓角的ImageView的,將您的圖像轉換成位圖,然後嘗試下面的代碼:
private Bitmap getRoundedCroppedBitmap(Bitmap bitmap) {
int widthLight = bitmap.getWidth();
int heightLight = bitmap.getHeight();
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Paint paintColor = new Paint();
paintColor.setFlags(Paint.ANTI_ALIAS_FLAG);
RectF rectF = new RectF(new Rect(0, 0, widthLight, heightLight));
canvas.drawRoundRect(rectF, widthLight/2 ,heightLight/2,paintColor);
Paint paintImage = new Paint();
paintImage.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
canvas.drawBitmap(bitmap, 0, 0, paintImage);
return output;
}
- 1. 圓形裁剪圖像
- 2. 如何裁剪圓形圖像openvc android
- 3. 將圖像裁剪成圓形
- 4. 如何將圖像裁剪成圓形?
- 5. 將圖像裁剪爲正方形 - Android
- 6. CSS圓形裁剪矩形圖像
- 7. imagemagick中的圖像圓形裁剪
- 8. 在Android中將很多圖片裁剪爲圓形
- 9. 如何在Android中以圓形裁剪圖像?
- 10. 從原始UIImage中裁剪圓形或橢圓形圖像
- 11. Android位圖裁剪橢圓形
- 12. 將方形圖像裁剪成圓形 - 以編程方式
- 13. 在android中裁剪圖像
- 14. 在android中裁剪圖像
- 15. Bootstrap 4 - 將圖像裁剪成圓形 - 風景照片未被正確裁剪
- 16. 如何在Android中裁剪橢圓形或面部形狀的圖像?
- 17. 裁剪android圖像
- 18. 如何在Titanium中以圓形裁剪圖像?
- 19. SVG - 如何將圖像裁剪成圓形?
- 20. 將圖像裁剪成圓形並添加邊框
- 21. 不能將圖像裁剪成真實的圓形
- 22. 如何爲滑動的圓形裁剪圖像製作邊框
- 23. android中的圖像裁剪
- 24. Android位圖:居中裁剪+創建位圖的圓形
- 25. 如何將圖像裁剪爲方形 - android?
- 26. 在matlab中裁剪正方形圖像
- 27. 將圖像裁剪爲矢量或覆蓋圖形的形狀
- 28. 如何在Android中的矩形內裁剪圓形路徑
- 29. 在圖像視圖中通過繪製圓來裁剪圖像
- 30. 如何將圖像裁剪爲最大圖像矩形?
@ user1281750閱讀問題詳細準確!它不重複任何方式! –
可能的重複[裁剪圓形區域從位圖在Android](http://stackoverflow.com/questions/11932805/cropping-circular-area-from-bitmap-in-android) – Adinia