借助Android复合绘图

时间:2012-12-13 15:58:27

标签: android button drawable android-canvas

我正在尝试在常规旧Android按钮的Compound Drawable上画一条线。我开始只是尝试使用Button.getCompoundDrawables()[1]获取按钮的可绘制,但线条永远不会出现。所以,我继续在我的XML布局中放置一个实际图像,用于按钮上的复合可绘制。这样可以正常工作(图片中的橙色正方形),但是当手机旋转时,橙色正方形不会调整大小,按钮会旋转,所以它最终太大了。我需要在轮换时调用getBounds()吗?

必须对drawable进行一些调整大小,因为如果你注意到,红线会以水平方向进入角落,但不会进入垂直方向;它是关闭的东西。橙色方块在drawable- [lhm] dpi /目录中的大小不同,但我没有两个单独的水平和垂直布局。

绘制线条的代码:

    @Override
            public View getView (int position, View convertView, ViewGroup parent)
            {           
                View row = convertView;
                if (row == null)
                {
                    LayoutInflater inflater = (LayoutInflater) _context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
                    row = inflater.inflate (R.layout.monthview, parent, false);

                }


                btn_cell = (Button) row.findViewById (R.id.bcell);
...
                    BitmapDrawable btn_draw = (BitmapDrawable) btn_cell.getCompoundDrawables ()[1];

                    if (btn_draw != null)
                    {
                        Log.d (TAG, "+++++++++++++ drawing line");
                        Bitmap btn_bmp = btn_draw.getBitmap ();
                        Bitmap offscreen_bmp = Bitmap.createBitmap(btn_bmp.getWidth(), btn_bmp.getHeight(), btn_bmp.getConfig());
                        BitmapDrawable offscreen_draw = new BitmapDrawable (offscreen_bmp);
                        offscreen_draw.setBounds (btn_draw.getBounds ());

                        Canvas c = new Canvas(offscreen_bmp);

                        // draw line
                        Paint p = new Paint();
                        p.setAntiAlias(true);
                        p.setStrokeWidth(1);
                        p.setStyle(Style.FILL_AND_STROKE);
                        p.setColor(Color.RED);

                        c.drawBitmap (btn_bmp, 0, 0, p);
                        c.drawLine (0, 0, offscreen_bmp.getWidth (), offscreen_bmp.getHeight (), p);

                        (R.drawable.cal_left_arrow_off), null, null);
                        btn_cell.setCompoundDrawables(null, offscreen_draw, null, null);
                    }

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

如果它帮助了其他人,我最终取消了Button而不是使用ImageView。我遇到了一个(显然)典型的问题,就是如何在ImageView上画一条线。我知道在onDraw()方法中对ImageView进行子类化和绘制线条,但是我不知道你可以在Eclipse的XML Layout GUI中引用那个“自定义”子类。我需要做的就是创建一个myImageView类,然后在XML源代码中引用它。而不是指定<ImageView>我需要使用<com.example.myImageView>并且一切正常。

我在自定义BaseAdapter中使用myImageView,因此在getView()方法中放置myImageView上的行只是一个问题:

        myImageView iv = (myImageView) row.findViewById (R.id.iv_draw);
        iv.setBarLength (15);
        iv.setOnClickListener (ocl);

com.example.test.myImageView.java:

public class myImageView extends android.widget.ImageView
{
    private final String TAG = this.getClass ().getName ();
    private int mBarLen = 5;

    /**
     * @param context
     * @param attrs
     */
    public myImageView (Context context, AttributeSet attrs)
    {
        super (context, attrs);
    }

    public void setBarLength (int length)
    {
        mBarLen = length;
    }

    @Override
    protected void onDraw (Canvas canvas)
    {
        super.onDraw (canvas);

        // draw 1px border
        Paint p = new Paint ();

        int x = 1;
        int y = 1;
        Rect bounds = canvas.getClipBounds ();

        int x2 = bounds.right - 1;
        int y2 = bounds.bottom - 1;

        canvas.drawLine (x, y, x2, y, p);
        canvas.drawLine (x2, y, x2, y2, p);
        canvas.drawLine (x2, y2, x, y2, p);
        canvas.drawLine (x, y2, x, y, p);

        p.setColor (Color.RED);
        p.setStrokeWidth (2);
        int bx = x + 5;
        int by = y + 5;
        canvas.drawLine (bx, by, bx+mBarLen, by, p);
        canvas.drawLine (bx, by, bx+mBarLen, by, p);

    }

}

xml文件:

<com.example.test.myImageView
    android:id="@+id/iv_draw"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:adjustViewBounds="true"
    android:minHeight="48dp"
    android:minWidth="24dp"
    android:scaleType="fitXY" />