如何在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"
不幸的是,它给我“不太好”的边界角如下图:
其他回答
我将下面的xml设置为可绘制的图像视图的背景。它的工作原理。
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke android:width="1dp" android:color="#000000" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
</shape>
然后添加android:background="@drawable/yourXmlFileName"到你的ImageView
我发现这样做容易得多:
1)编辑帧内有内容(用9patch工具)。
2)将ImageView放置在线性布局中,并设置你想要的框架背景或颜色作为线性布局的背景。当你设置帧本身有内容时,你的ImageView将在帧内(就在你用9patch工具设置内容的地方)。
创建边界
创建一个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。
在相同的xml中,我使用了下一个:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff" <!-- border color -->
android:padding="3dp"> <!-- border width -->
<ImageView
android:layout_width="160dp"
android:layout_height="120dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop" />
</RelativeLayout>
ImageView的xml文件
<ImageView
android:id="@+id/myImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="1dp"
android:scaleType="centerCrop"
android:cropToPadding="true"
android:background="@drawable/border_image"
android:src="@drawable/ic_launcher" />
以border_image.xml的名称保存下面的代码,它应该在可绘制文件夹中
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#ffffff"
android:startColor="#ffffff" />
<corners android:radius="0dp" />
<stroke
android:width="0.7dp"
android:color="#b4b4b4" />
</shape>
如果你想给图像的边界圆角,那么你可以改变border.xml文件中的一行
<corners android:radius="4dp" />
推荐文章
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点
- 相同的导航抽屉在不同的活动
- 如何从视图中获得托管活动?
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器
- Android命令行工具sdkmanager总是显示:警告:无法创建设置