在我的項目,我想pick image from gallery
然後用戶可以點擊該圖像ImageView的像素和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);
我嘗試像集填充更多的東西,設置圖像佈局動態的,但總能得到這個例外。
我搜索很多關於堆棧溢出和谷歌,但沒有得到正面迴應。
請給我一些想法。
x/y是屏幕座標。您應該將屏幕座標轉換爲位圖本地座標,然後調用getPixel。 – Leonidos
@Leonidos如何將屏幕座標轉換爲位圖。 – Harshid
@Leonidos我也試試這個鏈接。 http://stackoverflow.com/questions/12674941/how-to-get-coordinates-on-touch-event-on-bitmap-not-of-screen – Harshid