Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

+ [android] support ripple background #792

Merged
merged 5 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ interface Recycler{
String VIF_FALSE = "ifFalse";
String UNDEFINED = "undefined";
String FLAT = "flat";
String RIPPLE_ENABLED = "rippleEnabled";
}

public interface Value {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@
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;
Expand Down Expand Up @@ -68,7 +74,7 @@
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.pesudo.OnActivePseudoListner;
import com.taobao.weex.ui.component.pesudo.OnActivePseudoListener;
import com.taobao.weex.ui.component.pesudo.PesudoStatus;
import com.taobao.weex.ui.component.pesudo.TouchActivePseudoListener;
import com.taobao.weex.ui.flat.FlatComponent;
Expand Down Expand Up @@ -104,7 +110,7 @@
* abstract component
*
*/
public abstract class WXComponent<T extends View> implements IWXObject, IWXActivityStateListener,OnActivePseudoListner {
public abstract class WXComponent<T extends View> implements IWXObject, IWXActivityStateListener,OnActivePseudoListener {

public static final String PROP_FIXED_SIZE = "fixedSize";
public static final String PROP_FS_MATCH_PARENT = "m";
Expand All @@ -124,6 +130,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;
Expand Down Expand Up @@ -432,14 +439,13 @@ protected BorderDrawable getOrCreateBorder() {
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}));
}
}
}
Expand Down Expand Up @@ -1274,10 +1280,63 @@ public void setSticky(String sticky) {
public void setBackgroundColor(String color) {
if (!TextUtils.isEmpty(color)) {
int colorInt = WXResourceUtils.getColor(color);
if (!(colorInt == Color.TRANSPARENT && mBackgroundDrawable == null)){
getOrCreateBorder().setColor(colorInt);
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) {
Expand Down Expand Up @@ -1676,6 +1735,15 @@ protected void setPseudoClassStatus(String clzName,boolean status){
status,
pesudoStyles,
styles.getPesudoResetStyles());

if (resultStyles != null && isRippleEnabled()) {
resultStyles.remove(Constants.Name.BACKGROUND_COLOR);
if (resultStyles.isEmpty()) {
WXLogUtils.d("PseudoClass", "skip empty pseudo styles");
return;
}
}

updateStyleByPesudo(resultStyles);
}

Expand Down Expand Up @@ -1839,4 +1907,14 @@ public void setWaste(boolean waste) {
}
}
}

protected boolean isRippleEnabled() {
try {
Object obj = getDomObject().getAttrs().get(Constants.Name.RIPPLE_ENABLED);
return WXUtils.getBoolean(obj, false);
} catch (Throwable t) {
//ignore
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
/**
* Created by sospartan on 05/01/2017.
*/
public interface OnActivePseudoListner {
public interface OnActivePseudoListener {
void updateActivePseudo(boolean isSet);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@
* Created by sospartan on 05/01/2017.
*/
public class TouchActivePseudoListener implements View.OnTouchListener {
private OnActivePseudoListner mOnActivePseudoListner;
private OnActivePseudoListener mOnActivePseudoListener;
private boolean mIsConsumeOnTouch;

public TouchActivePseudoListener(OnActivePseudoListner l, boolean consumeInTouch) {
mOnActivePseudoListner = l;
public TouchActivePseudoListener(OnActivePseudoListener l, boolean consumeInTouch) {
mOnActivePseudoListener = l;
mIsConsumeOnTouch = consumeInTouch;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (mOnActivePseudoListner != null) {
if (mOnActivePseudoListener != null) {
if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN) {
mOnActivePseudoListner.updateActivePseudo(true);
mOnActivePseudoListener.updateActivePseudo(true);
} else if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) {
mOnActivePseudoListner.updateActivePseudo(false);
mOnActivePseudoListener.updateActivePseudo(false);
}
}
return mIsConsumeOnTouch;
Expand Down