无法将colorFilter应用于文本选择手柄 [英] Can't apply a colorFilter to text selection handles

查看:81
本文介绍了无法将colorFilter应用于文本选择手柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将实质性的文本选择句柄引入我的应用程序.我从SDK中获得了用于中/右/左手柄(位图)和文本光标(9-patch)的可绘制对象,并设置:

I'm trying to bring material text selection handles to my app. I got drawables from the SDK for middle/right/left handle (bitmaps) and text cursor (9-patch), and set:

<item name="android:textSelectHandleLeft">@drawable/text_select_handle_left_mtrl_alpha</item>
<item name="android:textSelectHandleRight">@drawable/text_select_handle_right_mtrl_alpha</item>
<item name="android:textSelectHandle">@drawable/text_select_handle_middle_mtrl_alpha</item>
<item name="android:textCursorDrawable">@drawable/text_cursor_mtrl_alpha</item>

它按预期工作.但是,在Lollipop中,这些可绘制对象使用android:tint属性在XML中用特定颜色着色,而我无法在API< 21上使用.因此,我正在尝试在运行时设置滤色器.

It works as expected. However, in Lollipop these drawables are tinted with a particular color in XML using the android:tint attribute, which I can't use on API<21. So I'm trying to set a color filter at runtime.

  • 文本光标不会被着色.我认为这可能是因为它是9个补丁.如何在运行时过滤9补丁可绘制对象?我可能尝试了所有PorterDuff.Mode s.

左右手柄为黑色,而中间手柄为白色.

Right/left handles are black, while middle handle is white.

也就是说,我都不希望它们是绿色的.为什么?

I.e., non of them is green as I would like. Why?

正如您在上方看到的那样,我在编辑文本下方设置了四个ImageView,但它们却被着色了.

As you can see above, I set up four ImageView below my edit text, and they instead get tinted.

   private void setUpTextCursors() {
        Drawable left = getResources().getDrawable(R.drawable.text_select_handle_left_mtrl_alpha);
        Drawable right = getResources().getDrawable(R.drawable.text_select_handle_right_mtrl_alpha);
        Drawable middle = getResources().getDrawable(R.drawable.text_select_handle_middle_mtrl_alpha);
        Drawable cursor = getResources().getDrawable(R.drawable.text_cursor_mtrl_alpha);
        ColorFilter cf = new PorterDuffColorFilter(mGreenColor, PorterDuff.Mode.SRC_IN);

        /**
        * tint my ImageViews, but no effect on edit text handles
        */
        left.setColorFilter(cf); 
        right.setColorFilter(cf);
        middle.setColorFilter(cf);

        /**
        * no effect whatsoever
        */
        cursor.setColorFilter(cf);
   }

看起来这里我们既有9个补丁的着色问题-由于过滤器甚至在测试ImageViews上也失败-以及与以下事实有关的问题:文本选择管理器未考虑所有应用的过滤器.

Looks like here we have both a 9-patch tinting issue - since filter fails even on test ImageViews - and an issue related to the fact that none of the applied filters get considered by the text selection manager.

与此相关的源代码来自TextView

Relevant source code about that is from the TextView class and from this Editor hidden helper class which I found somehow. Spent some time on it but still can't tell why my filters are ignored.

对于@pskink:让cursor是经过过滤的可绘制对象,我可以拥有:

To @pskink: let cursor be the filtered drawable, I can have:

<ImageView
    android:id="@id/1"
    android:src="@drawable/cursor_drawable" />

<ImageView 
    android:id="@id/2" />

第一个不会着色,但是如果我叫imageView2.setBackground(cursor),则它会着色. 另外,如果我有

The first won't be tinted, but if I call imageView2.setBackground(cursor), then it's tinted. Also if I have

<item name="android:textSelectHandle">@drawable/cursor_drawable</item>

这会影响编辑选择(因为我会覆盖默认光标),但不会再次着色.

this affects the edit selection (because I override the default cursor) but it's not tinted, again.

推荐答案

您需要覆盖活动"使用的默认资源:

you need to override the default Resources used by your Activity:

// your activity source file
Resources res;

@Override
public Resources getResources() {
    if (res == null) {
        res = new TintResources(super.getResources());
    }
    return res;
}

自定义Resources类将覆盖getDrawable()方法,因此您可以拦截创建Drawables并设置颜色滤镜,例如:

the custom Resources class will override getDrawable() method so you can intercept creating your Drawables and set up the color filter, for example:

class TintResources extends Resources {

    public TintResources(Resources resources) {
        super(resources.getAssets(), resources.getDisplayMetrics(), resources.getConfiguration());
    }

    @Override
    public Drawable getDrawable(int id) throws NotFoundException {
        Drawable d = super.getDrawable(id);
        if (id == R.drawable.text_cursor_material) {
            // setup @drawable/text_cursor_material
            d.setColorFilter(0xff00aa00, PorterDuff.Mode.SRC_IN);
        }
        return d;
    }
}

与设置其他Drawables(@ drawable/text_select_handle _ * _ material)的方式相同,请注意,由于EditText没有用于访问这些Drawables的getter方法,因此您不必采用直接方式.

the same way you can setup other Drawables (@drawable/text_select_handle_*_material), note you need that not direct way since EditText doesn't have getter methods for accessing those Drawables

这篇关于无法将colorFilter应用于文本选择手柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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