Android 安卓:我想更改图像视图的颜色
我想更改imageView的颜色。 将代码粘贴到下面:- 首先,我粘贴了footer.xml文件Android 安卓:我想更改图像视图的颜色,android,imageview,Android,Imageview,我想更改imageView的颜色。 将代码粘贴到下面:- 首先,我粘贴了footer.xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="48dp" and
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#f1eeee"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/fHome"
android:background="@drawable/colorchanged"
android:src="@drawable/home" /> <!-- your image here -->
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/fAttendence"
android:src="@drawable/att" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/fTarget"
android:src="@drawable/target" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/fReport"
android:src="@drawable/report" />
</LinearLayout>
当我点击imageView时,我想在上面设置蓝色
有人能帮我吗?您可以使用下面这样的
颜色过滤器,在查看时单击即可触发:
yourImageView.setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.MULTIPLY);
请注意,这实际上会改变yourImageView
实例的状态,因此,您需要一个变量来跟踪它。一种方法是通过photoshop编辑这两个.png。
例如,当前的“灰色”版本可能有名称
主页按钮未选中。png
编辑此图像并将其从灰色更改为蓝色或所需颜色,另存为
主页按钮选中。png
将它们导入到项目中,现在就有了这两个文件。(对于每个图像)
1) 设置默认状态,我猜可能是“home\u button\u unselected.png”,通过如下xml:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/fHome"
android:src="@drawable/home_button_unselected" />
如果您有其他按钮,则应首先检查选择了哪个按钮,如果选择了其他按钮,然后所有其他按钮应更改为未选中的png主要基于意见的问题。搜索google@intellijamiya我也试过了,但它并没有给我想要的输出。最好的方法是把那些2.png的图片通过photoshop编辑成蓝色,然后保存为“home_selected”。将它们导入到您的项目中,然后单击imageView.setImageResource(R.Drawable.name\u of_image)更改图标;因此,对于每个可以选择的图像,您都选择了image.png和image_。png@SachinSolanki请看上面的评论。@Tony,你能用示例代码解释一下吗?这是有效的,我还有一个问题:首先,我把这些图像用作菜单。如果我跳转到下一页,那么它只会保持蓝色,但我想在跳转到下一页时设置默认的灰色。您可以调用yourImageView.clearColorFilter()
删除过滤器,但需要编写处理此问题的逻辑(这就是为什么我说您需要一个变量来存储状态)我们可以使用选择器吗?
mHomeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mHomeButton.setImageResource(R.Drawable.home_button_selected); // setting the image to the selected one ( which is blue/selected)
}
});