package com.BV.LinearGradient;

import com.facebook.react.bridge.ReadableArray;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.view.View;

public class LinearGradientView extends View {

    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private LinearGradient mShader;

    private float[] mLocations;
    private float[] mStartPos = {0, 0};
    private float[] mEndPos = {0, 1};
    private int[] mColors;
    private int[] mSize = {0, 0};
    private float mBorderRadius = 0f;


    public LinearGradientView(Context context) {
        super(context);
    }

    public void setStartPosition(ReadableArray startPos) {
        mStartPos = new float[]{(float) startPos.getDouble(0), (float) startPos.getDouble(1)};
        drawGradient();
    }

    public void setEndPosition(ReadableArray endPos) {
        mEndPos = new float[]{(float) endPos.getDouble(0), (float) endPos.getDouble(1)};
        drawGradient();
    }

    public void setColors(ReadableArray colors) {
        int[] _colors = new int[colors.size()];
        for (int i=0; i < _colors.length; i++)
        {
            _colors[i] = colors.getInt(i);
        }
        mColors = _colors;
        drawGradient();
    }

    public void setLocations(ReadableArray locations) {
        float[] _locations = new float[locations.size()];
        for (int i=0; i < _locations.length; i++)
        {
            _locations[i] = (float) locations.getDouble(i);
        }
        mLocations = _locations;
        drawGradient();
    }

    public void setBorderRadius(float borderRadius) {
        mBorderRadius = borderRadius;
        drawGradient();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mSize = new int[]{w, h};
        drawGradient();
    }

    private void drawGradient() {
        mShader = new LinearGradient(
            mStartPos[0] * mSize[0],
            mStartPos[1] * mSize[1],
            mEndPos[0] * mSize[0],
            mEndPos[1] * mSize[1],
            mColors,
            mLocations,
            Shader.TileMode.MIRROR);
        mPaint.setShader(mShader);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mBorderRadius != 0f) {
            RectF clipBounds = new RectF(canvas.getClipBounds());
            canvas.drawRoundRect(
                clipBounds,
                mBorderRadius,
                mBorderRadius,
                mPaint);
        } else {
            canvas.drawPaint(mPaint);
        }
    }
}

@@ -233,20 +233,20 @@
 raphics.
-Rect
+Path
 ;%0Aimport
@@ -444,16 +444,99 @@
 _FLAG);%0A
+    private Path mPathForBorderRadius;%0A    private RectF mTempRectForBorderRadius;%0A
     priv
@@ -1877,24 +1877,46 @@
 rderRadius;%0A
+        updatePath();%0A
         draw
@@ -2050,16 +2050,38 @@
 %7Bw, h%7D;%0A
+        updatePath();%0A
         
@@ -2473,104 +2473,100 @@
     
-@Override%0A    protec
+priva
 te
-d
  void 
-onDraw(Canvas canvas) %7B%0A        super.onDraw(canvas);%0A        if (m
+updatePath() %7B%0A        if (mPathForBorderRadius == null) %7B%0A            mPathFor
 Bord
@@ -2578,16 +2578,21 @@
 ius 
-!
 = 
-0f) %7B
+new Path();
 %0A   
@@ -2604,202 +2604,512 @@
     
-RectF clipBounds = new RectF(canvas.getClipBounds());%0A            canvas.drawRoundRect(%0A                clipBounds,%0A                mBorderRadius,%0A                mBorderRadius,%0A                
+mTempRectForBorderRadius = new RectF();%0A        %7D%0A        mPathForBorderRadius.reset();%0A        mTempRectForBorderRadius.set(0f, 0f, (float) mSize%5B0%5D, (float) mSize%5B1%5D);%0A        mPathForBorderRadius.addRoundRect(%0A            mTempRectForBorderRadius,%0A            mBorderRadius,%0A            mBorderRadius,%0A            Path.Direction.CW);%0A    %7D%0A%0A    @Override%0A    protected void onDraw(Canvas canvas) %7B%0A        super.onDraw(canvas);%0A        if (mPathForBorderRadius == null) %7B%0A            canvas.drawPaint(
 mPai
@@ -3147,36 +3147,57 @@
    canvas.drawPa
-int(
+th(mPathForBorderRadius, 
 mPaint);%0A       

optimize render performance for rounded corners on Android