Si8
Si8

Reputation: 9235

How to convert a layout's background into bitmap

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>

Upvotes: 0

Views: 408

Answers (1)

Matan Dahan
Matan Dahan

Reputation: 389

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

Upvotes: 1

Related Questions