如何从png图像制作可点击的imageview

How to make a clickable imageview from png image

在我的应用程序可点击图像视图 (Android Studio) 中,我使用了具有透明背景的 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. 计算图像的 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.
    

但是,这只适用于圈子。即使您创建了透明图像,透明区域也不会接收到触摸事件。每个图像都是一个矩形。