Android - 自动覆盖触地/突出显示的图像按钮状态

发布于 2024-10-29 18:42:51 字数 489 浏览 3 评论 0原文

是否可以使用 ImageButton 实现触摸和突出显示状态,而无需在 Android 上添加 2 个图像资源(考虑到 h/m/ldpi,还需要 6 个图像资源)?我基本上正在寻找类似于 iOS 的行为,其中操作系统可以在按钮的触摸状态上放置半 alpha 覆盖。

我尝试在 onTouch 侦听器中使用 setColorFilter(0xFF000000, Mode.MULTIPLY) ,结果非常接近我想要的结果 - 但我不确定状态处理的最佳方式实现这一点:

  1. touchDown 事件 ->更改颜色叠加。
  2. touchUp 事件 ->删除颜色叠加,并执行按钮操作。

有没有更好的方法...或者有人可以帮助填补空白?

我不想使用单独的图像有几个原因 - 这是一个 iPhone 端口,我还没有合适的资源,并且考虑到我已经有低/中/高的创意,需要更多的设计时间考虑。

谢谢!

Is it possible using an ImageButton to have touch down and highlighted states, without the need for 2 more image resources on Android (6 more, considering h/m/ldpi)? I'm basically looking for behavior similar to iOS where the OS can put an semi-alpha overlay on the touch down state of a button.

I've tried using setColorFilter(0xFF000000, Mode.MULTIPLY) in the onTouch listener, and the result was pretty close to what I was after - but I'm not sure the best way of the state handling to achieve this:

i.e.

  1. touchDown event -> Change color overlay.
  2. touchUp event -> remove color overlay, and perform button action.

Is there a better way... Or can someone help fill in the gaps?

I don't want to use separate images for a couple of reasons - it's an iPhone port for which I don't have the appropriate assets yet, and would require more designer time considering I've got the low/medium/high creatives to consider.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城花音 2024-11-05 18:42:51

如果您对 setColorFilter(0xFF000000, Mode.MULTIPLY) 感到满意,那么如何为自己创建一个扩展 ImageButton 的自定义组件呢?

像这样的东西:(

抱歉我没有机会测试这个)

package com.example;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;

public class IosImageButton extends ImageButton implements OnTouchListener
{

    public IosImageButton(final Context context, final AttributeSet attrs, final int defStyle)
    {
        super(context, attrs, defStyle);
        this.init();
    }

    public IosImageButton(final Context context, final AttributeSet attrs)
    {
        super(context, attrs);
        this.init();
    }

    public IosImageButton(final Context context)
    {
        super(context);
        this.init();
    }

    private void init()
    {
        super.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(final View view, final MotionEvent event)
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                // touchDown event -> Change color overlay. goes here
                // e.g. this.setColorFilter(0xFF000000, Mode.MULTIPLY);
                return true;

            case MotionEvent.ACTION_UP:
                // touchUp event -> remove color overlay, and perform button action.
                // e.g. this.setColorFilter(0xFF000000, Mode.MULTIPLY);
                return true;

            default:
                return false;
        }
    }
}

然后是 view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.example.IosImageButton 
        <!-- add standard ImageButton setting here -->
        />

</RelativeLayout>

If you are happy with the setColorFilter(0xFF000000, Mode.MULTIPLY) then how about making yourself a custom component that extends ImageButton?

Something like:

(Sorry I haven't had a chance to test this)

package com.example;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;

public class IosImageButton extends ImageButton implements OnTouchListener
{

    public IosImageButton(final Context context, final AttributeSet attrs, final int defStyle)
    {
        super(context, attrs, defStyle);
        this.init();
    }

    public IosImageButton(final Context context, final AttributeSet attrs)
    {
        super(context, attrs);
        this.init();
    }

    public IosImageButton(final Context context)
    {
        super(context);
        this.init();
    }

    private void init()
    {
        super.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(final View view, final MotionEvent event)
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                // touchDown event -> Change color overlay. goes here
                // e.g. this.setColorFilter(0xFF000000, Mode.MULTIPLY);
                return true;

            case MotionEvent.ACTION_UP:
                // touchUp event -> remove color overlay, and perform button action.
                // e.g. this.setColorFilter(0xFF000000, Mode.MULTIPLY);
                return true;

            default:
                return false;
        }
    }
}

Then the view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.example.IosImageButton 
        <!-- add standard ImageButton setting here -->
        />

</RelativeLayout>
心房敞 2024-11-05 18:42:51

不要使用触摸事件来尝试在此类内容上显示适当的状态。你不可避免地会得到部分错误。该框架做了很多工作来检测用户意图(例如:用户触地但随后滑落手指),同时考虑到诸如倾斜等因素。

对于 ImageButton,您可以使用 src 属性来显示图标,然后使用适当的 StateListDrawable 作为背景。

我创建了一个快速项目供您查看 在 Github 上

请注意,触摸状态显示在背景中,不是前景。请参阅 Google+ 上的讨论,了解为什么 Android 框架工程师认为触摸反馈应该放在后面,而不是放在后面正面。如果您仍然希望可绘制对象位于前面,请参阅我的帖子 在我的博客上

Do not use touch events to try to show the appropriate states on something like this. You will inevitably get it partially wrong. The framework does a lot of work to detect user intent (ex: the user touched down but then slid off his finger) taking into account things like slop etc.

For ImageButton, you can use the src attribute to show the icon and then use an appropriate StateListDrawable as the background.

I have created a quick project for you to see on Github

Note that the touch states are shown in the background, not the foreground. See the discussion on Google+ on why Android framework engineers think touch feedback should be behind, not in front. If you still want the drawable to be in front, see my post on my blog

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文