Rounded corners for an Imageview in android(android中Imageview的圆角)
问题描述
我在线性布局中有一个 textview 和 imageview.Textview 在顶部,imageview 在底部.我使用下面的线为线性布局设置圆角.但是 imageview 的角不是圆角的.我看到只有线性布局的顶角是四舍五入的.我怎样才能使 imageview 的底角变圆?(如果我删除 imageview,我看到所有的角都是圆角的)
I have a textview and imageview inside a linearlayout. Textview is at top and imageview at bottom. I used below lines to have rounded corners for linearlayout. But imageview corners are not rounding. I see only top corners of linearlayout are rounding. How can i have rounded bottom corners of imageview? ( I see all the corners are rounded if i remove imageview)
rounded_corners.xml
rounded_corners.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffff" />
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
</shape>
main.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:background="@xml/rounded_corners"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="xxxxxxxx" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/my_image_view" />
</LinearLayout>
屏幕截图:
推荐答案
可以把图片的左下角和右下角弄圆,像这样:
you can make the image's left bottom and right bottom corner rounded,like this:
代码:
public static Bitmap getRoundCornerBitmap(Bitmap bitmap, int radius) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Bitmap output = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
final RectF rectF = new RectF(0, 0, w, h);
canvas.drawRoundRect(rectF, radius, radius, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, null, rectF, paint);
/**
* here to define your corners, this is for left bottom and right bottom corners
*/
final Rect clipRect = new Rect(0, 0, w, h - radius);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
canvas.drawRect(clipRect, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, null, rectF, paint);
bitmap.recycle();
return output;
}
这种方法可以给你一个左下角和右下角圆角的图像.
this method can give you a image with left bottom and right bottom corner rounded.
这篇关于android中Imageview的圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:android中Imageview的圆角
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01