2013-05-21 244 views
21

我正在設計一個GUI遠程控制,但不是爲每個遠程按鈕製作單獨的按鈕,我想獲得一個完整的準備好的遠程圖像並設置其可點擊的某些部分。 有沒有一種方法,而不是運動事件來做到這一點?可點擊的圖像區域

+0

你想使這一切在一個圖像? – kabuto178

+1

@ kabuto178是的.... –

+0

是他們以某種方式將圖像分割爲行和列的方式? –

回答

30

我有2個解決方案爲您的requirements.in兩個,整個圖像保持點擊,但你可以得到有關點擊區域的信息。

解決方案1:

可以屏蔽圖像和獲取下image.so的像素顏色最終你可以來了解被點擊哪個區域。

這裏,無論何時發生點擊,您都可以檢查background image的像素顏色,並將其與預定義的顏色設置相匹配,以瞭解哪個區域已被點擊。

前景畫面: Foreground image

背景畫面: Background image

可點擊區域: Representing clickable area

還是糊塗?

參考: 我想建議你去通過this教程。

解決方案2:

你可以用座標映射圖像,因此你可以得到已被點擊區域的信息。

例子: MappedImage與座標

,如果你不知道座標,你可以創建here

座標爲堪薩斯您mappedimage會是這個樣子,

 <area shape="poly" coords="243,162,318,162,325,172,325,196,244,196" id="@+id/area14" name = "Kansas"/> 

MappedImage with co-ordinates

參考: 請看看Android Image Mapping

我希望它會有幫助!

+0

好的,謝謝.......我會很快測試 –

+0

感謝您的支持!任何想法如何在鈦合金應用程序中使用這些方法? – learner123

+0

如果我們必須放大圖像並獲得像素,該怎麼辦?它會返回相同的顏色嗎? –

7

您仍然可以使用按鈕。

你可以將它們放在你的圖像上的正確位置,並使它們不可見。

在XML

<Button android:visibility="invisible"/> 

或者

Button mybutton = (Button) v1; 
mybutton.setVisibility(View.INVISIBLE); 
+1

我有一個相對的佈局,其中有一些按鈕。如果我在相對佈局上設置背景圖像,並將按鈕設置爲不可見,那麼onClick不會被觸發。當我將它們設置爲可見時,它們就會顯示。 – Rule

+0

這不取決於你如何讓你的按鈕不可見?也就是說,如果您將'visibility'屬性設置爲'gone'(或'invisible'),那麼它們不能被點擊,但如果您嘗試將按鈕的背景設置爲100%透明 - 我希望您仍然可以獲得點擊事件(??) –

+0

@sealz我喜歡這個解決方案,但可見性沒有工作。我使背景透明並且工作。 – jcaruso

1

我有同樣的挑戰,並與庫"PhotoView",在那裏你可以聽

onPhotoTap(View view, float x, float y){} 

事件,並檢查是否解決了它該選項卡位於圖像區域內,然後執行某些任務。

我創建了一個庫來幫助其他開發人員更快地實現此類功能。它位於Github上:ClickableAreasImages

此庫允許您定義圖像中的可點擊區域,將對象與其關聯並偵聽該區域的觸摸事件。

如何使用圖書館:

public class MainActivity extends AppCompatActivity implements OnClickableAreaClickedListener { 

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

     // Add image 
     ImageView image = (ImageView) findViewById(R.id.imageView); 
     image.setImageResource(R.drawable.simpsons); 

     // Create your image 
     ClickableAreasImage clickableAreasImage = new ClickableAreasImage(new PhotoViewAttacher(image), this); 

     // Initialize your clickable area list 
     List<ClickableArea> clickableAreas = new ArrayList<>(); 

     // Define your clickable areas 
     // parameter values (pixels): (x coordinate, y coordinate, width, height) and assign an object to it 
     clickableAreas.add(new ClickableArea(500, 200, 125, 200, new Character("Homer", "Simpson"))); 
     clickableAreas.add(new ClickableArea(600, 440, 130, 160, new Character("Bart", "Simpson"))); 
    } 

    // Listen for touches on your images: 
    @Override 
    public void onClickableAreaTouched(Object item) { 
     if (item instanceof Character) { 
      String text = ((Character) item).getFirstName() + " " + ((Character) item).getLastName(); 
      Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); 
     } 
    } 
... 
} 
+0

如果我需要檢查非矩形區域(如圓圈)中的點擊,該怎麼辦?它與你的圖書館一起工作嗎?謝謝。 – trinity420

0

提高@sealz回覆,在這裏我的答案:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    > 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_centerInParent="true" 
     android:layout_alignTop="@+id/mail" 
     android:layout_alignBottom="@+id/mail" 
     > 
     <Button 
      android:id="@+id/leftMail" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight=".5" 
      android:background="@android:color/transparent" 
      /> 
     <Button 
      android:id="@+id/rightMail" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight=".5" 
      android:background="@android:color/transparent"  
      /> 
     </LinearLayout> 
    <ImageView 
     android:id="@+id/mail" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/mail1600x600" 
     android:layout_centerInParent="true" 
     /> 
</RelativeLayout>