com.taobao.weex.ui.view.border.BorderDrawable Java Examples

The following examples show how to use com.taobao.weex.ui.view.border.BorderDrawable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WXViewUtils.java    From ucar-weex-core with Apache License 2.0 6 votes vote down vote up
public static @Nullable
BorderDrawable getBorderDrawable(@NonNull View view){
  Drawable drawable=view.getBackground();
  if(drawable instanceof BorderDrawable){
    return (BorderDrawable) drawable;
  }
  else if(drawable instanceof LayerDrawable){
    if(((LayerDrawable) drawable).getNumberOfLayers()>1) {
      Drawable innerDrawable=((LayerDrawable) drawable).getDrawable(0);
      if(innerDrawable instanceof BorderDrawable){
        return (BorderDrawable) innerDrawable;
      }
    }
  }
  return null;
}
 
Example #2
Source File: WXViewUtils.java    From ucar-weex-core with Apache License 2.0 6 votes vote down vote up
/**
 * Due limitation in Android platform, the linear gradient in the following page will not be
 * rounded if {@link Canvas#clipPath(Path)} of the parent view invoked when API level is lower
 * than 21.
 * http://dotwe.org/weex/963c9ade129f86757cecdd85651cd30e
 * @param targetView
 * @param borderDrawable
 * @return
 */
private static boolean clipCanvasIfBackgroundImageExist(@NonNull View targetView,
                                                        @NonNull BorderDrawable borderDrawable) {
  if (targetView instanceof ViewGroup) {
    View child;
    ViewGroup parent = ((ViewGroup) targetView);
    int count = parent.getChildCount();
    for (int i = 0; i < count; i++) {
      child = parent.getChildAt(i);
      if (child.getBackground() instanceof BorderDrawable &&
          ((BorderDrawable) child.getBackground()).hasImage() &&
          Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return false;
      }
    }
  }
  return true;
}
 
Example #3
Source File: WXComponent.java    From ucar-weex-core with Apache License 2.0 6 votes vote down vote up
public void setBorderRadius(String key, float borderRadius) {
  if (borderRadius >= 0) {
    switch (key) {
      case Constants.Name.BORDER_RADIUS:
        getOrCreateBorder().setBorderRadius(BorderDrawable.BORDER_RADIUS_ALL, WXViewUtils.getRealSubPxByWidth(borderRadius,mInstance.getInstanceViewPortWidth()));
        break;
      case Constants.Name.BORDER_TOP_LEFT_RADIUS:
        getOrCreateBorder().setBorderRadius(BorderDrawable.BORDER_TOP_LEFT_RADIUS, WXViewUtils.getRealSubPxByWidth(borderRadius,mInstance.getInstanceViewPortWidth()));
        break;
      case Constants.Name.BORDER_TOP_RIGHT_RADIUS:
        getOrCreateBorder().setBorderRadius(BorderDrawable.BORDER_TOP_RIGHT_RADIUS, WXViewUtils.getRealSubPxByWidth(borderRadius,mInstance.getInstanceViewPortWidth()));
        break;
      case Constants.Name.BORDER_BOTTOM_RIGHT_RADIUS:
        getOrCreateBorder().setBorderRadius(BorderDrawable.BORDER_BOTTOM_RIGHT_RADIUS, WXViewUtils.getRealSubPxByWidth(borderRadius,mInstance.getInstanceViewPortWidth()));
        break;
      case Constants.Name.BORDER_BOTTOM_LEFT_RADIUS:
        getOrCreateBorder().setBorderRadius(BorderDrawable.BORDER_BOTTOM_LEFT_RADIUS, WXViewUtils.getRealSubPxByWidth(borderRadius,mInstance.getInstanceViewPortWidth()));
        break;
    }
  }
}
 
Example #4
Source File: WXViewUtils.java    From weex-uikit with MIT License 6 votes vote down vote up
public static @Nullable
BorderDrawable getBorderDrawable(@NonNull View view){
  Drawable drawable=view.getBackground();
  if(drawable instanceof BorderDrawable){
    return (BorderDrawable) drawable;
  }
  else if(drawable instanceof LayerDrawable){
    if(((LayerDrawable) drawable).getNumberOfLayers()>1) {
      Drawable innerDrawable=((LayerDrawable) drawable).getDrawable(0);
      if(innerDrawable instanceof BorderDrawable){
        return (BorderDrawable) innerDrawable;
      }
    }
  }
  return null;
}
 
