我有以下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;
}
};
我遇到的问题。
01-25 01:13:21.059:E / AndroidRuntime(13345):致命异常:主要 01-25 01:13:21.059:E / AndroidRuntime(13345): java.lang.IllegalArgumentException:x必须是&lt; 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(原生方法)
我想让用户能够一直向左,向上,向右和向下拖动。如何修改上面的代码来实现呢?感谢!!!
答案 0 :(得分:1)
在访问位图之前将x和y日志记录移动到以查看返回的值。
根据堆栈跟踪,不要使用超出位图尺寸的x和y值调用getPixel。