如何将布局的背景转换为位图

时间:2014-02-02 18:40:11

标签: android bitmap

我有一个RelativeLayout,它有一个可绘制的背景。如何将背景drawable转换为位图,以便我可以使用它来获取

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

我的XML是这样的:

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

1 个答案:

答案 0 :(得分:1)

你可以获得像这样的布局维度:

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

并使用它调整位图大小..

其他选项是您在创建位图后为其创建RelativeLayout规则: here

另一个想法是在该布局上放置一个不可见的imagview并用fill_parent拉伸它,这样它将覆盖整个布局并测量该图像以获得布局尺寸

相关问题