Android: implements locations in gradient color

This commit is contained in:
王劲鹏 2020-05-09 17:44:50 +08:00 committed by osborn
parent e26a2f3256
commit 506f89930e

View File

@ -22,8 +22,11 @@ import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator; import android.animation.ObjectAnimator;
import android.content.Context; import android.content.Context;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable; import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -234,53 +237,68 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
if (prop.isNumber()) { if (prop.isNumber()) {
setBackgroundColor(prop.asNumber().toInt()); setBackgroundColor(prop.asNumber().toInt());
} else if (prop.isObject()) { } else if (prop.isObject()) {
GradientDrawable gradientDrawable = new GradientDrawable(); final JSValue dict = prop;
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
if (prop.asObject().propertySet().contains("colors")) { ShapeDrawable shapeDrawable = new ShapeDrawable();
JSValue colors = prop.asObject().getProperty("colors"); shapeDrawable.setShape(new RectShape());
gradientDrawable.setColors(colors.asArray().toIntArray()); shapeDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
LinearGradient linearGradient = null;
int[] colors = null;
float[] locations = null;
if (dict.asObject().propertySet().contains("colors")) {
colors = dict.asObject().getProperty("colors").asArray().toIntArray();
if (dict.asObject().propertySet().contains("locations")) {
locations = dict.asObject().getProperty("locations").asArray().toFloatArray();
}
} else { } else {
if (prop.asObject().propertySet().contains("start") && prop.asObject().propertySet().contains("end")) { if (dict.asObject().propertySet().contains("start") && dict.asObject().propertySet().contains("end")) {
JSValue start = prop.asObject().getProperty("start"); JSValue start = dict.asObject().getProperty("start");
JSValue end = prop.asObject().getProperty("end"); JSValue end = dict.asObject().getProperty("end");
gradientDrawable.setColors(new int[]{start.asNumber().toInt(), end.asNumber().toInt()}); colors = new int[]{start.asNumber().toInt(), end.asNumber().toInt()};
} }
} }
JSValue orientation = prop.asObject().getProperty("orientation");
JSValue orientation = dict.asObject().getProperty("orientation");
switch (orientation.asNumber().toInt()) { switch (orientation.asNumber().toInt()) {
case 0: case 0:
gradientDrawable.setOrientation(GradientDrawable.Orientation.TOP_BOTTOM); linearGradient = new LinearGradient(0.f, 0.f, 0.f, height, colors, locations, Shader.TileMode.CLAMP);
break; break;
case 1: case 1:
gradientDrawable.setOrientation(GradientDrawable.Orientation.TR_BL); linearGradient = new LinearGradient(width, 0.f, 0.f, height, colors, locations, Shader.TileMode.CLAMP);
break; break;
case 2: case 2:
gradientDrawable.setOrientation(GradientDrawable.Orientation.RIGHT_LEFT); linearGradient = new LinearGradient(width, 0.f, 0.f, 0.f, colors, locations, Shader.TileMode.CLAMP);
break; break;
case 3: case 3:
gradientDrawable.setOrientation(GradientDrawable.Orientation.BR_TL); linearGradient = new LinearGradient(width, height, 0.f, 0.f, colors, locations, Shader.TileMode.CLAMP);
break; break;
case 4: case 4:
gradientDrawable.setOrientation(GradientDrawable.Orientation.BOTTOM_TOP); linearGradient = new LinearGradient(0.f, height, 0.f, 0.f, colors, locations, Shader.TileMode.CLAMP);
break; break;
case 5: case 5:
gradientDrawable.setOrientation(GradientDrawable.Orientation.BL_TR); linearGradient = new LinearGradient(0.f, height, width, 0.f, colors, locations, Shader.TileMode.CLAMP);
break; break;
case 6: case 6:
gradientDrawable.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT); linearGradient = new LinearGradient(0.f, 0.f, width, 0.f, colors, locations, Shader.TileMode.CLAMP);
break; break;
case 7: case 7:
gradientDrawable.setOrientation(GradientDrawable.Orientation.TL_BR); linearGradient = new LinearGradient(0.f, 0.f, width, height, colors, locations, Shader.TileMode.CLAMP);
break; break;
} }
gradientDrawable.setSize(view.getMeasuredWidth(), view.getMeasuredHeight()); return linearGradient;
view.setBackground(gradientDrawable); }
});
view.setBackground(shapeDrawable);
} }
} }
break; break;