简体   繁体   English

在Android复合可绘制对象上绘制

[英]Drawing on an Android compound drawable

I'm trying to draw a line on the Compound Drawable of a regular old Android button. 我正在尝试在常规的旧Android按钮的Composite Drawable上画一条线。 I started out by just trying to get the drawable of the button using Button.getCompoundDrawables()[1] but the lines would never show up. 我刚开始尝试使用Button.getCompoundDrawables()[1]来获取按钮的可绘制对象,但这些行将永远不会显示。 So, I went ahead and put an actual image in my XML layout for the compound drawable on the button. 因此,我继续将实际图像放入XML布局中,用于按钮上可绘制的复合图像。 This works OK (the orange square in the pic) but when the phone is rotated, the orange square doesn't resize, proprotional to the button, so it ends up being way too big. 这样可以正常工作(图片中的橙色方块),但是旋转手机时,橙色方块不会按按钮的大小进行调整,因此最终会变得太大。 Do I need to be calling getBounds() upon rotation or something? 我需要在旋转时调用getBounds()吗?

There must be some resizing going on with the drawable because if you notice, the red line goes to the corners in Horizontal orientation, but not in the vertical; 可绘制对象必须进行一些调整大小,因为如果您注意到,红线在水平方向上到达角,但在垂直方向上到达; it's off or something. 没事了。 The orange square resides as different sizes in drawable-[lhm]dpi/ directories, but I do not have two separate layouts for horizontal and vertical. 橙色正方形在drawable- [lhm] dpi /目录中以不同的大小驻留,但是我没有水平和垂直两个单独的布局。

Code for drawing the line: 画线的代码:

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

在此处输入图片说明在此处输入图片说明

In case it helps someone else, I ended up doing away with the Button and instead using an ImageView. 万一它对其他人有帮助,我最终放弃了Button,而是使用ImageView。 I ran into a (apparently) typical problem of just how to draw a line on the ImageView. 我遇到了一个(显然)典型的问题,即如何在ImageView上画一条线。 I knew about sub-classing ImageView and drawing lines in the onDraw() method, however I didn't know you could reference that "custom" sub-class inside Eclipse's XML Layout GUI. 我知道在onDraw()方法中子类化ImageView和绘图线,但是我不知道您可以在Eclipse的XML Layout GUI中引用“自定义”子类。 All I needed to do was create a myImageView class and then reference it in the XML source code. 我需要做的就是创建一个myImageView类,然后在XML源代码中引用它。 Instead of specifying <ImageView> I needed to use <com.example.myImageView> and everything worked. 无需指定<ImageView>我需要使用<com.example.myImageView> ,一切正常。

I was using myImageView in a custom BaseAdapter so getting the lines placed on myImageView in the getView() method was just a matter of: 我在自定义BaseAdapter中使用myImageView,因此在getView()方法中将行放置在myImageView上只是一个问题:

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

com.example.test.myImageView.java: 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 file: 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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM