Repository: incubator-weex Updated Branches: refs/heads/master 3949bffae -> 1c7f1ff75
+ [android] support ripple background with pseudo class on API 21 and higher Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/0bf3d925 Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/0bf3d925 Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/0bf3d925 Branch: refs/heads/master Commit: 0bf3d925dcbb38d211ebb23a1eb5451f69e4f55e Parents: 08e1c3e Author: misakuo <misa...@apache.org> Authored: Mon Sep 25 17:13:16 2017 +0800 Committer: misakuo <misa...@apache.org> Committed: Mon Sep 25 17:13:16 2017 +0800 ---------------------------------------------------------------------- .../java/com/taobao/weex/common/Constants.java | 1 + .../taobao/weex/ui/component/WXComponent.java | 79 ++++++++++++++++++-- 2 files changed, 72 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/0bf3d925/android/sdk/src/main/java/com/taobao/weex/common/Constants.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/common/Constants.java b/android/sdk/src/main/java/com/taobao/weex/common/Constants.java index e74e083..de2c604 100644 --- a/android/sdk/src/main/java/com/taobao/weex/common/Constants.java +++ b/android/sdk/src/main/java/com/taobao/weex/common/Constants.java @@ -199,6 +199,7 @@ public class Constants { String VIF_FALSE = "ifFalse"; String UNDEFINED = "undefined"; String FLAT = "flat"; + String RIPPLE_ENABLED = "rippleEnabled"; } public interface Value { http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/0bf3d925/android/sdk/src/main/java/com/taobao/weex/ui/component/WXComponent.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/component/WXComponent.java b/android/sdk/src/main/java/com/taobao/weex/ui/component/WXComponent.java index b389d67..09e5e8d 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/component/WXComponent.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/component/WXComponent.java @@ -21,13 +21,19 @@ package com.taobao.weex.ui.component; import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; +import android.content.res.ColorStateList; +import android.graphics.Canvas; import android.graphics.Color; +import android.graphics.Path; import android.graphics.Point; import android.graphics.PointF; import android.graphics.Rect; +import android.graphics.RectF; import android.graphics.Shader; +import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.LayerDrawable; +import android.graphics.drawable.RippleDrawable; import android.os.Build; import android.os.Message; import android.support.annotation.CallSuper; @@ -44,6 +50,7 @@ import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.MarginLayoutParams; import android.widget.FrameLayout; + import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.taobao.weex.ComponentObserver; @@ -67,8 +74,6 @@ import com.taobao.weex.tracing.Stopwatch; import com.taobao.weex.tracing.WXTracing; import com.taobao.weex.ui.IFComponentHolder; import com.taobao.weex.ui.animation.WXAnimationModule; -import com.taobao.weex.ui.component.binding.Statements; -import com.taobao.weex.ui.component.list.WXCell; import com.taobao.weex.ui.component.pesudo.OnActivePseudoListner; import com.taobao.weex.ui.component.pesudo.PesudoStatus; import com.taobao.weex.ui.component.pesudo.TouchActivePseudoListener; @@ -88,6 +93,7 @@ import com.taobao.weex.utils.WXReflectionUtils; import com.taobao.weex.utils.WXResourceUtils; import com.taobao.weex.utils.WXUtils; import com.taobao.weex.utils.WXViewUtils; + import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -100,6 +106,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import static com.taobao.weex.utils.WXUtils.getBoolean; + /** * abstract component * @@ -124,6 +132,7 @@ public abstract class WXComponent<T extends View> implements IWXObject, IWXActi private Set<String> mGestureType; private BorderDrawable mBackgroundDrawable; + private Drawable mRippleBackground; private int mPreRealWidth = 0; private int mPreRealHeight = 0; private int mPreRealLeft = 0; @@ -432,14 +441,13 @@ public abstract class WXComponent<T extends View> implements IWXObject, IWXActi if (mBackgroundDrawable == null) { mBackgroundDrawable = new BorderDrawable(); if (mHost != null) { - Drawable backgroundDrawable = mHost.getBackground(); WXViewUtils.setBackGround(mHost, null); - if (backgroundDrawable == null) { + if (mRippleBackground == null) { WXViewUtils.setBackGround(mHost, mBackgroundDrawable); } else { //TODO Not strictly clip according to background-clip:border-box WXViewUtils.setBackGround(mHost, new LayerDrawable(new Drawable[]{ - mBackgroundDrawable, backgroundDrawable})); + mRippleBackground, mBackgroundDrawable})); } } } @@ -985,7 +993,7 @@ public abstract class WXComponent<T extends View> implements IWXObject, IWXActi if (view instanceof WXGestureObservable) { if (mGesture == null) { mGesture = new WXGesture(this, mContext); - boolean isPreventMove = WXUtils.getBoolean(getDomObject().getAttrs().get(Constants.Name.PREVENT_MOVE_EVENT),false); + boolean isPreventMove = getBoolean(getDomObject().getAttrs().get(Constants.Name.PREVENT_MOVE_EVENT),false); mGesture.setPreventMoveEvent(isPreventMove); } mGestureType.add(type); @@ -1270,12 +1278,67 @@ public abstract class WXComponent<T extends View> implements IWXObject, IWXActi public void setBackgroundColor(String color) { if (!TextUtils.isEmpty(color)) { int colorInt = WXResourceUtils.getColor(color); - if (!(colorInt == Color.TRANSPARENT && mBackgroundDrawable == null)){ - getOrCreateBorder().setColor(colorInt); + Object obj = getDomObject().getAttrs().get(Constants.Name.RIPPLE_ENABLED); + boolean isRippleEnabled = getBoolean(obj, false); + if (isRippleEnabled && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + mRippleBackground = prepareBackgroundRipple(); + if (mRippleBackground != null) { + if (mBackgroundDrawable == null) { + WXViewUtils.setBackGround(mHost, mRippleBackground); + } else { + LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{mRippleBackground, mBackgroundDrawable}); + WXViewUtils.setBackGround(mHost, layerDrawable); + } + return; + } + } + if (!(colorInt == Color.TRANSPARENT && mBackgroundDrawable == null)) { + getOrCreateBorder().setColor(colorInt); } } } + private RippleDrawable prepareBackgroundRipple() { + try { + if (getDomObject().getStyles() != null && getDomObject().getStyles().getPesudoResetStyles() != null) { + Map<String, Object> resetStyles = getDomObject().getStyles().getPesudoResetStyles(); + + Object bgColor = resetStyles.get(Constants.Name.BACKGROUND_COLOR); + int colorInt = Color.TRANSPARENT; + if (bgColor != null) { + colorInt = WXResourceUtils.getColor(bgColor.toString(), Color.TRANSPARENT); + if (colorInt == Color.TRANSPARENT) { + return null; + } + } + + Object bg = resetStyles.get(Constants.Name.BACKGROUND_COLOR + Constants.PSEUDO.ACTIVE); + if (bg == null) { + return null; + } + int rippleColor = WXResourceUtils.getColor(bg.toString(), colorInt); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + ColorStateList colorStateList = new ColorStateList( + new int[][]{new int[]{}}, + new int[]{rippleColor}); + return new RippleDrawable(colorStateList, new ColorDrawable(colorInt), null) { + @Override + public void draw(@NonNull Canvas canvas) { + if (mBackgroundDrawable != null) { + Path border = mBackgroundDrawable.getContentPath(new RectF(0, 0, canvas.getWidth(), canvas.getHeight())); + canvas.clipPath(border); + } + super.draw(canvas); + } + }; + } + } + } catch (Throwable t) { + WXLogUtils.w("Exception on create ripple: ", t); + } + return null; + } + public void setBackgroundImage(@NonNull String bgImage) { if ("".equals(bgImage.trim())) { getOrCreateBorder().setImage(null);