Android - 获取视图上的像素

时间:2012-11-07 11:20:11

标签: android android-layout android-widget

我需要将位置放入用户触摸的像素的imageview

我向我的imageviewer添加了OnTouch方法,但我认为它发送给我的像素不是我imageviewer中像素的位置,而是像素在整个视图中的位置。因为我得到它的颜色与我触摸的像素颜色不一样

miViewer.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    // do something here when the element is clicked

                    Bitmap miBitmap = ((BitmapDrawable)miViewer.getDrawable()).getBitmap();

                    int[] viewCoords = new int[2];
                    miViewer.getLocationOnScreen(viewCoords);

                    int touchX=(int)event.getX();
                    int touchY=(int)event.getY();

                    int imageX = touchX - viewCoords[0]; // viewCoords[0] is the X coordinate
                    int imageY = touchY - viewCoords[1]; // viewCoords[1] is the y coordinate

                    //el ontouch da coordenada globales
                Log.e("PixelsView", String.valueOf(imageX) +" " +String.valueOf(imageY));
                Log.e("Screenpx", String.valueOf(touchX) +" " +String.valueOf(touchY));
                int pixelInt=miBitmap.getPixel(imageX,imageY);




                MyColorClass myColorClass=new MyColorClass();
                colorViewer.setBackgroundColor(myColorClass.CalculateSimilarColor(pixelInt));


                }




                return true;
            }
        });

0 个答案:

没有答案