Example #5
Source File: WXViewUtils.java    From weex-uikit with MIT License 6 votes vote down vote up
public static void clipCanvasWithinBorderBox(View targetView, Canvas canvas) {
  Drawable drawable;
  /* According to https://developer.android.com/guide/topics/graphics/hardware-accel.html#unsupported
    API 18 or higher supports clipPath to canvas based on hardware acceleration.
   */
  if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 ||
       !canvas.isHardwareAccelerated()) &&
      ((drawable = targetView.getBackground()) instanceof BorderDrawable)) {
    BorderDrawable borderDrawable = (BorderDrawable) drawable;
    if(borderDrawable.isRounded()) {
      Path path = borderDrawable.getContentPath(
          new RectF(0, 0, targetView.getWidth(), targetView.getHeight()));
      canvas.clipPath(path);
    }
  }
}
 
Example #6
Source File: WXComponent.java    From weex-uikit with MIT License 6 votes vote down vote up
public void setBorderRadius(String key, float borderRadius) {
  if (borderRadius >= 0) {
    switch (key) {
      case Constants.Name.BORDER_RADIUS:
        getOrCreateBorder().setBorderRadius(BorderDrawable.BORDER_RADIUS_ALL, WXViewUtils.getRealSubPxByWidth(borderRadius,mInstance.getViewPortWidth()));
        break;
      case Constants.Name.BORDER_TOP_LEFT_RADIUS:
        getOrCreateBorder().setBorderRadius(BorderDrawable.BORDER_TOP_LEFT_RADIUS, WXViewUtils.getRealSubPxByWidth(borderRadius,mInstance.getViewPortWidth()));
        break;
      case Constants.Name.BORDER_TOP_RIGHT_RADIUS:
        getOrCreateBorder().setBorderRadius(BorderDrawable.BORDER_TOP_RIGHT_RADIUS, WXViewUtils.getRealSubPxByWidth(borderRadius,mInstance.getViewPortWidth()));
        break;
      case Constants.Name.BORDER_BOTTOM_RIGHT_RADIUS:
        getOrCreateBorder().setBorderRadius(BorderDrawable.BORDER_BOTTOM_RIGHT_RADIUS, WXViewUtils.getRealSubPxByWidth(borderRadius,mInstance.getViewPortWidth()));
        break;
      case Constants.Name.BORDER_BOTTOM_LEFT_RADIUS:
        getOrCreateBorder().setBorderRadius(BorderDrawable.BORDER_BOTTOM_LEFT_RADIUS, WXViewUtils.getRealSubPxByWidth(borderRadius,mInstance.getViewPortWidth()));
        break;
    }
  }
}
 
Example #7
Source File: WXViewUtils.java    From ucar-weex-core with Apache License 2.0 5 votes vote down vote up
public static void clipCanvasWithinBorderBox(View targetView, Canvas canvas) {
  Drawable drawable;
  if (clipCanvasDueToAndroidVersion(canvas) &&
      clipCanvasIfAnimationExist() &&
      ((drawable = targetView.getBackground()) instanceof BorderDrawable)) {
    BorderDrawable borderDrawable = (BorderDrawable) drawable;
    if (borderDrawable.isRounded()) {
      if (clipCanvasIfBackgroundImageExist(targetView, borderDrawable)) {
        Path path = borderDrawable.getContentPath(
            new RectF(0, 0, targetView.getWidth(), targetView.getHeight()));
        canvas.clipPath(path);
      }
    }
  }
}
 
Example #8
Source File: BackgroundColorProperty.java    From ucar-weex-core with Apache License 2.0 5 votes vote down vote up
@Override
public Integer get(View object) {
  int color;
  BorderDrawable borderDrawable;
  if ((borderDrawable = WXViewUtils.getBorderDrawable(object)) != null) {
    color = borderDrawable.getColor();
  } else if (object.getBackground() instanceof ColorDrawable) {
    color = ((ColorDrawable) object.getBackground()).getColor();
  } else {
    color = Color.TRANSPARENT;
    WXLogUtils.e(TAG, "Unsupported background type");
  }
  return color;
}
 
Example #9
Source File: BackgroundColorProperty.java    From ucar-weex-core with Apache License 2.0 5 votes vote down vote up
@Override
public void set(View object, Integer value) {
  BorderDrawable borderDrawable;
  if ((borderDrawable = WXViewUtils.getBorderDrawable(object)) != null) {
    borderDrawable.setColor(value);
  } else if (object.getBackground() instanceof ColorDrawable) {
    ((ColorDrawable) object.getBackground()).setColor(value);
  } else {
    WXLogUtils.e(TAG, "Unsupported background type");
  }
}
 
