create LinearGradientView directly instead of wrapping in FrameLayout

This commit is contained in:
Dmitry Gladkov 2016-01-19 10:58:59 +02:00
parent a4701d266b
commit c41e14aa47
2 changed files with 23 additions and 93 deletions

View File

@ -14,7 +14,7 @@ import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
public class LinearGradientManager extends SimpleViewManager<FrameLayout> {
public class LinearGradientManager extends SimpleViewManager<LinearGradientView> {
public static final String REACT_CLASS = "BVLinearGradient";
public static final String PROP_COLORS = "colors";
@ -22,58 +22,33 @@ public class LinearGradientManager extends SimpleViewManager<FrameLayout> {
public static final String PROP_START_POS = "start";
public static final String PROP_END_POS = "end";
public LinearGradientView mGradientView;
@Override
public String getName() {
return REACT_CLASS;
}
@Override
protected FrameLayout createViewInstance(ThemedReactContext context) {
WritableMap defaults = new WritableNativeMap();
WritableArray array = new WritableNativeArray();
array.pushInt(0);
array.pushInt(0);
defaults.putArray("colors", array);
mGradientView = new LinearGradientView(context, new CatalystStylesDiffMap(defaults));
FrameLayout frame = new FrameLayout(context);
frame.removeAllViews();
frame.addView(mGradientView);
return frame;
protected LinearGradientView createViewInstance(ThemedReactContext context) {
return new LinearGradientView(context);
}
@ReactProp(name=PROP_COLORS)
public void updateColors(FrameLayout frame, ReadableArray colors){
if(mGradientView != null) {
mGradientView.updateColors(colors);
}
public void setColors(LinearGradientView gradientView, ReadableArray colors){
gradientView.setColors(colors);
}
@ReactProp(name=PROP_LOCATIONS)
public void updatePositions(FrameLayout frame, ReadableArray locations){
if(mGradientView != null) {
mGradientView.updateLocations(locations);
}
public void setLocations(LinearGradientView gradientView, ReadableArray locations){
gradientView.setLocations(locations);
}
@ReactProp(name=PROP_START_POS)
public void updateStartPosition(FrameLayout frame, ReadableArray startPos){
if(mGradientView != null) {
mGradientView.updateStartPosition(startPos);
}
public void setStartPosition(LinearGradientView gradientView, ReadableArray startPos){
gradientView.setStartPosition(startPos);
}
@ReactProp(name=PROP_END_POS)
public void updateEndPosition(FrameLayout frame, ReadableArray endPos){
if(mGradientView != null) {
mGradientView.updateEndPosition(endPos);
}
public void setEndPosition(LinearGradientView gradientView, ReadableArray endPos){
gradientView.setEndPosition(endPos);
}
}

View File

@ -14,89 +14,44 @@ import com.facebook.react.uimanager.CatalystStylesDiffMap;
public class LinearGradientView extends View {
private Paint mPaint;
private Paint mPaint = new Paint();
private LinearGradient mShader;
private float[] mLocations;
private float[] mStartPos;
private float[] mEndPos;
private float[] mStartPos = {0, 0};
private float[] mEndPos = {0, 1};
private int[] mColors;
private int[] mSize;
private int[] mSize = {0, 0};
public LinearGradientView(Context context, CatalystStylesDiffMap props) {
super(context, null);
mPaint = new Paint();
ReadableArray colors = props.getArray("colors");
// if we managed to get here and not get colors, give up.
assert colors != null;
mColors = new int[colors.size()];
for (int i=0; i < mColors.length; i++)
{
mColors[i] = colors.getInt(i);
}
try {
ReadableArray locations = props.getArray("locations");
assert locations != null;
mLocations= new float[locations.size()];
for (int i=0; i < mLocations.length; i++)
{
mLocations[i] = (float) locations.getDouble(i);
}
} catch (Exception e) {
mLocations = null;
}
try {
ReadableArray startPos = props.getArray("start");
assert startPos != null;
mStartPos = new float[]{(float) startPos.getDouble(0), (float) startPos.getDouble(1)};
} catch (Exception e) {
mStartPos = new float[]{0,0};
}
try {
ReadableArray endPos = props.getArray("end");
assert endPos != null;
mEndPos= new float[]{(float) endPos.getDouble(0), (float) endPos.getDouble(1)};
} catch (Exception e) {
// default to full height.
mEndPos = new float[]{0, 1};
}
mSize = new int[]{0, 0};
drawGradient();
public LinearGradientView(Context context) {
super(context);
}
public void updateStartPosition(ReadableArray startPos) {
public void setStartPosition(ReadableArray startPos) {
float[] _startPos;
try {
assert startPos != null;
_startPos= new float[]{(float) startPos.getDouble(0), (float) startPos.getDouble(1)};
} catch (Exception e) {
_startPos = new float[]{0,0};
_startPos = new float[]{0, 0};
}
mStartPos = _startPos;
drawGradient();
}
public void updateEndPosition(ReadableArray endPos) {
public void setEndPosition(ReadableArray endPos) {
float[] _endPos;
try {
assert endPos != null;
_endPos= new float[]{(float) endPos.getDouble(0), (float) endPos.getDouble(1)};
} catch (Exception e) {
_endPos = new float[]{0,0};
_endPos = new float[]{0, 0};
}
mEndPos = _endPos;
drawGradient();
}
public void updateColors(ReadableArray colors){
public void setColors(ReadableArray colors){
int[] _colors = new int[colors.size()];
for (int i=0; i < _colors.length; i++)
{
@ -106,7 +61,7 @@ public class LinearGradientView extends View {
drawGradient();
}
public void updateLocations(ReadableArray locations){
public void setLocations(ReadableArray locations){
float[] _locations;
try {
assert locations != null;