如何在Android GridView中获取ImageButton大小?

时间:2012-12-18 17:02:11

标签: android layout graphics

我正在对ImageButton进行子类化,以便在其上绘制线条并尝试找出实际按钮坐标在gridview中的位置。我使用onGlobalLayout来设置Top,Bottom,Right和Left,但这些似乎是网格中的实际“方形”,而不是实际的按钮(见图)。使用从myImageButton.onGlobalLayout()收集的coords在myImageButton.onDraw()中绘制紫色线条。我认为这些是按钮,但它们似乎来自其他东西。不知道是什么。我希望紫色线条与按钮的轮廓相匹配,这样我绘制的线条就会出现在按钮上,而不仅仅是在某个地方的LinearLayout中浮出来。浅蓝色是垂直LinearLayout的背景颜色,它包含Textview(对于数字)和myImageButton。有没有办法获得实际的按钮大小?

gridview/layout/button

XML布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lay_cellframe"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="fill_vertical|fill_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_cell"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:gravity="center"
            android:text="TextView"
            android:textSize="10sp" />

        <com.example.icaltest2.myImageButton
            android:id="@+id/imageButton1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_margin="0dp"
            android:adjustViewBounds="false"
            android:background="@android:drawable/btn_default"
            android:scaleType="fitXY"
            android:src="@android:color/transparent" />

    </LinearLayout>
</FrameLayout>

myImageButton.java

   public myImageButton (Context context, AttributeSet attrs)
   {
       super (context, attrs);
       mBounds = new Rect();
       ViewTreeObserver vto = this.getViewTreeObserver ();
       vto.addOnGlobalLayoutListener (ogl);
       Log.d (TAG, "myImageButton");
   }


...

    OnGlobalLayoutListener ogl = new OnGlobalLayoutListener()
        {


            @Override
            public void onGlobalLayout ()
            {
                Rect b = getDrawable ().getBounds ();


                mBtnTop = b.centerY () - (b.height () / 2);
                mBtnBot = b.centerY () + (b.height () / 2);
                mBtnLeft = b.centerX () - (b.width () / 2);
                mBtnRight = b.centerX () + (b.width () / 2);

            }
        };
...

 @Override
   protected void onDraw (Canvas canvas)
   {

      super.onDraw (canvas);
      Paint p = new Paint ();
        p.setStyle (Paint.Style.STROKE);
        p.setStrokeWidth (1);

        p.setColor (Color.MAGENTA);
        canvas.drawCircle (mBtnLeft, mBtnTop, 2, p);
        canvas.drawCircle (mBtnLeft, mBtnBot, 2, p);
        canvas.drawCircle (mBtnRight, mBtnTop, 2, p);
        canvas.drawCircle (mBtnRight, mBtnBot, 2, p);
            canvas.drawRect (mBtnLeft, mBtnTop, mBtnRight, mBtnBot, p);


}

更新:使用jsmith的建议添加了图片

Rect r = canvas.getClipBounds (); //<- not sure about this
        int w = getMeasuredWidth () - getPaddingLeft () - getPaddingRight ();
        int h = getMeasuredHeight () - getPaddingTop () - getPaddingBottom ();

        int left = r.centerX () - (w / 2);
        int right = r.centerX() + ( w / 2);
        int top = r.centerY() - (h / 2);
        int bot = r.centerY() + (h / 2);


        p.setColor (Color.GREEN);
        canvas.drawRect (left, top, right, bot, p);

enter image description here

2 个答案:

答案 0 :(得分:1)

我认为你所做的是对的,问题是Android的标准按钮样式有大约3dip的填充,所以看起来图像与gridview的单元格对齐,但事实并非如此。

因此,线条需要有更少的6dip宽度(左边3个,右边3个),以适应按钮的视图。

mBtnLeft = b.centerX () - (b.width () / 2) + 3;
mBtnRight = b.centerX () + (b.width () / 2) - 3;

答案 1 :(得分:1)

当onDraw被调用时,处理了布局并且大小信息应该可用。您可以使用View.getMeasuredWidth() / View.getMeasuredHeight()来检索所需的信息。但是,您可能还需要使用View.getPaddingLeft() / ..来填充填充。例如:

final int displayWidth = (getMeasuredWidth() - getPaddingLeft() - getPaddingRight());