Java 为什么X的值没有一直延伸到布局的边缘
palette2可绘制,用作背景: esquare drawable使用@dimen调整大小并用作拇指: XML: 有人知道为什么我只能拖动到579,而x只能拖动到579,而布局宽度是660,我如何修复它Java 为什么X的值没有一直延伸到布局的边缘,java,android,xml,android-imageview,Java,Android,Xml,Android Imageview,palette2可绘制,用作背景: esquare drawable使用@dimen调整大小并用作拇指: XML: 有人知道为什么我只能拖动到579,而x只能拖动到579,而布局宽度是660,我如何修复它 编辑:我想做的是允许用户拖动RelativeLayout中的任何位置,在用户拖动的位置,拇指会随着手指移动,但会停留在布局中。我想能够检索作为位图的背景,以便我可以转换为RGB值。我如何做到这一点?您能按预期转到图像的左边缘吗?顶部和底部呢 你试过其他安卓设备吗 请注意,您的手指比像素大得多,
编辑:我想做的是允许用户拖动RelativeLayout中的任何位置,在用户拖动的位置,拇指会随着手指移动,但会停留在布局中。我想能够检索作为位图的背景,以便我可以转换为RGB值。我如何做到这一点?您能按预期转到图像的左边缘吗?顶部和底部呢 你试过其他安卓设备吗 请注意,您的手指比像素大得多,因此触摸屏需要将其环绕。一般来说,它应该报告你的手指中心,但取决于质量,它可能做得不好。特别是,如果您的手指太多位于屏幕之外,它可能无法在活动区域之外获取有关您手指的任何信息,然后无法报告任何触摸
你可以使用类似的应用程序来查看设备的行为。首先检查你是否能用这个到达边缘。如果没有,那么问题不在代码中,而在设备上。dimen/title\U text\U pad的值是多少。还有图像的尺寸是多少?对于默认屏幕,它的10dp x 10dpYes。。。左和右的工作没有任何问题。当我走到底部时,小正方形离开了布局,它不会一直走到右边。。我将使用链接进行测试。我可以发送给你的项目,如果你想。我想知道我是否应该使用createBitmap或resizeDrawable的布局高度和宽度。我有点困惑,你说现在左和右是好的,但在问题中,你说你的问题是X只去579,而不是所有的方式660。。。那是哪一个呢?您可以将代码简化一点,而不是发送项目。例如,我不知道您的代码中的ivTouch是什么?这与这里的问题有关吗?所有颜色都一样。。。你能摆脱那些代码,仍然显示出你遇到了什么问题吗。。。左边和上面都很好。我有一个调色板,用户可以拖动,应用程序将获得位图的X和Y坐标,并生成RGB颜色。我将清理代码,使其更符合我的问题。我想如果你看看这个项目,你会更好地理解它。谢谢你更详细地回答我的问题。
<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>
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;
}
};
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