在我的应用程序中,我从图库中获取图像,并且该图像的形状位于正方形中我想将该图像设置为imageView,然后它应该是椭圆形。就我而言,我需要像人脸一样剪切图像。任何人都可以提前告诉我该怎么做。如何将方形图像转换为椭圆形
0
A
回答
5
使用以下类代替图像视图。
RoundedCornerImageView imageView1;
imageView1.setRadius(10);
这将使图像半径增加10px,您可以给出想要的wat值并将其设置为您想要的形状。试试。
一切顺利:)
public class RoundedCornerImageView extends ImageView {
private int radius = 10;
public RoundedCornerImageView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
Path clipPath = new Path();
int w = this.getWidth();
int h = this.getHeight();
clipPath.addRoundRect(new RectF(0, 0, w, h), radius, radius, Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
}
public RoundedCornerImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public RoundedCornerImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setRadius(int radius){
this.radius = radius;
this.invalidate();
}
}
4
您可以使用此
public Drawable getRoundedCornerImage(Drawable bitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable)bitmapDrawable).getBitmap();
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());
final RectF rectF = new RectF(rect);
final float roundPx = 100;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
Drawable image = new BitmapDrawable(output);
return image;
}
希望这有助于你
-1
您也可以使用此或Download demo code example
public class Shape {
private Bitmap bmp;
private ImageView img;
public Shape(Bitmap bmp, ImageView img) {
this.bmp=bmp;
this.img=img;
onDraw();
}
private void onDraw(){
Canvas canvas=new Canvas();
if (bmp.getWidth() == 0 || bmp.getHeight() == 0) {
return;
}
int w = bmp.getWidth(), h = bmp.getHeight();
Bitmap roundBitmap = getOvalCroppedBitmap(bmp, w);
img.setImageBitmap(roundBitmap);
}
public static Bitmap getOvalCroppedBitmap(Bitmap bitmap, int radius) {
Bitmap finalBitmap;
if (bitmap.getWidth() != radius || bitmap.getHeight() != radius)
finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius,
false);
else
finalBitmap = bitmap;
Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(),
finalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Paint paint = new Paint();
final Rect rect = new Rect(0, 0, finalBitmap.getWidth(),
finalBitmap.getHeight());
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
RectF oval = new RectF(0, 0, 130, 150);
canvas.drawOval(oval, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(finalBitmap, rect, oval, paint);
return output;
}
最后,在您的主要活动中,实例化您的类Shape并将两个参数传递给您的类。要裁剪成椭圆形的图像以及将设置最终图像的图像视图。
相关问题
- 1. 转换多边形椭圆
- 2. 将方形图像转换为矩形
- 3. PHP - 如何将矩形图像转换为方形图像?
- 4. 转换的水平力,以椭圆形
- 5. iOS:将视图转换为圆柱形
- 6. 将一组图像旋转成椭圆形
- 7. 从原始UIImage中裁剪圆形或椭圆形图像
- 8. 为什么matplotlib将我的圆形绘制为椭圆形?
- 9. 如何将矩形图像设置为圆形图像
- 10. 椭圆形的作物图像
- 11. Java插入椭圆形图像
- 12. 如何将椭圆变换为Java中的矩形?
- 13. 将椭圆的绘制点旋转为圆形
- 14. CSS或jQuery/JavaScript椭圆形/圆形方形进度条
- 15. 将圆角矩形变换为圆形
- 16. Java:如何用导入的PNG文件替换“椭圆形”或“矩形”图形?
- 17. Pyqtgraph:如何绘制椭圆或圆形
- 18. 在矩形内创建椭圆/圆形
- 19. 弯曲线形成圆形和椭圆
- 20. 如何将矩形选择转换为圆形(在网格中)
- 21. 如何将Raphael中的多边形转换为圆形?
- 22. View.setX正在将我的圆形背景转换为方形
- 23. CSS div椭圆形
- 24. OpenGL圆形绘图变椭圆
- 25. 将图像转换为图形在c#
- 26. 如何绘制形状像椭圆形,线和手势
- 27. 是否有可能将UIView转换为圆形图像?
- 28. 旋转圆形图像
- 29. 旋转圆形图像
- 30. 如何检查椭圆形是否触及任何其他椭圆形VB
的可能的复制[如何裁剪在椭圆形或Android的面罩形状图像?](http://stackoverflow.com/questions/15200214/how-to-crop-image-in-oval字形或 - 兼具口罩的形状在-机器人) – bummi