0

I want to achieve below meter by use of canvas .

enter image description here

As you can see the black portion has 2 colors in it . I am able to set color of the black moving image using shape drawable. Code and image that I have achieved so far is given below. How can I have desired result. I am novice in canvas .

enter image description here

    float[] outerR = new float[] { 7, 7,curve , curve, curve, curve, 7, 7};
    ShapeDrawable mMovingRectangle = new ShapeDrawable();
    mMovingRectangle.setShape(new RoundRectShape(outerR, null, null));
    mMovingRectangle.getPaint().setColor(getResources().getColor(R.color.black_alfa_60));
Payal
  • 903
  • 1
  • 9
  • 28

1 Answers1

0

Look at this ShapeDrawable with shaderfactory

private void FillCustomGradient(View v) {
    final View view = v;
    Drawable[] layers = new Drawable[1];

    ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            LinearGradient lg = new LinearGradient(
                    0,
                    0,
                    0,
                    view.getHeight(),
                    new int[] {
                             getResources().getColor(R.color.color1), // please input your color from resource for color-1,2,3,4
                             getResources().getColor(R.color.color2),
                             getResources().getColor(R.color.color3),
                             getResources().getColor(R.color.color4)},
                    new float[] { 0, 0.49f, 0.50f, 1 },
                    Shader.TileMode.CLAMP);
            return lg;
        }
    };
    PaintDrawable p = new PaintDrawable();
    p.setShape(new RectShape());
    p.setShaderFactory(sf);
    p.setCornerRadii(new float[] { 5, 5, 5, 5, 0, 0, 0, 0 });
    layers[0] = (Drawable) p;

    LayerDrawable composite = new LayerDrawable(layers);
    view.setBackgroundDrawable(composite);
}

may be this is helpful to you

Qadir Hussain
  • 8,721
  • 13
  • 89
  • 124