2014-10-02 68 views
0

这个答案LayerDrawable programmatically只帮助提问的人,没有真正教任何东西。所以我在这里试图做一些类似的事情需要一点帮助。以编程方式创建一个LayeredDrawable

我想要的东西非常接近这个:

enter image description here

  1. 他们如果不指定颜色背景颜色动态或#eeeeee规定。
  2. 他们有1px的边框颜色#888888有70%的不透明度(所以它需要从背景的信封的颜色)
  3. 在该边框内有一个1px的白线在顶部,50%的不透明度。
  4. 下边框应该用1px黑线代替,90%不透明度。

之后没有背景颜色上,我发现我不能使用XML绘制创建这个,但是我认为它可以通过编程来完成,但真的没有任何很好的例子在那里,是很好的苍蝇确定解释。

提前致谢!

我的具体问题是如何使用分层绘制来创建它。

+1

你的具体问题是什么?要创建'LayerDrawable',请使用构造函数。 – CommonsWare 2014-10-02 21:25:15

+0

@CommonsWare我的具体问题是如何使用分层绘制来创建这个。 – jcaruso 2014-10-02 21:26:06

回答

2

下面的类使用自定义ShapeDrawable而不是自定义的LayerDrawable实现您想要的功能。 请注意,drawLine调用中的所有坐标偏移都是为了防止重叠,并确保我们可以看到周边线条的整个宽度。

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); 
    } 
} 
+0

注意:我在这里扩展了ShapeDrawable,因为没有真正需要使用LayerDrawable。如果您为什么特别需要LayerDrawable还有其他一些原因,您仍然可以使用相同的方法。 – 2014-10-03 04:44:11

+0

你不仅给我一个关于如何做到这一点的广泛轮廓,就像@Malcolm在上面做的那样,但是你在内联中向我展示了如何做!谢谢! – jcaruso 2014-10-03 13:18:52

+0

我会注意到,尽管每个人都应该看看http:// stackoverflow。com/questions/11947603/setbackground-vs-setbackgrounddrawable-android在使用setbackgrounddrawable之前,以防别人认为这有帮助。 – jcaruso 2014-10-03 13:19:36

1

这应该不是难事。创建绘图资源,你需要:

  1. 背景最好与ColorDrawable,以便您可以拨打setColor()绘制。
  2. 可以使用ShapeDrawable创建线条。
  3. 最后,只需创建一个LayerDrawable使用您的可绘制堆栈。使用setLayerInset()来调整形状,使它们显示为边框。

这是您需要做的一般概述。如有需要,我可能会发布一些代码进行澄清。

相关问题