为什么X的值一路不会去布局的边缘 [英] Why does the value of X does not go all the way to the edge of the layout

查看:123
本文介绍了为什么X的值一路不会去布局的边缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

palette2 绘制它是用来作为背景:

palette2 drawable which is used as a background:

esquare 绘制(通过调整 @dimen 并用作大拇指):

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的:

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;
        }
    };

在LogCat中显示以下内容:

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

有谁知道为什么我只能拖到579(只上升到579的X),而布局宽度为660,如何解决呢?

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?

修改:我所希望做的是让用户在 RelativeLayout的和任意位置拖动,用户拖动,拇指动作用手指,但停留在布局中。我希望能够检索背景位图,所以我可以转换为RGB值。我该如何实现这个目标?

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?

推荐答案

你能去到图像的左边,你期待什么呢?那么顶部和底部?

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

您是否尝试过其他任何Android设备?

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).

您可以使用如触摸测试的应用程序怎么看你的设备的行为。而第一次检查,你实际上可以达到这个边缘。如果你不那么你的问题是不是在你的code,但与设备。

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.

这篇关于为什么X的值一路不会去布局的边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