如何创建这样的自定义按钮? [英] How to create such custom button?

查看:154
本文介绍了如何创建这样的自定义按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个自定义触摸区域的按钮?结果
我想创建曲线的按钮类似下面的:

How can I create a Button with a custom touch area?
I want to create curved Buttons like the ones below:

在这里输入的形象描述

但只有曲线下的区域可点击,就这么简单ImageButtons将无法工作,因为击中格为长方形。

But only the area under the curve should be clickable, so simple ImageButtons won't work since the hitbox is rectangular.

1pic。它的工作原理与ImageButtons结果
2pic。版本中的一个作为必须

1pic. how it works with ImageButtons
2pic. one of the versions as have to be

在这里输入的形象描述

推荐答案

好吧,我解决了一个ImageView的和onTouchListener porblem。我已经计算每个按键区和式交叉检查触摸视图的坐标。
在这里,code的检查和折叠按钮:

Ok, I've solved that porblem by one ImageView and onTouchListener. I've calculated the formula for each button-zone and cross-checked the coordinates of touching view. Here the code for Check and Fold buttons:

 public boolean onTouch(View v, MotionEvent event) {
    x = event.getX();
    y = event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: // нажатие
            if (y > 0.98 * x +75) {
                Log.d("TEST", x+" __FOLD__ "+y);
                img.setBackgroundResource(R.drawable.fold_all);
            } else
            if(y > -0.98 * (x-1200)){
                Log.d("TEST", x+" __CHECK__ "+y);
                img.setBackgroundResource(R.drawable.check_all);
            } else {
                Log.d("TEST", x+" __NOTHING__ "+y);
            }
            break;
        case MotionEvent.ACTION_MOVE: // движение
            break;
        case MotionEvent.ACTION_UP: // отпускание
        case MotionEvent.ACTION_CANCEL:
            img.setBackgroundResource(R.drawable.all);
            break;
    }
  return true;
}

对不起,我的英语水平。

Sorry for my english.

这篇关于如何创建这样的自定义按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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