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?