java - 如果用户向左或向右按​​太多,则应用程序为 FC

标签 java android eclipse android-layout

我有以下 XML:

<LinearLayout
    android:id="@+id/llColorSpect"
    android:layout_width="match_parent"
    android:layout_height="@dimen/color_scheme_height"
    android:orientation="vertical"
    android:background="@drawable/colorspect"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginBottom="@dimen/seek_bar_margin"
    android:layout_below="@+id/tvBGColor" >
    <RelativeLayout
        android:id="@+id/rlColorSpect"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <ImageView
            android:id="@+id/ivSquare"
            android:layout_width="@dimen/title_text_pad"
            android:layout_height="@dimen/title_text_pad"
            android:layout_alignParentBottom="true"
            android:scaleType="fitXY"
            android:src="@drawable/esquare" />
    </RelativeLayout>
</LinearLayout>

我的部分Java代码:

View.OnTouchListener llTouch = new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int x = (int)event.getX();
        int y = (int)event.getY();
        int action = event.getAction();
        int pixel = bitmap.getPixel((int)x,(int) y);

        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN: {
                Log.i("COORDINATES","Touch coordinates : x" + String.valueOf(x) + "y" + String.valueOf(y));
                ivTouch.setX(x);
                ivTouch.setY(y);
                int redValue = Color.red(pixel);
                int blueValue = Color.blue(pixel);
                int greenValue = Color.green(pixel);
                Log.d("Colors","R:" +redValue +" G:" +greenValue+" B:" + blueValue);
                sbRed.setProgress(redValue);
                sbGreen.setProgress(greenValue);
                sbBlue.setProgress(blueValue);
                dispHVal();
            break;
            }
            case MotionEvent.ACTION_MOVE:{
                Log.i("COORDINATES","Touch coordinates : x" + String.valueOf(x) + "y" + String.valueOf(y));
                ivTouch.setX(x);
                ivTouch.setY(y);
                int redValue = Color.red(pixel);
                int blueValue = Color.blue(pixel);
                int greenValue = Color.green(pixel);
                Log.d("Colors","R:" +redValue +" G:" +greenValue+" B:" + blueValue);
                sbRed.setProgress(redValue);
                sbGreen.setProgress(greenValue);
                sbBlue.setProgress(blueValue);
                dispHVal();
            break;
            }
        }
        return true;
    }
};

我遇到的问题。

  1. 当按下布局以获取其特定部分的坐标时(如果我一直向左或向右按​​下/拖动),我的应用程序 FC.我在下面添加 LogCat:

01-25 01:13:21.059: E/AndroidRuntime(13345): FATAL EXCEPTION: main 01-25 01:13:21.059: E/AndroidRuntime(13345): java.lang.IllegalArgumentException: x must be < bitmap.width() 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.graphics.Bitmap.checkPixelAccess(Bitmap.java:1155) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.graphics.Bitmap.getPixel(Bitmap.java:1107) 01-25 01:13:21.059: E/AndroidRuntime(13345): at com.test.testing.MainActivity$1.onTouch(MainActivity.java:98) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.View.dispatchTouchEvent(View.java:7241) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875) 01-25 01:13:21.059: E/AndroidRuntime(13345): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953) 01-25 01:13:21.059: E/AndroidRuntime(13345): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.app.Activity.dispatchTouchEvent(Activity.java:2410) 01-25 01:13:21.059: E/AndroidRuntime(13345): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.View.dispatchPointerEvent(View.java:7426) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.os.MessageQueue.nativePollOnce(Native Method) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.os.MessageQueue.next(MessageQueue.java:125) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.os.Looper.loop(Looper.java:124) 01-25 01:13:21.059: E/AndroidRuntime(13345): at android.app.ActivityThread.main(ActivityThread.java:5195) 01-25 01:13:21.059: E/AndroidRuntime(13345): at java.lang.reflect.Method.invokeNative(Native Method) 01-25 01:13:21.059: E/AndroidRuntime(13345): at java.lang.reflect.Method.invoke(Method.java:511) 01-25 01:13:21.059: E/AndroidRuntime(13345): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) 01-25 01:13:21.059: E/AndroidRuntime(13345): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 01-25 01:13:21.059: E/AndroidRuntime(13345): at dalvik.system.NativeStart.main(Native Method)

我想让用户能够一直向左、上、右、下拖动。我如何修改上面的代码来实现这一点?谢谢!!!

最佳答案

  1. 在访问位图之前将 x 和 y 日志记录移至以查看返回的值。

  2. 根据堆栈跟踪,请勿使用超出位图尺寸的 x 和 y 值调用 getPixel。

关于java - 如果用户向左或向右按​​太多,则应用程序为 FC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21347431/

相关文章:

java - 队列中的多线程作业 - 作业太多?

JavaFx 节点大小

android - 为工作区中的每个项目创建 ant build.xml 文件

eclipse - 二郎。有人可以帮我弄清楚为什么在我尝试运行程序时会抛出这个错误吗?

java - 奇怪的 jackson 非法字符((CTRL-CHAR,代码0)) map 缩减组合器中的异常

java - 将图像添加到 Canvas 窗口

android - sonar lint 插件无法读取 lint-results.xml

java - 如何终止我的应用程序的远程进程?

android - 如何将 Firebase Auth token 传递给 webView 并在 Android 上注册通知

java - 如何在不运行该系统的情况下获取方法的调用者(用 JAVA 编写)