Android的ImageView的色彩变化来模拟按钮点击 [英] Android imageview change tint to simulate button click

查看:201
本文介绍了Android的ImageView的色彩变化来模拟按钮点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我所设置的位图从URL中取出一个ImageView的。 在ImageView的我已成立开辟了一个对话的onClickListener。

I have an imageview on which I have set a bitmap fetched from an url. On the imageview I have set an onClickListener which opens up a dialog.

我想以某种方式改变色调(使其更暗)时,ImageView的是pressed时提供一个排序按钮的点击般的感觉。

I want to somehow change the tint (make it darker) when the imageview is pressed upon to provide a sort of button click like feel.

你有什么建议怎么样?

What do you suggest?

推荐答案

happydude的答案是最优雅的方式来处理这个问题,但遗憾的是(如在评论中指出)源$ C ​​$下ImageView的只接受一个整数(纯色)。 <一href="https://$c$c.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=18220">Issue 18220 已经有几年解决这一点,我已经发布了解决办法在那里我能在这里总结一下:

happydude's answer is the most elegant way to handle this but unfortunately (as pointed out in the comments) the source code for ImageView only accepts an integer (solid colour). Issue 18220 has been around for a couple years addressing this, I've posted a workaround there that I'll summarize here:

扩展的ImageView和包装drawableStateChanged()与code表示将根据新的国家色调:

Extend ImageView and wrap drawableStateChanged() with code that sets the tint based on the new state:

TintableImageView.java

package com.example.widgets;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.example.R;

public class TintableImageView extends ImageView {

    private ColorStateList tint;

    public TintableImageView(Context context) {
        super(context);
    }

    public TintableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public TintableImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TintableImageView, defStyle, 0);
        tint = a.getColorStateList(R.styleable.TintableImageView_tint);
        a.recycle();
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        if (tint != null && tint.isStateful())
            updateTintColor();
    }

    public void setColorFilter(ColorStateList tint) {
        this.tint = tint;
        super.setColorFilter(tint.getColorForState(getDrawableState(), 0));
    }

    private void updateTintColor() {
        int color = tint.getColorForState(getDrawableState(), 0);
        setColorFilter(color);
    }

}

定义自定义属性:

Define a custom attribute:

attrs.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>

    <declare-styleable name="TintableImageView">
        <attr name="tint" format="reference|color" />
    </declare-styleable>

</resources>

使用小部件和自定义属性与您当地的命名空间,而不是Android的:

Use the widget and custom attribute with your local namespace instead of Android's:

example_layout.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <com.example.widgets.TintableImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/example"
        app:tint="@color/color_selector"/>

</LinearLayout>

您可以再使用颜色选择器像happydude建议:

You can then use a colour selector like happydude suggested:

color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/pressed_color"/>
    <item android:color="#00000000"/>
</selector>

这篇关于Android的ImageView的色彩变化来模拟按钮点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