2015-06-24 49 views
0

在我的應用程序可點擊的imageview(Android Studio)中,我使用了透明背景的PNG圖像,但是當我運行該應用程序時,它看起來像當我點擊圖像附近點擊時,你能幫我嗎 !如何從PNG圖像製作可點擊的圖像

以下是截圖: 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代碼的

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

請寄出logcat。 –

+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