本文介绍了得到抛出:IllegalArgumentException而试图使透明区域的图像按钮是不可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到非法的异常,而试图使图像按钮不可点击的透明区域。如何解决这个问题:我已实施 OnTouchListner 和实施 loadBitmapFromView(视图V)

code是:

 包com.sunojsworkshop;进口android.os.Bundle;
进口android.app.Activity;
进口android.graphics.Bitmap;
进口android.graphics.Canvas;
进口android.graphics.Color;进口android.view.MotionEvent;
进口android.view.View;进口android.view.View.OnTouchListener;进口android.widget.ImageButton;
进口android.widget.Toast;公共类MainActivity延伸活动{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        的ImageButton imgView =(的ImageButton)findViewById(R.id.imageButton1);
         imgView.setDrawingCacheEnabled(真);
         imgView.setOnTouchListener(新OnTouchListener(){
        //私人最终OnTouchListener changeColorListener =新OnTouchListener(){             公共布尔onTouch(视图V,MotionEvent事件){
                  BMP位图= loadBitmapFromView(V);
                  INT颜色= bmp.getPixel((int)的event.getX(),(INT)event.getY());
                 如果(颜色== Color.TRANSPARENT)
                  返回false;
                  其他{
                 // code执行
                 Toast.makeText(MainActivity.this,图像按钮被点击Toast.LENGTH_SHORT).show();
                 返回true;
                      }
                    }        公共位图loadBitmapFromView(视图v){
            // TODO自动生成方法存根                 位图B = Bitmap.createBitmap(v.getLayoutParams()宽度,v.getLayoutParams()的高度,Bitmap.Config.ARGB_8888);
                 帆布C =新的Canvas(B);
                 v.layout(0,0,v.getLayoutParams()宽度,v.getLayoutParams()高度。);
                 v.draw(C);
                 返回b;
            }
                 });
    }
}

XML是:

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    机器人:背景=@绘制/ worldfinal
    工具:上下文=MainActivity。>    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=选择所需的国家/>    <的ImageButton
        机器人:ID =@ + ID / imageButton1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_below =@ + ID / textView1
        机器人:layout_marginRight =28dp
        机器人:layout_marginTop =93dp
        机器人:SRC =@绘制/ india1
        机器人:背景=@机器人:彩色/透明/>

和日志是:

  08-06 13:13:24.162:E / AndroidRuntime(329):致命异常:主要
08-06 13:13:24.162:E / AndroidRuntime(329):java.lang.IllegalArgumentException异常:宽度和高度必须是> 0
08-06 13:13:24.162:E / AndroidRuntime(329):在android.graphics.Bitmap.nativeCreate(本机方法)
08-06 13:13:24.162:E / AndroidRuntime(329):在android.graphics.Bitmap.createBitmap(Bitmap.java:477)
08-06 13:13:24.162:E / AndroidRuntime(329):在com.sunojsworkshop.MainActivity $ 1.loadBitmapFromView(MainActivity.java:49)
08-06 13:13:24.162:E / AndroidRuntime(329):在com.sunojsworkshop.MainActivity $ 1.onTouch(MainActivity.java:35)
08-06 13:13:24.162:E / AndroidRuntime(329):在android.view.View.dispatchTouchEvent(View.java:3881)
08-06 13:13:24.162:E / AndroidRuntime(329):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
08-06 13:13:24.162:E / AndroidRuntime(329):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
08-06 13:13:24.162:E / AndroidRuntime(329):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
08-06 13:13:24.162:E / AndroidRuntime(329):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
08-06 13:13:24.162:E / AndroidRuntime(329):在com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
08-06 13:13:24.162:E / AndroidRuntime(329):在com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
08-06 13:13:24.162:E / AndroidRuntime(329):在android.app.Activity.dispatchTouchEvent(Activity.java:2096)
08-06 13:13:24.162:E / AndroidRuntime(329):在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
08-06 13:13:24.162:E / AndroidRuntime(329):在android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
08-06 13:13:24.162:E / AndroidRuntime(329):在android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
08-06 13:13:24.162:E / AndroidRuntime(329):在android.os.Handler.dispatchMessage(Handler.java:99)
08-06 13:13:24.162:E / AndroidRuntime(329):在android.os.Looper.loop(Looper.java:123)
08-06 13:13:24.162:E / AndroidRuntime(329):在android.app.ActivityThread.main(ActivityThread.java:3683)
08-06 13:13:24.162:E / AndroidRuntime(329):在java.lang.reflect.Method.invokeNative(本机方法)
08-06 13:13:24.162:E / AndroidRuntime(329):在java.lang.reflect.Method.invoke(Method.java:507)
08-06 13:13:24.162:E / AndroidRuntime(329):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
08-06 13:13:24.162:E / AndroidRuntime(329):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-06 13:13:24.162:E / AndroidRuntime(329):在dalvik.system.NativeStart.main(本机方法)
    < / RelativeLayout的>


