Android 以编程方式创建LayeredDrawable
这个答案,只是帮助了提问的人,并没有真正教会他们什么。所以在这里,我试图做一些类似的事情,需要一些帮助 我想要非常接近这个的东西:Android 以编程方式创建LayeredDrawable,android,Android,这个答案,只是帮助了提问的人,并没有真正教会他们什么。所以在这里,我试图做一些类似的事情,需要一些帮助 我想要非常接近这个的东西: 它们有一个动态指定的背景色,如果没有指定颜色,则为#eeeeee 它们有一个1像素的边框颜色#8888888,不透明度为70%(因此它与背景的信封颜色相同) 在该边框内,顶部有一条1px的白线,不透明度为50% 底部边框应替换为1px黑线,不透明度为90% 不,因为背景颜色是动态确定的,所以我发现我不能使用XML drawable来创建它,但是我认为它可以通过编程
我的具体问题是如何使用分层绘图来创建它。下面的类通过自定义ShapeDrawable而不是自定义LayerDrawable来实现您想要的效果。 请注意,
绘制线
调用中的所有坐标偏移都是为了防止重叠,并确保我们可以看到周长周围线条的全宽
public class MyShapeDrawable extends ShapeDrawable {
private int startX, startY, endX, endY;
private float mLineWidth = 1f;
private Paint mLinePaint;
public MyShapeDrawable() {
// No color specified, so call constructor with default color White
this(Color.WHITE);
}
public MyShapeDrawable(int color) {
// use the setter defined below, to set the main color for this drawable
setColor(color);
// setup the Paint for drawing the lines
mLinePaint = new Paint();
mLinePaint.setStyle(Paint.Style.STROKE);
mLinePaint.setStrokeWidth(mLineWidth);
}
public void setColor(int color) {
Paint paint = getPaint();
paint.setColor(color);
}
public void setLineWidth(float lineWidth) {
mLineWidth = lineWidth;
mLinePaint.setStrokeWidth(mLineWidth);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
// bottom black line
////////////////////
mLinePaint.setColor(Color.BLACK);
mLinePaint.setAlpha((int) (255 * 0.9)); // Opacity 90%
canvas.drawLine(
getBounds().left, getBounds().bottom - mLineWidth * 0.5f,
getBounds().right, getBounds().bottom - mLineWidth * 0.5f,
mLinePaint);
// translucent grey rim
///////////////////////
mLinePaint.setColor(Color.parseColor("#888888"));
mLinePaint.setAlpha((int) (255 * 0.7)); // Opacity 70%
// top
canvas.drawLine(
getBounds().left, getBounds().top + mLineWidth * 0.5f,
getBounds().right, getBounds().top + mLineWidth * 0.5f,
mLinePaint);
// left
canvas.drawLine(
getBounds().left + mLineWidth * 0.5f, getBounds().bottom - mLineWidth,
getBounds().left + mLineWidth * 0.5f, getBounds().top + mLineWidth,
mLinePaint);
// right
canvas.drawLine(
getBounds().right - mLineWidth * 0.5f, getBounds().bottom - mLineWidth,
getBounds().right - mLineWidth * 0.5f, getBounds().top + mLineWidth,
mLinePaint);
// top white line
/////////////////
mLinePaint.setColor(Color.WHITE);
mLinePaint.setAlpha((int) (255 * 0.5)); // Opacity 50%
canvas.drawLine(
getBounds().left + mLineWidth, getBounds().top + mLineWidth * 1.5f,
getBounds().right - mLineWidth, getBounds().top + mLineWidth * 1.5f,
mLinePaint);
}
您可以按如下方式使用该类:
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
MyShapeDrawable myShapeDrawable = new MyShapeDrawable(getResources().getColor(R.color.yellow));
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.test);
frameLayout.setBackgroundDrawable(myShapeDrawable);
}
}
下面的类通过自定义ShapeDrawable而不是自定义LayerDrawable实现了您想要的功能。 请注意,
绘制线
调用中的所有坐标偏移都是为了防止重叠,并确保我们可以看到周长周围线条的全宽
public class MyShapeDrawable extends ShapeDrawable {
private int startX, startY, endX, endY;
private float mLineWidth = 1f;
private Paint mLinePaint;
public MyShapeDrawable() {
// No color specified, so call constructor with default color White
this(Color.WHITE);
}
public MyShapeDrawable(int color) {
// use the setter defined below, to set the main color for this drawable
setColor(color);
// setup the Paint for drawing the lines
mLinePaint = new Paint();
mLinePaint.setStyle(Paint.Style.STROKE);
mLinePaint.setStrokeWidth(mLineWidth);
}
public void setColor(int color) {
Paint paint = getPaint();
paint.setColor(color);
}
public void setLineWidth(float lineWidth) {
mLineWidth = lineWidth;
mLinePaint.setStrokeWidth(mLineWidth);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
// bottom black line
////////////////////
mLinePaint.setColor(Color.BLACK);
mLinePaint.setAlpha((int) (255 * 0.9)); // Opacity 90%
canvas.drawLine(
getBounds().left, getBounds().bottom - mLineWidth * 0.5f,
getBounds().right, getBounds().bottom - mLineWidth * 0.5f,
mLinePaint);
// translucent grey rim
///////////////////////
mLinePaint.setColor(Color.parseColor("#888888"));
mLinePaint.setAlpha((int) (255 * 0.7)); // Opacity 70%
// top
canvas.drawLine(
getBounds().left, getBounds().top + mLineWidth * 0.5f,
getBounds().right, getBounds().top + mLineWidth * 0.5f,
mLinePaint);
// left
canvas.drawLine(
getBounds().left + mLineWidth * 0.5f, getBounds().bottom - mLineWidth,
getBounds().left + mLineWidth * 0.5f, getBounds().top + mLineWidth,
mLinePaint);
// right
canvas.drawLine(
getBounds().right - mLineWidth * 0.5f, getBounds().bottom - mLineWidth,
getBounds().right - mLineWidth * 0.5f, getBounds().top + mLineWidth,
mLinePaint);
// top white line
/////////////////
mLinePaint.setColor(Color.WHITE);
mLinePaint.setAlpha((int) (255 * 0.5)); // Opacity 50%
canvas.drawLine(
getBounds().left + mLineWidth, getBounds().top + mLineWidth * 1.5f,
getBounds().right - mLineWidth, getBounds().top + mLineWidth * 1.5f,
mLinePaint);
}
您可以按如下方式使用该类:
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
MyShapeDrawable myShapeDrawable = new MyShapeDrawable(getResources().getColor(R.color.yellow));
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.test);
frameLayout.setBackgroundDrawable(myShapeDrawable);
}
}
下面的类通过自定义ShapeDrawable而不是自定义LayerDrawable实现了您想要的功能。 请注意,
绘制线
调用中的所有坐标偏移都是为了防止重叠,并确保我们可以看到周长周围线条的全宽
public class MyShapeDrawable extends ShapeDrawable {
private int startX, startY, endX, endY;
private float mLineWidth = 1f;
private Paint mLinePaint;
public MyShapeDrawable() {
// No color specified, so call constructor with default color White
this(Color.WHITE);
}
public MyShapeDrawable(int color) {
// use the setter defined below, to set the main color for this drawable
setColor(color);
// setup the Paint for drawing the lines
mLinePaint = new Paint();
mLinePaint.setStyle(Paint.Style.STROKE);
mLinePaint.setStrokeWidth(mLineWidth);
}
public void setColor(int color) {
Paint paint = getPaint();
paint.setColor(color);
}
public void setLineWidth(float lineWidth) {
mLineWidth = lineWidth;
mLinePaint.setStrokeWidth(mLineWidth);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
// bottom black line
////////////////////
mLinePaint.setColor(Color.BLACK);
mLinePaint.setAlpha((int) (255 * 0.9)); // Opacity 90%
canvas.drawLine(
getBounds().left, getBounds().bottom - mLineWidth * 0.5f,
getBounds().right, getBounds().bottom - mLineWidth * 0.5f,
mLinePaint);
// translucent grey rim
///////////////////////
mLinePaint.setColor(Color.parseColor("#888888"));
mLinePaint.setAlpha((int) (255 * 0.7)); // Opacity 70%
// top
canvas.drawLine(
getBounds().left, getBounds().top + mLineWidth * 0.5f,
getBounds().right, getBounds().top + mLineWidth * 0.5f,
mLinePaint);
// left
canvas.drawLine(
getBounds().left + mLineWidth * 0.5f, getBounds().bottom - mLineWidth,
getBounds().left + mLineWidth * 0.5f, getBounds().top + mLineWidth,
mLinePaint);
// right
canvas.drawLine(
getBounds().right - mLineWidth * 0.5f, getBounds().bottom - mLineWidth,
getBounds().right - mLineWidth * 0.5f, getBounds().top + mLineWidth,
mLinePaint);
// top white line
/////////////////
mLinePaint.setColor(Color.WHITE);
mLinePaint.setAlpha((int) (255 * 0.5)); // Opacity 50%
canvas.drawLine(
getBounds().left + mLineWidth, getBounds().top + mLineWidth * 1.5f,
getBounds().right - mLineWidth, getBounds().top + mLineWidth * 1.5f,
mLinePaint);
}
您可以按如下方式使用该类:
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
MyShapeDrawable myShapeDrawable = new MyShapeDrawable(getResources().getColor(R.color.yellow));
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.test);
frameLayout.setBackgroundDrawable(myShapeDrawable);
}
}
下面的类通过自定义ShapeDrawable而不是自定义LayerDrawable实现了您想要的功能。 请注意,
绘制线
调用中的所有坐标偏移都是为了防止重叠,并确保我们可以看到周长周围线条的全宽
public class MyShapeDrawable extends ShapeDrawable {
private int startX, startY, endX, endY;
private float mLineWidth = 1f;
private Paint mLinePaint;
public MyShapeDrawable() {
// No color specified, so call constructor with default color White
this(Color.WHITE);
}
public MyShapeDrawable(int color) {
// use the setter defined below, to set the main color for this drawable
setColor(color);
// setup the Paint for drawing the lines
mLinePaint = new Paint();
mLinePaint.setStyle(Paint.Style.STROKE);
mLinePaint.setStrokeWidth(mLineWidth);
}
public void setColor(int color) {
Paint paint = getPaint();
paint.setColor(color);
}
public void setLineWidth(float lineWidth) {
mLineWidth = lineWidth;
mLinePaint.setStrokeWidth(mLineWidth);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
// bottom black line
////////////////////
mLinePaint.setColor(Color.BLACK);
mLinePaint.setAlpha((int) (255 * 0.9)); // Opacity 90%
canvas.drawLine(
getBounds().left, getBounds().bottom - mLineWidth * 0.5f,
getBounds().right, getBounds().bottom - mLineWidth * 0.5f,
mLinePaint);
// translucent grey rim
///////////////////////
mLinePaint.setColor(Color.parseColor("#888888"));
mLinePaint.setAlpha((int) (255 * 0.7)); // Opacity 70%
// top
canvas.drawLine(
getBounds().left, getBounds().top + mLineWidth * 0.5f,
getBounds().right, getBounds().top + mLineWidth * 0.5f,
mLinePaint);
// left
canvas.drawLine(
getBounds().left + mLineWidth * 0.5f, getBounds().bottom - mLineWidth,
getBounds().left + mLineWidth * 0.5f, getBounds().top + mLineWidth,
mLinePaint);
// right
canvas.drawLine(
getBounds().right - mLineWidth * 0.5f, getBounds().bottom - mLineWidth,
getBounds().right - mLineWidth * 0.5f, getBounds().top + mLineWidth,
mLinePaint);
// top white line
/////////////////
mLinePaint.setColor(Color.WHITE);
mLinePaint.setAlpha((int) (255 * 0.5)); // Opacity 50%
canvas.drawLine(
getBounds().left + mLineWidth, getBounds().top + mLineWidth * 1.5f,
getBounds().right - mLineWidth, getBounds().top + mLineWidth * 1.5f,
mLinePaint);
}
您可以按如下方式使用该类:
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
MyShapeDrawable myShapeDrawable = new MyShapeDrawable(getResources().getColor(R.color.yellow));
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.test);
frameLayout.setBackgroundDrawable(myShapeDrawable);
}
}
这应该不难。创建所需的绘图工具:
这是你需要做的事情的概要。如果需要的话,我可能会发布一些代码进行澄清。这应该不难。创建所需的绘图工具:
这是你需要做的事情的概要。如果需要的话,我可能会发布一些代码进行澄清。这应该不难。创建所需的绘图工具:
这是你需要做的事情的概要。如果需要的话,我可能会发布一些代码进行澄清。这应该不难。创建所需的绘图工具:
这是你需要做的事情的概要。如果需要,我可能会发布一些代码进行澄清。您的具体问题是什么?若要创建一个
分层绘图表
,请使用构造函数。@commonware我的具体问题是如何使用分层绘图表创建它。您的具体问题是什么?若要创建一个分层绘图表
,请使用构造函数。@commonware我的具体问题是如何使用分层绘图表创建它。您的具体问题是什么?若要创建一个分层绘图表
,请使用构造函数。@commonware我的具体问题是如何使用分层绘图表创建它。您的具体问题是什么?要创建一个LayerDrawable
,请使用构造函数。@commonware我的具体问题是如何使用一个分层的drawable创建它。注意:我在这里扩展了ShapeDrawable,因为实际上不需要使用LayerDrawable。如果您特别需要LayerDrawable的原因还有其他原因,那么您仍然可以使用相同的方法。您不仅像上面的@Malcolm那样为我概括了如何实现它,而且还以内联方式向我展示了如何实现它!非常感谢。不过我会注意到,在使用setbackgrounddrawable之前,每个人都应该先看一下,以防其他人觉得这很有帮助。注意:我在这里扩展了ShapeDrawable,因为实际上不需要使用LayerDrawable。如果您特别需要LayerDrawable的原因还有其他原因,那么您仍然可以使用相同的方法。您不仅像上面的@Malcolm那样为我概括了如何实现它,而且还以内联方式向我展示了如何实现它!非常感谢。不过我会注意到,在使用setbackgrounddrawable之前,每个人都应该先看一下,以防其他人觉得这很有帮助。注意:我在这里扩展了ShapeDrawable,因为实际上不需要使用LayerDrawable。如果您特别需要LayerDrawable的原因还有其他原因,那么您仍然可以使用相同的方法。您不仅像上面的@Malcolm那样为我概括了如何实现它,而且还以内联方式向我展示了如何实现它!非常感谢。不过我会注意到,在使用setbackgrounddrawable之前,每个人都应该先看看,以防其他人发现