[英]How to implement image region click listener for multiple screen size in android?
我想創建全屏活動,以全屏方式加載圖片廣告。 示例圖像如下所示:
請注意,該按鈕也是圖像的一部分。 這是布局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>
您可以看到該圖像已加載android:background
。
我要實現的是:單擊“按鈕”圖像部分時,它將執行某項操作(例如啟動另一個活動),以及單擊右上角的“關閉[X]”矩形,將其進行操作。關閉活動。
在“按鈕”圖形的區域部分上實現點擊邏輯似乎很容易。 到目前為止,我發現了兩種方法:
在LinearLayout上設置ontouch偵聽器,並檢查Button繪制區域的X和Y坐標
將不可見的ImageView
放在布局中的“按鈕”圖像頂部,然后設置onclick偵聽器。
但是問題是:許多Android設備上有多種屏幕尺寸和分辨率。 因此,我想知道這兩種解決方案是否可以在不同的屏幕尺寸上以便攜式方式使用?
由於以不同的屏幕分辨率加載圖像時可能會拉伸,因此對於解決方案1:我必須調整X&Y數。 對於解決方案2:不可見的ImageView的位置可能會從Button圖形中被同步掉。
那么有人知道更好的解決方案嗎?
更新:
我正在嘗試使用隱形點擊占位符解決方案。 這是布局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
是按鈕繪圖的單擊占位符。
或為android支持的每種密度提供一個image
,然后添加layout
以對每個圖像相應地定位按鈕。或者更好的方法是使圖像的下部區域可單擊。 該button
本身很大,幾乎覆蓋了下部屏幕的整個區域。 如果用戶不想單擊該按鈕,則不會嘗試單擊該按鈕。 我可以從第一手告訴你,這就是廣告公司在這種情況下所做的。
<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>
----更新----
<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);
}
}
我決定回答我的問題。 我改進了原始解決方案:
<?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>
這是布局的架構:
關閉按鈕隨ImageView一起加載。
所以基本上我只是在這里使用layout_weight
技巧。 我避免使用硬編碼的dpi,因為它會導致不同的LinearLayout的寬/高導致不同的屏幕尺寸。
我已經測試了幾種屏幕分辨率(平板電腦尺寸除外),它可以正常工作。 我不需要為多種密度提供不同的圖像和布局文件。 而且Activity類中所需的代碼非常少,只有代碼可以設置全屏顯示:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
和onClick
事件方法。
當然,如果您可以找到更好的解決方案,請告訴我。
如果您不想使用按鈕和視圖,則可以考慮X和Y以整個屏幕寬度和高度的分數形式。
假設可點擊區域從屏幕寬度的1/3變為2/3。 然后,您可以調出實際的ScreenWidth(以dp,像素或其他形式),並在運行時說可單擊區域從(1/3)* ScreenWidth變為(2/3)* ScreenWidth。
Ys或關閉按鈕也一樣。 然后,設計師將根據這些比例制作背景。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.