Example #10
Source File: WXImageTest.java    From ucar-weex-core with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest(WXImageView.class)
public void testSetBackgroundColor() throws Exception {

  ImageView imageView = mWXImage.initComponentHostView(Robolectric.setupActivity(TestActivity.class));
  mWXImage.mHost = imageView;

  mWXImage.setBackgroundColor("#FFFFFF");

  Drawable drawable = mWXImage.getHostView().getBackground();
  assertEquals(drawable instanceof BorderDrawable, true);
}
 
Example #11
Source File: WXImageTest.java    From weex-uikit with MIT License 5 votes vote down vote up
@Test
@PrepareForTest(WXImageView.class)
public void testSetBackgroundColor() throws Exception {

  ImageView imageView = mWXImage.initComponentHostView(Robolectric.setupActivity(TestActivity.class));
  mWXImage.mHost = imageView;

  mWXImage.setBackgroundColor("#FFFFFF");

  Drawable drawable = mWXImage.getHostView().getBackground();
  assertEquals(drawable instanceof BorderDrawable, true);
}
 
Example #12
Source File: ViewPropertiesSupplier.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 5 votes vote down vote up
private static float getBorderWidthNative(@NonNull BorderDrawable drawable, int position) {
    try {
        Method method = drawable.getClass().getDeclaredMethod("getBorderWidth",int.class);
        method.setAccessible(true);
        return (float) method.invoke(drawable,position);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0f;
}
 
Example #13
Source File: ViewPropertiesSupplier.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 5 votes vote down vote up
private static int getBorderColorNative(@NonNull BorderDrawable drawable, int position) {
    try {
        Method method = drawable.getClass().getDeclaredMethod("getBorderColor",int.class);
        method.setAccessible(true);
        return (int) method.invoke(drawable,position);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}
 
Example #14
Source File: AnimationAction.java    From ucar-weex-core with Apache License 2.0 4 votes vote down vote up
private
@Nullable
ObjectAnimator createAnimator(final View target, final int viewPortWidth) {
  if (target == null) {
    return null;
  }
  WXAnimationBean.Style style = mAnimationBean.styles;
  if (style != null) {
    ObjectAnimator animator;
    List<PropertyValuesHolder> holders = style.getHolders();
    if (!TextUtils.isEmpty(style.backgroundColor)) {
      BorderDrawable borderDrawable;
      if ((borderDrawable = WXViewUtils.getBorderDrawable(target)) != null) {
        holders.add(PropertyValuesHolder.ofObject(
            new BackgroundColorProperty(), new ArgbEvaluator(),
            borderDrawable.getColor(),
            WXResourceUtils.getColor(style.backgroundColor)));
      } else if (target.getBackground() instanceof ColorDrawable) {
        holders.add(PropertyValuesHolder.ofObject(
            new BackgroundColorProperty(), new ArgbEvaluator(),
            ((ColorDrawable) target.getBackground()).getColor(),
            WXResourceUtils.getColor(style.backgroundColor)));
      }
    }

    if (target.getLayoutParams() != null &&
        (!TextUtils.isEmpty(style.width) || !TextUtils.isEmpty(style.height))) {
      ViewGroup.LayoutParams layoutParams = target.getLayoutParams();
      if (!TextUtils.isEmpty(style.width)) {
        holders.add(PropertyValuesHolder.ofInt(new WidthProperty(), layoutParams.width,
            (int) WXViewUtils.getRealPxByWidth(WXUtils.getFloat(style.width), viewPortWidth)));
      }
      if (!TextUtils.isEmpty(style.height)) {
        holders.add(PropertyValuesHolder.ofInt(new HeightProperty(), layoutParams.height,
            (int) WXViewUtils.getRealPxByWidth(WXUtils.getFloat(style.height), viewPortWidth)));
      }
    }

    if (style.getPivot() != null) {
      Pair<Float, Float> pair = style.getPivot();
      target.setPivotX(pair.first);
      target.setPivotY(pair.second);
    }
    animator = ObjectAnimator.ofPropertyValuesHolder(
        target, holders.toArray(new PropertyValuesHolder[holders.size()]));
    animator.setStartDelay(mAnimationBean.delay);
    return animator;
  } else {
    return null;
  }
}
 
Example #15
Source File: WXAnimationModule.java    From weex-uikit with MIT License 4 votes vote down vote up
private static @Nullable
ObjectAnimator createAnimator(@NonNull WXAnimationBean animation, final View target,final int viewPortWidth) {
  if(target == null){
    return null;
  }
  WXAnimationBean.Style style = animation.styles;
  if (style != null) {
    ObjectAnimator animator;
    List<PropertyValuesHolder> holders =style.getHolders();
    if (!TextUtils.isEmpty(style.backgroundColor)) {
      BorderDrawable borderDrawable;
      if ((borderDrawable=WXViewUtils.getBorderDrawable(target))!=null) {
        holders.add(PropertyValuesHolder.ofObject(
            WXAnimationBean.Style.BACKGROUND_COLOR, new ArgbEvaluator(),
            borderDrawable.getColor(),
            WXResourceUtils.getColor(style.backgroundColor)));
      } else if (target.getBackground() instanceof ColorDrawable) {
        holders.add(PropertyValuesHolder.ofObject(
            WXAnimationBean.Style.BACKGROUND_COLOR, new ArgbEvaluator(),
            ((ColorDrawable) target.getBackground()).getColor(),
            WXResourceUtils.getColor(style.backgroundColor)));
      }
    }
    if (style.getPivot() != null) {
      Pair<Float, Float> pair = style.getPivot();
      target.setPivotX(pair.first);
      target.setPivotY(pair.second);
    }
    animator = ObjectAnimator.ofPropertyValuesHolder(
        target, holders.toArray(new PropertyValuesHolder[holders.size()]));
    animator.setStartDelay(animation.delay);
    if (target.getLayoutParams() != null &&
        (!TextUtils.isEmpty(style.width) || !TextUtils.isEmpty(style.height))) {
      DimensionUpdateListener listener = new DimensionUpdateListener(target);
      ViewGroup.LayoutParams layoutParams = target.getLayoutParams();
      if (!TextUtils.isEmpty(style.width)) {
        listener.setWidth(layoutParams.width,
                          (int) WXViewUtils.getRealPxByWidth(WXUtils.getFloat(style.width),viewPortWidth));
      }
      if (!TextUtils.isEmpty(style.height)) {
        listener.setHeight(layoutParams.height,
                           (int) WXViewUtils.getRealPxByWidth(WXUtils.getFloat(style.height),viewPortWidth));
      }
      animator.addUpdateListener(listener);
    }
    return animator;
  } else {
    return null;
  }
}
 
Example #16
Source File: ViewPropertiesSupplier.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 4 votes vote down vote up
@NonNull
     Map<String,String> supplyPropertiesFromNativeView(@NonNull View view) {
        Map<String,String> result = new LinkedHashMap<>();
        result.put(BoxModelConstants.LEFT, String.valueOf(view.getLeft()));
        result.put(BoxModelConstants.TOP, String.valueOf(view.getTop()));
        result.put(BoxModelConstants.RIGHT, String.valueOf(view.getRight()));
        result.put(BoxModelConstants.BOTTOM, String.valueOf(view.getBottom()));

        result.put(BoxModelConstants.WIDTH, String.valueOf(view.getWidth()));
        result.put(BoxModelConstants.HEIGHT, String.valueOf(view.getHeight()));

        result.put(BoxModelConstants.PADDING_LEFT, String.valueOf(view.getPaddingLeft()));
        result.put(BoxModelConstants.PADDING_TOP, String.valueOf(view.getPaddingTop()));
        result.put(BoxModelConstants.PADDING_RIGHT, String.valueOf(view.getPaddingRight()));
        result.put(BoxModelConstants.PADDING_BOTTOM, String.valueOf(view.getPaddingBottom()));

        result.put(BoxModelConstants.VISIBILITY, (view.getVisibility() == View.VISIBLE ? "visible":"invisible"));

        ViewGroup.LayoutParams params = view.getLayoutParams();
        if(params != null && params instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) params;
            result.put(BoxModelConstants.MARGIN_LEFT, String.valueOf(marginLayoutParams.leftMargin));
            result.put(BoxModelConstants.MARGIN_TOP, String.valueOf(marginLayoutParams.topMargin));
            result.put(BoxModelConstants.MARGIN_RIGHT, String.valueOf(marginLayoutParams.rightMargin));
            result.put(BoxModelConstants.MARGIN_BOTTOM, String.valueOf(marginLayoutParams.bottomMargin));
        }

        result.put(BoxModelConstants.BORDER_LEFT_WIDTH, null);
        result.put(BoxModelConstants.BORDER_RIGHT_WIDTH, null);
        result.put(BoxModelConstants.BORDER_TOP_WIDTH, null);
        result.put(BoxModelConstants.BORDER_BOTTOM_WIDTH, null);

        if(view instanceof WXTextView) {
            WXTextView textView = (WXTextView) view;
            if(textView.getText() != null) {
                result.put("value",textView.getText().toString());
            }
        }

        if(view.getBackground() != null) {
            Drawable drawable = view.getBackground();
            if(drawable instanceof BorderDrawable) {
                BorderDrawable borderDrawable = (BorderDrawable) drawable;
                result.put(BoxModelConstants.BACKGROUND_COLOR,String.format(Locale.CHINA,"#%06X",0xFFFFFF & borderDrawable.getColor()));
                if(borderDrawable.getOpacity() != -1) {
                    result.put(BoxModelConstants.OPACITY,borderDrawable.getOpacity()+"");
                }

//                float borderWidth = getBorderWidthNative(borderDrawable, Spacing.ALL);
//                if(borderWidth != 0f) {
//                    result.put(BoxModelConstants.BORDER_LEFT_WIDTH,String.valueOf(borderWidth));
//                    result.put(BoxModelConstants.BORDER_TOP_WIDTH,String.valueOf(borderWidth));
//                    result.put(BoxModelConstants.BORDER_RIGHT_WIDTH,String.valueOf(borderWidth));
//                    result.put(BoxModelConstants.BORDER_BOTTOM_WIDTH,String.valueOf(borderWidth));
//                }else {
//                    float borderLeftWidth = getBorderWidthNative(borderDrawable, Spacing.LEFT);
//                    if(borderLeftWidth != 0f) {
//                        result.put(BoxModelConstants.BORDER_LEFT_WIDTH,String.valueOf(borderLeftWidth));
//                    }
//                    float borderRightWidth = getBorderWidthNative(borderDrawable, Spacing.RIGHT);
//                    if(borderRightWidth != 0f) {
//                        result.put(BoxModelConstants.BORDER_RIGHT_WIDTH,String.valueOf(borderRightWidth));
//                    }
//                    float borderTopWidth = getBorderWidthNative(borderDrawable, Spacing.TOP);
//                    if(borderTopWidth != 0f) {
//                        result.put(BoxModelConstants.BORDER_TOP_WIDTH,String.valueOf(borderTopWidth));
//                    }
//                    float borderBottomWidth = getBorderWidthNative(borderDrawable, Spacing.BOTTOM);
//                    if(borderBottomWidth != 0f) {
//                        result.put(BoxModelConstants.BORDER_BOTTOM_WIDTH,String.valueOf(borderBottomWidth));
//                    }
//                }

                int borderColor = getBorderColorNative(borderDrawable,Spacing.ALL);
                if(borderColor != 0) {
                    result.put(BoxModelConstants.BORDER_LEFT_COLOR,String.format(Locale.CHINA,"#%06X",0xFFFFFF & borderColor));
                    result.put(BoxModelConstants.BORDER_TOP_COLOR,String.format(Locale.CHINA,"#%06X",0xFFFFFF & borderColor));
                    result.put(BoxModelConstants.BORDER_RIGHT_COLOR,String.format(Locale.CHINA,"#%06X",0xFFFFFF & borderColor));
                    result.put(BoxModelConstants.BORDER_BOTTOM_COLOR,String.format(Locale.CHINA,"#%06X",0xFFFFFF & borderColor));
                }else {
                    int borderLeftColor = getBorderColorNative(borderDrawable,Spacing.LEFT);
                    result.put(BoxModelConstants.BORDER_LEFT_COLOR,String.format(Locale.CHINA,"#%06X",0xFFFFFF & borderLeftColor));

                    int borderTopColor = getBorderColorNative(borderDrawable,Spacing.TOP);
                    result.put(BoxModelConstants.BORDER_TOP_COLOR,String.format(Locale.CHINA,"#%06X",0xFFFFFF & borderTopColor));

                    int borderRightColor = getBorderColorNative(borderDrawable,Spacing.RIGHT);
                    result.put(BoxModelConstants.BORDER_RIGHT_COLOR,String.format(Locale.CHINA,"#%06X",0xFFFFFF & borderRightColor));

                    int borderBottomColor = getBorderColorNative(borderDrawable,Spacing.BOTTOM);
                    result.put(BoxModelConstants.BORDER_BOTTOM_COLOR,String.format(Locale.CHINA,"#%06X",0xFFFFFF & borderBottomColor));
                }
            }
        }

        return Collections.unmodifiableMap(result);
    }