Why does the value of X does not go all the way to the edge of the layout

十年热恋 提交于 2019-12-31 05:29:50

问题


palette2 drawable which is used as a background:

esquare drawable (resized using @dimen and used as a thumb):

XML:

<LinearLayout
                android:id="@+id/llColorSpect"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_alignParentBottom="true" >
                <RelativeLayout
                    android:id="@+id/rlColorSpect"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/palette2" >
                    <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:

llColors = (RelativeLayout) findViewById(R.id.rlColorSpect);
        llColors.setOnTouchListener(llTouch);

        bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.palette2);

        llColors.setBackground(getResources().getDrawable(R.drawable.palette2));

        w = bitmap.getWidth();
        h = bitmap.getHeight();

        Log.i("PALETTE WIDTH", "width" +w);
        Log.i("PALETTE HEIGHT", "height" +h);

        observer = llColors.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                width = llColors.getWidth();
                height = llColors.getHeight();

                Log.i("LAYOUT WIDTH", "width" +width);
                Log.i("LAYOUT HEIGHT", "height" +height);
                //in here, place the code that requires you to know the dimensions.
                //Place your code here
            }
        });

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

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            x = (int)event.getX();
            y = (int)event.getY();
            if (x<0) {
                ivTouch.setX(0);
                ivTouch.setY(y);
                x=0;
                Toast.makeText(getApplicationContext(), "x=0", 2000).show();
            }
            if (y<0) {
                ivTouch.setY(0);
                ivTouch.setX(x);
                y=0;
                Toast.makeText(getApplicationContext(), "y=0", 2000).show();
            }
            if (x>bitmap.getWidth()) {
                ivTouch.setX(bitmap.getWidth());
                x=bitmap.getWidth()-1;
                Toast.makeText(getApplicationContext(), "x=bitmap.maxwidth(}", 2000).show();
            }
            if (y>bitmap.getHeight()) {
                ivTouch.setY(bitmap.getHeight());
                y=bitmap.getHeight()-20;
                Toast.makeText(getApplicationContext(), "y=bitmap.maxheight(}", 2000).show();
            }
            if (x>0 && x<bitmap.getWidth() || y>0 || y<bitmap.getHeight()) {
                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);
                        inRed = Color.red(pixel);
                        inBlue = Color.blue(pixel);
                        inGreen = Color.green(pixel);
                        Log.d("Colors","R:" +inRed +" G:" +inGreen+" B:" + inBlue);
                        dispHVal(inRed, inGreen, inBlue);
                        hexToRGB();
                        break;
                    }
                    case MotionEvent.ACTION_MOVE:{
                        Log.i("COORDINATES","Touch coordinates : x" + String.valueOf(x) + "y" + String.valueOf(y));
                        ivTouch.setX(x);
                        ivTouch.setY(y);
                        inRed = Color.red(pixel);
                        inBlue = Color.blue(pixel);
                        inGreen = Color.green(pixel);
                        Log.d("Colors","R:" +inRed +" G:" +inGreen+" B:" + inBlue);
                        dispHVal(inRed, inGreen, inBlue);
                        hexToRGB();
                        break;
                    }
                }
            }
            return true;
        }
    };

The LogCat displays the following:

01-31 22:55:38.729: I/LAYOUT WIDTH(23021): width660
01-31 22:55:38.729: I/LAYOUT HEIGHT(23021): height470
01-31 22:55:38.744: I/COORDINATES(23021): Touch coordinates : x579y89
01-31 22:55:38.744: D/Colors(23021): R:226 G:226 B:226
01-31 22:55:38.760: I/LAYOUT WIDTH(23021): width660
01-31 22:55:38.760: I/LAYOUT HEIGHT(23021): height470

Does anyone know why I can only drag to 579 (the x only goes up to 579), while the layout width is 660 and how do I fix it?

EDIT: What I am looking to do is allow the user to drag anywhere within the RelativeLayout and where the user drags, the thumb moves with the finger but stays within the layout. I want to be able to retrieve the background as bitmap so I can convert to RGB values. How do I accomplish that?


回答1:


Can you go to the left edge of the image as you expect? What about top and bottom?

Have you tried any other Android device?

Note that your finger is much bigger than pixels, so the touch screen needs to round it. In general, it should report the center of your finger, but depending on the quality, it may not be doing a good job at it. In particular, it may not be able to get any information about your finger outside of the active area (and then not report any touch if too much of your finger is outside of the screen).

You could use an app like Touch Test to see how your device behaves. And first check that you can actually reach the edge with this. If you don't then your problem is not in your code but with the device.



来源:https://stackoverflow.com/questions/21493913/why-does-the-value-of-x-does-not-go-all-the-way-to-the-edge-of-the-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!