0

I'm trying to draw a drawable background with a path. But i getting to one point where i can't draw because i need the size of the object that will receive the background. Is there a function to find the size of the specific view that i will draw on ?

public class CustomDrawableEditText extends Drawable{
private Context context;
public CustomDrawableEditText(Context context) {
    // TODO Auto-generated constructor stub
    this.context = context;
}
@Override
public void draw(Canvas canvas) {
    // TODO Auto-generated method stub
    Path path = new Path();
    Paint paint = new Paint();
    path.moveTo(0, 0);
    path.lineTo(10, 0);
    path.moveTo(0, 0);
    path.lineTo(0, Y);
    path.lineTo(10,  Y);
    path.moveTo( X, 0);
    path.lineTo( X-10, 0);
    path.moveTo( X, 0);
    path.lineTo( X,  X);
    path.lineTo( X-10,  X);
    paint.setColor(context.getResources().getColor(R.color.orange));
    paint.setStrokeWidth(2);
    paint.setStyle(Paint.Style.STROKE);
    canvas.drawPath(path, paint);
}

private int getY() {
    final Resources res = context.getResources();
    final float scale = res.getDisplayMetrics().density;
    return (int) (res.getDimension(R.dimen.dim4) * scale + 0.5f);
}
@Override
public int getOpacity() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void setAlpha(int alpha) {
    // TODO Auto-generated method stub

}

@Override
public void setColorFilter(ColorFilter cf) {
    // TODO Auto-generated method stub

}

}
4

2 回答 2

0

调用Drawable.getBounds()orDrawable.copyBounds(Rect rect)方法。

这两个方法应该返回要绘制View的边界(当 aView的大小发生变化时,它会调用(应该调用)Drawable.setBounds每个Drawable具有适当参数值的方法)。然后,您自己的 custom-Drawable 可以通过调用或检索此值。ViewDrawableDrawable.getBounds()Drawable.copyBounds(Rect rect)

于 2013-03-04T16:25:06.480 回答
0

要获取View的大小,您可以调用getWidth()getHeight(). 请注意,您可能也对getMeasuredWidth()和感兴趣getMeasuredHeight()

确保在视图的布局过程之后调用这些方法。

于 2013-03-04T15:46:10.477 回答