android - 如何给我的图像添加点击效果?
问题描述
我有一个图像,我想创建一个按钮点击效果 - 当按钮被触摸时,它应该会立即改变颜色。
我查看了下面的链接,但它对我不起作用:
如何在 Android 上提供像按钮一样的 imageview 点击效果?
也许是我的安卓版本,即 Lollipop。尽管我在上面的链接中尝试了建议,但它不起作用。我的图像不会改变并且保持相同的颜色。
这是我的代码:
//into the toolbar, inflate the back button and Populisto title,
//which we find in toolbar_custom_view_layout.xml
View logo = getLayoutInflater().inflate(R.layout.toolbar_custom_view_layout, null);
toolbar.addView(logo);
ImageView backButton = (ImageView) logo.findViewById(R.id.back_arrow_id);
backButton.setBackgroundColor(Color.rgb(100, 100, 50));
//set the ontouch listener
backButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
ImageView view = (ImageView) v;
view.getDrawable().setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);
view.invalidate();
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
ImageView view = (ImageView) v;
//clear the overlay
view.getDrawable().clearColorFilter();
view.invalidate();
break;
}
}
return false;
}
});
解决方案
return false;
它似乎不接受您所写声明的更改。请将其更改为以下内容:
return true;
还有什么问题,欢迎提问:)
推荐阅读
- python - 如何使用两个随机变量列表在 python 中创建点列表(二维数组)
- gradle - 如何使用 gradle 以一定的顺序发布许多依赖的子项目?
- apache-kafka - Kafka 流仅在收到第一个确认时才使用下一条消息
- php - 从 Woocommerce 的产品库中获取第一张图片的 URL
- qt - Qt QProcess间歇性读取标准输出失败
- regex - 如何在python中使用正则表达式从csv文件中获取数据
- validation - 保存实体之前进行 cakephp3 验证
- types - 在不使用包的情况下输入端口声明 VHDL
- python - Python中的统计数据
- bash - anaconda 安装,火炬激活:没有这样的文件或目录