如何在Android中为ImageView设置边框并更改其颜色?
当前回答
以下是我曾经有黑色边框的代码。注意,我没有使用额外的xml文件为边界。
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/red_minus_icon"
android:background="#000000"
android:padding="1dp"/>
其他回答
你必须在res/drawable这段代码中创建一个background.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners android:radius="6dp" />
<stroke
android:width="6dp"
android:color="@android:color/white" />
<padding
android:bottom="6dp"
android:left="6dp"
android:right="6dp"
android:top="6dp" />
</shape>
创建边界
创建一个xml文件(例如:"frame_image_view.xml")和以下内容在你的可绘制文件夹:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="@dimen/borderThickness"
android:color="@color/borderColor" />
<padding
android:bottom="@dimen/borderThickness"
android:left="@dimen/borderThickness"
android:right="@dimen/borderThickness"
android:top="@dimen/borderThickness" />
<corners android:radius="1dp" /> <!-- remove line to get sharp corners -->
</shape>
替换@dimen/borderThickness和@color/borderColor与任何你想要或添加相应的dimen/ color。
添加Drawable作为ImageView的背景:
<ImageView
android:id="@+id/my_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/frame_image_view"
android:cropToPadding="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
你必须使用android:cropToPadding="true",否则所定义的填充没有效果。或者在ImageView中使用android:padding="@dimen/borderThickness"来实现相同的效果。 如果边框帧父视图而不是ImageView,尝试使用android:adjustViewBounds="true"。
改变边框颜色
在代码中更改边框颜色的最简单方法是使用tintbackground属性。
ImageView img = findViewById(R.id.my_image_view);
img.setBackgroundTintList(ColorStateList.valueOf(Color.RED); // changes border color to red
or
ImageView img = findViewById(R.id.my_image_view);
img.setBackgroundTintList(getColorStateList(R.color.newColor));
不要忘记定义你的newColor。
使用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扩展了材质设计主题。
上面已经使用过,但没有专门提到。
setCropToPadding(boolean);
如果为真,图像将被裁剪以适合它的填充。
这将使ImageView源与背景填充相匹配。
通过XML它可以做到如下-
android:cropToPadding="true"
对于那些正在搜索自定义边界和形状的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"/>
推荐文章
- 视图绑定-我如何获得包含布局的绑定?
- 在Android Studio中改变矢量资产的填充颜色
- 在构建中编写注释的语法是什么?gradle文件?
- 如何以编程方式添加按钮色调
- 用Android Studio进行调试永远停留在“等待调试器”状态
- Openssl不被视为内部或外部命令
- 无法执行dex:在Eclipse中超过GC开销限制
- 如何以编程方式将视图添加到视图
- 单击url会打开默认浏览器
- 使用Retrofit刷新OAuth令牌,而不修改所有调用
- 多个dex文件定义了landoid /support/v4/accessibilityservice/AccessibilityServiceInfoCompat
- 如何获得动作栏的高度?
- 从活动外部调用startActivity() ?
- createScaledBitmap的过滤器参数做什么?
- 为什么我在使用adb时访问数据文件夹被拒绝?