0

I wanna create a view with Arc Shape background.. my view has weight of .3.. which makes it fill one third of my layout..

I try to set its background to an arc shape (I want it to be half oval) like this:

    ArcShape shape = new ArcShape(270, 180);
    ShapeDrawable shapeDrawable = new ShapeDrawable(shape);
    shapeDrawable.getPaint().setColor(getResources().getColor(R.color.primary_color));
    leftPathView.setBackgroundDrawable(shapeDrawable);

this generates the shape I need but it doesn't fill the space of my view.. It only takes half of it.. But when I create a completed circle it takes all the space..

Any Idea ?

Muhammad Ashraf
  • 1,252
  • 1
  • 14
  • 32
  • Avoid using constant pixel values (`270, 180`). Compute the values at runtime using View.getWith() and View.getHeight() – Simon Marquis Aug 09 '15 at 10:38
  • These are the angles of the arc.. take a look at the constructor: http://developer.android.com/reference/android/graphics/drawable/shapes/ArcShape.html – Muhammad Ashraf Aug 09 '15 at 11:43
  • My bad, so maybe you should use the resize method: https://developer.android.com/reference/android/graphics/drawable/shapes/Shape.html#resize(float, float) – Simon Marquis Aug 09 '15 at 11:45

1 Answers1

0

After a bit of research I found this to be the best answer. Especially if you want to do this in your onCreate function, bacause when the onCreate function hasn't ended the layout does not yet have a width and height (more info here).

final LinearLayout leftPathView= (LinearLayout) findViewById(R.id.left_path_view);

layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        leftPathView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        leftPathView.setX(-layout.getWidth());
        leftPathView.getLayoutParams().width = layout.getWidth() * 2;
        leftPathView.requestLayout();
    }
});

ArcShape shape = new ArcShape(270, 180);
ShapeDrawable shapeDrawable = new ShapeDrawable(shape);
shapeDrawable.getPaint().setColor(getResources().getColor(R.color.primary_color));
layout.setBackground(shapeDrawable);

This code does make your View go out of the bounds of your screen, so when adding other items to this View you need to take that into account. A solution for that could be to put the View in a RelativeLayout and put another view on top of the leftPathView.

M0CH1R0N
  • 756
  • 9
  • 19