解决方案

  v.layout(0,0,v.getLayoutParams()。width方法,v.getLayoutParams()。高) ;

在所得到的值0这里,但布局需要至少为1的值。你有没有尝试在这里提供的值不同?

I'm getting illegal exception while trying to make transparent area of an image button unclickable. How to solve this issue: I have implemented OnTouchListner and implemented loadBitmapFromView(View v)

code is :

 package com.sunojsworkshop;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;

import android.view.MotionEvent;
import android.view.View;

import android.view.View.OnTouchListener;

import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends Activity {

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


        ImageButton imgView= (ImageButton) findViewById(R.id.imageButton1);
         imgView.setDrawingCacheEnabled(true);
         imgView.setOnTouchListener(new OnTouchListener(){




        //private final OnTouchListener changeColorListener = new OnTouchListener() {

             public boolean onTouch(View v, MotionEvent event) {
                  Bitmap bmp = loadBitmapFromView(v);
                  int color = bmp.getPixel((int) event.getX(),(int) event.getY());
                 if (color == Color.TRANSPARENT)
                  return false;
                  else {
                 //code to execute
                 Toast.makeText(MainActivity.this, "Image button is being clicked", Toast.LENGTH_SHORT).show();
                 return true;
                      }
                    }

        public Bitmap loadBitmapFromView(View v) {
            // TODO Auto-generated method stub

                 Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
                 Canvas c = new Canvas(b);
                 v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
                 v.draw(c);
                 return b;
            }


                 });


    }


}

XML is:

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SELECT A DESIRED COUNTRY" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginRight="28dp"
        android:layout_marginTop="93dp"
        android:src="@drawable/india1"
        android:background="@android:color/transparent"  />

and Log is:

 08-06 13:13:24.162: E/AndroidRuntime(329): FATAL EXCEPTION: main
08-06 13:13:24.162: E/AndroidRuntime(329): java.lang.IllegalArgumentException: width and height must be > 0
08-06 13:13:24.162: E/AndroidRuntime(329):  at android.graphics.Bitmap.nativeCreate(Native Method)
08-06 13:13:24.162: E/AndroidRuntime(329):  at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
08-06 13:13:24.162: E/AndroidRuntime(329):  at com.sunojsworkshop.MainActivity$1.loadBitmapFromView(MainActivity.java:49)
08-06 13:13:24.162: E/AndroidRuntime(329):  at com.sunojsworkshop.MainActivity$1.onTouch(MainActivity.java:35)
08-06 13:13:24.162: E/AndroidRuntime(329):  at android.view.View.dispatchTouchEvent(View.java:3881)
08-06 13:13:24.162: E/AndroidRuntime(329):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
08-06 13:13:24.162: E/AndroidRuntime(329):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
08-06 13:13:24.162: E/AndroidRuntime(329):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
08-06 13:13:24.162: E/AndroidRuntime(329):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
08-06 13:13:24.162: E/AndroidRuntime(329):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
08-06 13:13:24.162: E/AndroidRuntime(329):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
08-06 13:13:24.162: E/AndroidRuntime(329):  at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
08-06 13:13:24.162: E/AndroidRuntime(329):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
08-06 13:13:24.162: E/AndroidRuntime(329):  at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
08-06 13:13:24.162: E/AndroidRuntime(329):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
08-06 13:13:24.162: E/AndroidRuntime(329):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 13:13:24.162: E/AndroidRuntime(329):  at android.os.Looper.loop(Looper.java:123)
08-06 13:13:24.162: E/AndroidRuntime(329):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-06 13:13:24.162: E/AndroidRuntime(329):  at java.lang.reflect.Method.invokeNative(Native Method)
08-06 13:13:24.162: E/AndroidRuntime(329):  at java.lang.reflect.Method.invoke(Method.java:507)
08-06 13:13:24.162: E/AndroidRuntime(329):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-06 13:13:24.162: E/AndroidRuntime(329):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-06 13:13:24.162: E/AndroidRuntime(329):  at dalvik.system.NativeStart.main(Native Method)


    </RelativeLayout>
解决方案
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);

you are getting the value "0" here, but the layout needs at least a value of "1". Did you try supplying different values here?

这篇关于得到抛出:IllegalArgumentException而试图使透明区域的图像按钮是不可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:11