我有自定义视图,它在视图内绘制一系列矩形形状。 我已经扩展了视图并重写了onDraw方法。Android中不同屏幕尺寸的自定义形状
这是构造器(添加只是重要的部分)
public CustomDrawableView(Context mContext , AttributeSet attr)
{
super(mContext,attr);
//setMeasuredDimension(measuredWidth, measuredHeight)
Log.e("CustomDrableView", "widht"+attr.getAttributeName(5));
Log.e("CustomDrableView", "widht2"+attr.getAttributeValue(5));
mColors = new int[] {
//color codes
};
mColors_state_init = new int[]
{
//color codes
};
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint2 = new Paint(mPaint);
mPaint2.setAlpha(64);
float[] radii = {15,15,15,15,15,15,15,15};
mDrawable = new ShapeDrawable(new RoundRectShape(radii, null, null));
// mDrawable.getPaint().setColor(0xff74AC23);
ShapeDrawable prev = mDrawable;
mDrawables = new ShapeDrawable[15];
for (int i = 0; i < 15; i++) {
mDrawables[i] = new ShapeDrawable(new RoundRectShape(radii, null, null));
}
}
这onDraw有
protected void onDraw(Canvas canvas) {
mDrawable.setBounds(x, y, x + width, y + height);
for (int i = 0; i < 15; i++) {
mDrawables[i].getPaint().setColor(0xff74AC23);
mDrawables[i].setDither(true);
mDrawables[i].setBounds(x, y + (i *20), x+width, y+height+(i*20));
}
for(int i = 0 ; i < 15 ; i++)
{
ColorFilter filter = null;
if (mColors[i] == 0) {
filter = null;
} else {
filter = new PorterDuffColorFilter(mColors_test[i],
PorterDuff.Mode.SRC_ATOP);
}
mDrawables[i].setColorFilter(filter);
mDrawables[i].draw(canvas);
}
的宽度和高度值具有不同的屏幕尺寸改变。我怎样才能做到这一点?
我为xml中的视图指定了静态宽度和高度。我应该避免吗?