如何在Android中为ImageView设置边框并更改其颜色?
当前回答
我差点就放弃了。
这是我使用滑动加载图像的情况,请参阅关于圆角转换和这里的详细滑动问题
我的ImageView也有相同的属性,每个人都回答这里1,这里2,这里3
android:cropToPadding="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter"`
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/path_to_rounded_drawable"
但还是没有成功。
经过一段时间的研究,使用前景属性从这个SO回答这里给出一个结果android:前景="@drawable/all_round_border_white"
不幸的是,它给我“不太好”的边界角如下图:
其他回答
上面已经使用过,但没有专门提到。
setCropToPadding(boolean);
如果为真,图像将被裁剪以适合它的填充。
这将使ImageView源与背景填充相匹配。
通过XML它可以做到如下-
android:cropToPadding="true"
我发现这样做容易得多:
1)编辑帧内有内容(用9patch工具)。
2)将ImageView放置在线性布局中,并设置你想要的框架背景或颜色作为线性布局的背景。当你设置帧本身有内容时,你的ImageView将在帧内(就在你用9patch工具设置内容的地方)。
首先添加你想要的背景色作为你的边界的颜色,然后
更改cropToPadding为true,然后添加填充。
然后你就有了imageView的边框。
对于那些正在搜索自定义边界和形状的ImageView。你可以使用android-shape-imageview
只需添加compile 'com.github.siyamed:android-shape-imageview:0.9。+@aar'到build.gradle。
并使用在你的布局。
<com.github.siyamed.shapeimageview.BubbleImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/neo"
app:siArrowPosition="right"
app:siSquare="true"/>
使用Material design和新的ShapeableImageView小部件,您可以使用strokeColor和strokeWidth属性轻松创建具有边框的图像。这很简单,不需要创建任何额外的XML文件。
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="200dp"
android:layout_height="200dp"
app:strokeColor="@color/black"
app:strokeWidth="2dp"
app:srcCompat="@drawable/sample_image" />
上面的代码创建了一个宽度为2dp的黑色边框。
如果你想给圆形图像添加描边,你可以使用shapeAppearanceOverlay属性。这允许您使用所提供的形状(在本例中为圆形)绘制位图。查看下面的代码了解更多细节:
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="200dp"
android:layout_height="200dp"
app:shapeAppearanceOverlay="@style/circleImageView"
app:srcCompat="@drawable/sample_image"
app:strokeColor="@color/black"
app:strokeWidth="2dp" />
将下面的代码添加到你的styles.xml文件中:
<!-- Circle Shape -->
<style name="circleImageView" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
确保你的应用为了使用ShapeableImageView扩展了材质设计主题。
推荐文章
- 视图绑定-我如何获得包含布局的绑定?
- 在Android Studio中改变矢量资产的填充颜色
- 在构建中编写注释的语法是什么?gradle文件?
- 如何以编程方式添加按钮色调
- 用Android Studio进行调试永远停留在“等待调试器”状态
- Openssl不被视为内部或外部命令
- 无法执行dex:在Eclipse中超过GC开销限制
- 如何以编程方式将视图添加到视图
- 单击url会打开默认浏览器
- 使用Retrofit刷新OAuth令牌,而不修改所有调用
- 多个dex文件定义了landoid /support/v4/accessibilityservice/AccessibilityServiceInfoCompat
- 如何获得动作栏的高度?
- 从活动外部调用startActivity() ?
- createScaledBitmap的过滤器参数做什么?
- 为什么我在使用adb时访问数据文件夹被拒绝?