简体   繁体   English

如何在Android中为多个屏幕尺寸实现图像区域点击监听器?

[英]How to implement image region click listener for multiple screen size in android?

I want to create fullscreen activity that load image ad in fullscreen. 我想创建全屏活动,以全屏方式加载图片广告。 The example image is like this: 示例图像如下所示:

在此处输入图片说明

Note that, the button is also part of image. 请注意,该按钮也是图像的一部分。 Here is the layout xml: 这是布局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>

You can see the image is loaded with android:background . 您可以看到该图像已加载android:background

What I want to achieve is: when the "Button" image portion is clicked, it will do something (like launching another activity), as well as the "Close [X]" rectangle at top-right corner, when clicked, it will close the activity. 我要实现的是:单击“按钮”图像部分时,它将执行某项操作(例如启动另一个活动),以及单击右上角的“关闭[X]”矩形,将其进行操作。关闭活动。

It seems it's easy to implement click logic on the region part of "Button" drawing. 在“按钮”图形的区域部分上实现点击逻辑似乎很容易。 So far I found two ways: 到目前为止,我发现了两种方法:

  1. Set ontouch listener on LinearLayout and check it's X and Y coordinate for the Button drawing region 在LinearLayout上设置ontouch侦听器,并检查Button绘制区域的X和Y坐标

  2. Put invisible ImageView on top of "Button" image in layout, and set onclick listener. 将不可见的ImageView放在布局中的“按钮”图像顶部,然后设置onclick侦听器。

But the problem is: there are multiple screen sizes and resolutions across many android devices. 但是问题是:许多Android设备上有多种屏幕尺寸和分辨率。 So I wonder if those two solusions can be used in portable way across different screen size? 因此,我想知道这两种解决方案是否可以在不同的屏幕尺寸上以便携式方式使用?

Since the image might be stretched when loaded in different screen resolution, and that means for solution #1: I have to adjust X & Y number. 由于以不同的屏幕分辨率加载图像时可能会拉伸,因此对于解决方案1:我必须调整X&Y数。 And for solution #2: the invisible ImageView's position might get synched-off from the Button drawing. 对于解决方案2:不可见的ImageView的位置可能会从Button图形中被同步掉。

So anyone know better solution for this? 那么有人知道更好的解决方案吗?

UPDATE: 更新:

I'm trying to use invisible click placeholder solution. 我正在尝试使用隐形点击占位符解决方案。 Here is the layout xml: 这是布局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"
    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 is the click placeholder for the button drawing. adImageLayout是按钮绘图的单击占位符。

Or supply an image for each of the densities that android supports, and add layout that position the button accordingly for each Image Or much better yet just make the lower region of the image clickable. 或为android支持的每种密度提供一个image ,然后添加layout以对每个图像相应地定位按钮。或者更好的方法是使图像的下部区域可单击。 The button itself is big and almost cover the entire region of the lower screen. button本身很大,几乎覆盖了下部屏幕的整个区域。 users will not try to click there if they don't want to click the button. 如果用户不想单击该按钮,则不会尝试单击该按钮。 I can tell you from first hand that this is what Ad companies doing in situations like that. 我可以从第一手告诉你,这就是广告公司在这种情况下所做的。

<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>

---- UPDATE ---- ----更新----

<FrameLayout 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">

<Button
    android:id="@+id/my_button"
    android:text="My Button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/black"
    android:textColor="@android:color/white"
    android:gravity="center"
    android:layout_gravity="bottom"
    android:padding="24dp"
    />

</FrameLayout>

public class MainActivity extends Activity {

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.my_button);

        final int heightPixels = getResources().getDisplayMetrics().heightPixels;
        button.setTranslationY(- heightPixels / 3 / 2);
    }



}

I decided to answer my question. 我决定回答我的问题。 I improved my original solution: 我改进了原始解决方案:

<?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>

Here is the schema of the Layout: 这是布局的架构:

在此处输入图片说明

The close button is loaded with ImageView. 关闭按钮随ImageView一起加载。

So basically I'm just using layout_weight trick here. 所以基本上我只是在这里使用layout_weight技巧。 I avoided hardcoded dpi, as it will cause different LinearLayout's width/height result in different screen size. 我避免使用硬编码的dpi,因为它会导致不同的LinearLayout的宽/高导致不同的屏幕尺寸。

I have tested with several screen resolutions (except tablet size), it works fine. 我已经测试了几种屏幕分辨率(平板电脑尺寸除外),它可以正常工作。 I don't need to supply different image and layout files for multiple densities. 我不需要为多种密度提供不同的图像和布局文件。 And the required code in the Activity class is very minimal, there are only code to set fullscreen: 而且Activity类中所需的代码非常少,只有代码可以设置全屏显示:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                     WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);

and the onClick event methods. onClick事件方法。

Of course, if you can found better solution, let me know. 当然,如果您可以找到更好的解决方案,请告诉我。

If you don't want to work with buttons and views, you could just think at X and Y in form of fractions of the whole screen width and height. 如果您不想使用按钮和视图,则可以考虑X和Y以整个屏幕宽度和高度的分数形式。

Let's say the clickable area goes from 1/3 to 2/3 of the screen width. 假设可点击区域从屏幕宽度的1/3变为2/3。 Then you can just recall the actual ScreenWidth (in dp, pixels, or whatever) and say at runtime that the clickable area goes from (1/3)*ScreenWidth to (2/3)*ScreenWidth. 然后,您可以调出实际的ScreenWidth(以dp,像素或其他形式),并在运行时说可单击区域从(1/3)* ScreenWidth变为(2/3)* ScreenWidth。

Same for Ys, or for the close button. Ys或关闭按钮也一样。 Designer will then make the background according to these ratios. 然后,设计师将根据这些比例制作背景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM