0

I have a RelativeLayout which has a drawable background. How do I convert the background drawable into a bitmap so I can use it to get

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

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            x = (int)event.getX();
            y = (int)event.getY();
            int action = event.getAction();
            int pixel = bitmap.getPixel((int)x,(int) y);
        }

My XML is like this:

            <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>
Si8
  • 9,141
  • 22
  • 109
  • 221
  • Do you need to do it by code? and is your background changes during run? – Matan Dahan Feb 02 '14 at 18:42
  • I am trying to allow user to drag within a layout and while they drag the X and Y is converted to pixel hence the converting the drawable to bitmap. – Si8 Feb 02 '14 at 18:51
  • I still need you to answer my questions so I can help you.. Is your background a single unchanged drawable? means, you can get it's directory like R.drawable.palette2 and it will not be changed during runtime.. you mean somthing like this? Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.palette2 ); – Matan Dahan Feb 02 '14 at 18:56
  • Yes you got it right. But I can't use `decodeResource()` because I need to set the width and height of the bitmap to that of the layout. Would you happen to know how? I was looking into `createBitmap` but not sure how I could use it. I want to stretch the bitmap to fit the layout. – Si8 Feb 02 '14 at 19:12
  • Do you want to take a snapshot of your RelativeLayout? – frogatto Feb 02 '14 at 19:15
  • I looked all over the web but unable to find where the user can drag within a layout and as they drag there is an image the drag along with the move. And while they are moving... The X and Y value is generated which can be converted to pixels. I'm making a color finder app with a color palette. As the user move around the layout they ate presented with the RGB colors. I'm having the touches time. – Si8 Feb 02 '14 at 19:22

1 Answers1

1

you van get the Layout Dimensions like This:

RelativeLayout myRelativeLayout  = (LinearLayout) findViewById(R.id.rlColorSpect);
    int layoutWidth = myRelativeLayout.getWidth();
    int layoutHeight = myRelativeLayout.getHeight();

and resize the bitmap using it..

other option is for you to create RelativeLayout Rules for your bitmap after you have created it: here

Another idea is to put an invisible imagview on that layout and strech it with fill_parent so it will cover the whole layout and to measure that image to get the layout dimensions

Community
  • 1
  • 1
Matan Dahan
  • 390
  • 5
  • 10
  • I don't think I can use it on `onCreate()` because it shows up as 0 and 0... And my app FC. – Si8 Feb 02 '14 at 19:24
  • Sure I'll post the code in pastebin for you to look. – Si8 Feb 02 '14 at 20:16
  • Here is my Java code: http://pastebin.com/08fVupVU and my XML code: http://pastebin.com/JLzj8Rwq (Please let me know if you have any questions) – Si8 Feb 02 '14 at 20:28
  • I also have another image inside the layout which will be used as a thumb and a placeholder for when the user drags around the layout the thumb will move and be placed in last X and Y coordinate – Si8 Feb 04 '14 at 21:08