如何在android中获取Imageview像素

时间:2013-07-22 11:18:38

标签: android image-processing android-imageview

在我想要pick image from gallery的项目中,用户可以触摸该图片pixel color store into my db.

问题:

它会给我例外:y must be <bitmap.height()

logcat的:

07-22 16:38:59.406: E/AndroidRuntime(6139): java.lang.IllegalArgumentException: y must be < bitmap.height()
07-22 16:38:59.406: E/AndroidRuntime(6139):     at android.graphics.Bitmap.checkPixelAccess(Bitmap.java:788)
07-22 16:38:59.406: E/AndroidRuntime(6139):     at android.graphics.Bitmap.getPixel(Bitmap.java:740)
07-22 16:38:59.406: E/AndroidRuntime(6139):     at com.example.mycolorreader.PhotoActivity$2.onTouch(PhotoActivity.java:121)
07-22 16:38:59.406: E/AndroidRuntime(6139):     at android.view.View.dispatchTouchEvent(View.java:3762)
07-22 16:38:59.406: E/AndroidRuntime(6139):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
07-22 16:38:59.406: E/AndroidRuntime(6139):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
07-22 16:38:59.406: E/AndroidRuntime(6139):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
07-22 16:38:59.406: E/AndroidRuntime(6139):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
07-22 16:38:59.406: E/AndroidRuntime(6139):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)

代码:

final Bitmap bitmap = ((BitmapDrawable) ivPhoto.getDrawable()).getBitmap();
ivPhoto.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        int pixel = bitmap.getPixel(x, y);

我正在尝试更多像set padding,设置Image layout动态但总是得到这个异常。

我在堆栈溢出 Google 上进行了大量搜索,但未获得积极响应。

请给我一些想法。

1 个答案:

答案 0 :(得分:5)

事件的x / y是触摸坐标。要获得触摸位图的像素,我们应该首先将它们转换为位图坐标。这个程序很好描述here