Java 如何在android中实现多屏幕尺寸的图像区域点击监听器?

Java 如何在android中实现多屏幕尺寸的图像区域点击监听器?,java,android,image,layout,Java,Android,Image,Layout,我想创建全屏活动,全屏加载图像广告。示例图像如下所示: 请注意,按钮也是图像的一部分。以下是布局xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent"

我想创建全屏活动,全屏加载图像广告。示例图像如下所示:

请注意,按钮也是图像的一部分。以下是布局xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ad_image">

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ad_image"
    android:id="@+id/adLayout"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.75"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.16"
        android:id="@+id/adImageLayout"
        android:onClick="adImageClicked">
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
</LinearLayout>

adImageLayout
是按钮绘图的点击占位符。

或为android支持的每个密度提供一个
图像
,并添加
布局
,为每个图像相应地定位按钮,或者更好的是,只需使图像的下部区域可点击即可。
按钮本身很大,几乎覆盖了下屏幕的整个区域。如果用户不想单击该按钮,则不会尝试单击该按钮。我可以直接告诉你们,这就是广告公司在这种情况下所做的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="your_background">

   <ImageButton
       android:layout_width="match_parent"
       android:layout_height="120dp"
       android:layout_alignParentBottom="true"
       android:background="#000"
    />
</RelativeLayout>

----更新----


公共类MainActivity扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Button=(Button)findViewById(R.id.my_按钮);
final int heightPixels=getResources().getDisplayMetrics().heightPixels;
按钮。设置平移Y(-heightPixels/3/2);
}
}

我决定回答我的问题。我改进了我原来的解决方案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ad_image"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.7">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:src="@drawable/xbutton"
            android:onClick="xButtonClicked"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.2">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.1" />

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.3"
            android:onClick="buttonClicked"/>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>
以及
onClick
事件方法


当然,如果您能找到更好的解决方案,请告诉我。

如果您不想使用按钮和视图,您可以在X和Y处以整个屏幕宽度和高度的分数的形式进行思考

假设可点击区域从屏幕宽度的1/3变为2/3。然后,您可以回忆实际屏幕宽度(以dp、像素或其他任何单位表示),并在运行时说可点击区域从(1/3)*屏幕宽度变为(2/3)*屏幕宽度


Ys或“关闭”按钮也是如此。设计师会根据这些比例制作背景。

如果按钮画在中间,那么让你的隐形按钮<代码>宽度>代码>到<代码> Matkh家长>代码,并对齐父按钮中的按钮中心。我刚才试过,它看起来是看不见的按钮不能点击。只是通过添加<代码> Android:OnCase=
中的“按钮点击”
,该按钮还具有
android:visibility=“隐形“
,然后我在
按钮click
方法中放置了一个显示Toast代码,但是onClick方法没有被调用。甚至在不可见按钮上设置onTouchListener也没有帮助。最后,我解决了这个问题,使用了
LinearLayout
作为按钮图形的不可见点击占位符(参见编辑的问题)。你怎么认为?这足够好吗?或者你有更好的建议吗?嗨,我试过你的布局,但不起作用。请看这个。按钮在左上角显示为蓝点。我会使用一个带有azure背景的RelativeLayout,然后添加一个文本视图和一个按钮(关闭X和按钮)。第一个将有一个9补丁作为背景,后者是一个圆角绘制。非常简单,不像全屏图片那样占用内存。@DerGolem:上面显示的图像只是一个示例,我将使用的真实图像不是那样的纯色背景图像。哦,我明白了。无论如何,在我看来,覆盖在家长背景技术上的TextView+按钮比实现一些不必要的自定义逻辑更方便。@DerGolem:不幸的是,它没有那么简单。广告图像可能是这样的。而且我不是艺术家,所以图像将由其他人提供。我只是在做代码逻辑。是的,给我一点时间。这一切都是关于设计,而不是代码(我将创建一个在布局中引用的共享onClick侦听器,并区分代码中的调用方)。让我先准备示例图像,从您的示例中导出(这需要一些时间,其余的都很简单)。很抱歉,我要说的是,您的解决方案非常糟糕。你7岁了<代码>视图组以便定位2<代码>视图。如果使用您的解决方案,则按钮图形之外的区域仍然可以单击。至少我的解决方案仍然可以获得每个按钮顶部单击区域的准确性。别担心,我肯定不会接受我自己的答案。但你只是做错了。你的
weight
只是说:
70%
屏幕的百分比不可点击
并且在
30%
左侧使x%可点击。使用我提供的解决方案,您可以很容易地做到这一点,但仅使用1
ViewGroup
和1-2视图(每个按钮)而不是9。但是您的解决方案使用硬编码dp,这意味着我必须提供不同密度的布局。正如我在评论中提到的,
不应使用硬编码的dp,而是按照语法计算布局的高度,然后将所需高度分配给按钮。我甚至向你展示了如何获得屏幕尺寸
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ad_image"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.7">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:src="@drawable/xbutton"
            android:onClick="xButtonClicked"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.2">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.1" />

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.3"
            android:onClick="buttonClicked"/>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                     WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);