2015-06-24 2 views
0

В моем приложении clickable imageview (Android Studio), и я использовал изображение PNG с прозрачным фоном, но когда я запускаю приложение, похоже, когда я нажимаю рядом с изображением, на которое его нажимают, Можете ли вы мне помочь !Как сделать clickable изображение из png image

Вот скриншот: http://i.stack.imgur.com/AhN1y.jpg

Мой XML код

<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" 
android:background="@color/dim_foreground_material_light"> 


<ImageView 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:layout_weight="16" 
    android:id="@+id/logo" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:src="@drawable/logo" 
    android:clickable="true"/> 

Java Code

package com.example.bibiwars.skills; 

import android.content.DialogInterface; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.ImageView; 
import android.widget.Toast; 


public class MainActivity extends ActionBarActivity { 

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




      ImageView img = (ImageView) findViewById(R.id.logo); 
      img.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      Toast.makeText(MainActivity.this, "Clicked!!", Toast.LENGTH_LONG).show(); 
     } 

    }); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 



} 

Я не знаю, почему приложение всегда падает? как я могу решить эту проблему?

::: Это LogCat :::

06-24 13:28:09.815 1922-1922/com.example.bibiwars.skills I/art﹕ Not late-enabling -Xcheck:jni (already on) 06-24 13:28:10.998 1922-1953/com.example.bibiwars.skills D/OpenGLRenderer﹕ Render dirty regions requested: true 06-24 13:28:10.999 1922-1922/com.example.bibiwars.skills D/﹕ HostConnection::get() New Host Connection established 0xa6c85570, tid 1922 06-24 13:28:11.042 1922-1922/com.example.bibiwars.skills D/Atlas﹕ Validating map... 06-24 13:28:14.228 1922-1953/com.example.bibiwars.skills D/﹕ HostConnection::get() New Host Connection established 0xa6c856b0, tid 1953 06-24 13:28:14.243 1922-1953/com.example.bibiwars.skills I/OpenGLRenderer﹕ Initialized EGL, version 1.4 06-24 13:28:14.567 1922-1953/com.example.bibiwars.skills D/OpenGLRenderer﹕ Enabling debug mode 0 06-24 13:28:14.728 1922-1953/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 13:28:14.728 1922-1953/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c7ac00, error=EGL_SUCCESS 06-24 13:28:14.887 1922-1922/com.example.bibiwars.skills I/Choreographer﹕ Skipped 39 frames! The application may be doing too much work on its main thread. 06-24 13:28:20.268 1922-1922/com.example.bibiwars.skills I/Choreographer﹕ Skipped 322 frames! The application may be doing too much work on its main thread. 06-24 13:28:36.548 1922-1922/com.example.bibiwars.skills I/Choreographer﹕ Skipped 41 frames! The application may be doing too much work on its main thread. 06-24 13:28:36.644 1922-1953/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 13:28:36.644 1922-1953/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c7ac00, error=EGL_SUCCESS 06-24 13:28:37.712 1922-1922/com.example.bibiwars.skills I/Choreographer﹕ Skipped 67 frames! The application may be doing too much work on its main thread. 06-24 13:28:41.599 1922-1953/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 13:28:41.599 1922-1953/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c7ac00, error=EGL_SUCCESS 06-24 13:28:52.650 1922-1922/com.example.bibiwars.skills I/Choreographer﹕ Skipped 644 frames! The application may be doing too much work on its main thread. 06-24 13:29:15.022 1922-1953/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 13:29:15.022 1922-1953/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6cd3b20, error=EGL_SUCCESS 06-24 13:29:20.624 1922-1953/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 13:29:20.624 1922-1953/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6cd3b20, error=EGL_SUCCESS 06-24 13:29:31.927 1922-1953/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 13:29:31.927 1922-1953/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6cd3b20, error=EGL_SUCCESS 06-24 13:35:56.876 1922-1953/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 13:35:56.876 1922-1953/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6cd3b20, error=EGL_SUCCESS 06-24 14:20:41.195 2422-2422/com.example.bibiwars.skills I/Choreographer﹕ Skipped 191 frames! The application may be doing too much work on its main thread. 06-24 14:20:53.827 2422-2437/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 14:20:53.827 2422-2437/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa685b980, error=EGL_SUCCESS 06-24 14:20:54.379 2422-2422/com.example.bibiwars.skills I/Choreographer﹕ Skipped 30 frames! The application may be doing too much work on its main thread. 06-24 14:21:08.637 2422-2437/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 14:21:08.637 2422-2437/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa68c73c0, error=EGL_SUCCESS 06-24 14:21:12.162 2422-2437/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 14:21:12.162 2422-2437/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa68c73c0, error=EGL_SUCCESS 06-24 14:21:16.752 2422-2437/com.example.bibiwars.skills W/EGL_emulation﹕ eglSurfaceAttrib not implemented 06-24 14:21:16.752 2422-2437/com.example.bibiwars.skills W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa68c73c0, error=EGL_SUCCESS

+1

Отправьте ловушку, пожалуйста. –

+0

Похожие - http://stackoverflow.com/questions/15343266/how-to-prevent-onclick-method-on-transparent-portion-of-a-png-loaded-imageview –

+0

Где вы используете диалоговый интерфейс? Не могли бы вы попытаться удалить свой импорт android.content.DialogInterface? –

ответ

0

Если вы имеете в виду, что площадь квадрата, охватывающему cirle, это должно произойти, чтобы предотвратить, что вы должны указать что вы не хотите касаться там.

Для этого сделать это,

  1. Вычислить X и Y координаты изображения.
  2. Рассчитать ширину и высоту изображения.
  3. В onClick получить координаты X 'и Y' на ощупь. Вычислить

    touch = (X'- X)^2 + (Y'- Y)^2 - (Width/2)^2 
    
    if(touch<=0) 
        //It's a Click!! 
    else 
        //It's in the transparent area. 
    

Но это будет работать только для кругов. Даже если вы создаете прозрачное изображение, прозрачная область не получает сенсорных событий. Каждое изображение представляет собой прямоугольник.

+0

Что случилось, я решил это! Вы приняли ответ. – Mayank

+0

да, но после этого я не мог сделать это в одиночку ... Я все еще новичок, так что, пожалуйста, покажите мне, как получить эти X, Y, X ', Y', ширину, высоту? то я с уверенностью могу согласиться! –

+0

Будучи новичком, не прошу вас копировать коды, используйте onMeasure. и сделать некоторые исследования – Mayank