【问题标题】:How to make custom shape buttons that overlap each other in Android如何在Android中制作相互重叠的自定义形状按钮
【发布时间】:2015-04-16 03:18:46
【问题描述】:

我有 6 张带有透明背景的独立图像。如何将所有这些图像作为按钮放在一起,例如:

根据我的阅读,我想我必须使用框架布局才能有重叠的按钮。

我需要每种颜色在单击时都是一个单独的按钮。

更新:我做了一个演示并在 onclick 方法中检查透明 但是,当我单击红色和蓝色交点附近的红色区域时,由于视图重叠,它不会记录单击红色按钮。请帮忙!

https://www.dropbox.com/s/fc98nnnfbrtdh82/Photo%20Apr%2016%2C%202%2002%2013.jpg?dl=0

public boolean onTouch(View v, MotionEvent 事件) {

                                     int eventPadTouch = event.getAction();
                                     int iX = (int)event.getX();
                                     int iY = (int)event.getY();          
                                     switch (eventPadTouch) {

                                         case MotionEvent.ACTION_DOWN:

                                             if (iX>=0 & iY>=0 & iX<TheBitmap.getWidth() & iY<TheBitmap.getHeight()&TheBitmap.getPixel(iX,iY)!=0) {
                                                 if (TheBitmap.getPixel(iX,iY)!=0) {
                                                     Toast.makeText(getApplicationContext(),"clicked blue",Toast.LENGTH_LONG).show();

                                                 }
                                             }
                                             return true;
                                     }

                                     return false;
                                 }
                             }

【问题讨论】:

  • 你有试过的代码吗?这将有助于人们回答您的问题。
  • 我没有时间弄清楚并写下答案,而是在谷歌上搜索“android 非矩形按钮”。那里的stackoverflow问题将为您指明正确的方向。由于您所有的按钮都是不同的颜色,因此想到的一个想法是在 onTouch 事件下方设置颜色。

标签: android button android-imagebutton


【解决方案1】:

您需要使用相对布局来使用 ImageView 放置背景图像,如下所示:

activity_layout.xml

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"/> 
</RelativeLayout>

之后,您需要在 drawable 中创建一个单独的 xml 文件,以尽可能接近地定义每个形状,在此处阅读更多相关信息: http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

一个示例形状的 xml 文件应该是这样的

drawable/myshape1.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<stroke
  android:width="2dp"
  android:color="#FFFFFF" />

<corners android:radius="5dp" />

<gradient
  android:angle="270"
  android:centerColor="#6E7FFF"
  android:endColor="#142FFC"
  android:startColor="#BAC2FF" /> 
</shape>

最后,在创建完所有形状后,您可以将它们添加到您的 activity_layout.xml 文件中,如下所示:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/bg"/>


        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_margin="20dp"
        android:background="@drawable/myshape1"
        android:orientation="vertical"
        android:padding="5dp" >


</RelativeLayout>

确保创建的形状尽可能透明,并将 onClick 处理程序附加到它们以执行分配的任务。

编辑:

根据您的评论,还有另一种方法可以做到这一点,即覆盖 OnTouch 侦听器,从位图中捕获像素并确定其颜色。

imageView.setOnTouchListener(new View.OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
         if (event.getAction() == MotionEvent.ACTION_DOWN){
                ImageView imageView = (ImageView) v;
                Bitmap bitmap =((BitmapDrawable)imageView.getDrawable()).getBitmap();
                int pixel = bitmap.getPixel(event.getX(),event.getY());
                int redValue = Color.red(pixel);
                int blueValue = Color.blue(pixel);
                int greenValue = Color.green(pixel);
//Now that you know the color values you can decide on what you want to do based on the color combination.
         }
         return true;
     }
});

【讨论】:

  • 你的建议是我应该在上面的图片上画6个形状?大多数人建议定位位图颜色,但是对于每个图像,按下时我也有一个 press_state_image,所以不能这样做:((
  • @kellyrose 我已经更新了我的答案以尝试回答位图颜色问题。
  • 所以我不再需要创建形状了吗?颜色是渐变的,不是完全红色或蓝色,所以我在主要活动中使用 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.YOUR_IMAGE_ID) 来获取重新评分,但应用程序不断崩溃
  • 得到了崩溃修复,如果使用这种方法,我仍然认为我不能在单击时更改图像,通常我们在 drawable 中创建 .xml 文件并使用选择器为按下和未按下状态选择图像,如果我按照你的建议做,我必须将 6 件的组合(如图所示)作为一个单独加载,因此可以更改单个按钮
  • @kellyrose 红色、蓝色和绿色值是我们创建所有颜色的主要颜色(也有 CMYK,但仅用于打印)。为了能够确定按下了哪个按钮,请坚持覆盖 onTouch (是的,您不需要上面的所有内容,只需要覆盖)并使用 RGB 的组合,您可以判断按下了哪个按钮例如,如果我将您的图像导入一个图像编辑器,我可以看出蓝色按钮是从 RGB (10,10,240) 到 RGB (25,40,100) (我只是在这里抛出随机值,但我希望你明白我的意思。en.wikipedia.org/wiki/RGB_color_model
【解决方案2】:

您始终可以使用RelativeLayout 作为包装器,然后将未来按钮添加为子按钮,您可以将它们定位为重叠。

例如:

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
    *
    *
    *
</RelativeLayout>

然后您可以使用对齐/边距/坐标定位ImageViews(或Buttons)。

希望对你有帮助。

【讨论】:

  • 谢谢,我创建了 2 个相互重叠的测试按钮,但是当单击重叠区域时(例如,如果我单击蓝色区域附近的红色区域,则没有任何反应,因为它仍在蓝色形状的矩形区域。你知道我该如何解决吗?
猜你喜欢
  • 1970-01-01
  • 2021-02-08
  • 2012-12-01
  • 2016-08-16
  • 2018-03-12
  • 1970-01-01
  • 2016-08-20
  • 2015-12-24
相关资源
最近更新 更多