我正在尝试在常规旧 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);
}