package com.github.takahirom.materialelement.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.ViewOutlineProvider;
import com.github.takahirom.materialelement.R;
import com.github.takahirom.materialelement.util.AndroidVersionUtil;
public class ForegroundConstraintLayout extends ConstraintLayout {
private Drawable foreground;
public ForegroundConstraintLayout(Context context) {
super(context);
}
public ForegroundConstraintLayout(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ForegroundView);
final Drawable d = a.getDrawable(R.styleable.ForegroundView_android_foreground);
if (d != null) {
setForeground(d);
}
a.recycle();
if (AndroidVersionUtil.isGreaterThanL()) {
setOutlineProvider(ViewOutlineProvider.BOUNDS);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (foreground != null) {
foreground.setBounds(0, 0, w, h);
}
}
@Override
public boolean hasOverlappingRendering() {
return false;
}
@Override
protected boolean verifyDrawable(Drawable who) {
return super.verifyDrawable(who) || (who == foreground);
}
@Override
public void jumpDrawablesToCurrentState() {
super.jumpDrawablesToCurrentState();
if (foreground != null) foreground.jumpToCurrentState();
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (foreground != null && foreground.isStateful()) {
foreground.setState(getDrawableState());
}
}
/**
* Returns the drawable used as the foreground of this view. The
* foreground drawable, if non-null, is always drawn on top of the children.
*
* @return A Drawable or null if no foreground was set.
*/
@Override
public Drawable getForeground() {
return foreground;
}
/**
* Supply a Drawable that is to be rendered on top of all of the child
* views within this layout. Any padding in the Drawable will be taken
* into account by ensuring that the children are inset to be placed
* inside of the padding area.
*
* @param drawable The Drawable to be drawn on top of the children.
*/
@Override
public void setForeground(Drawable drawable) {
if (foreground != drawable) {
if (foreground != null) {
foreground.setCallback(null);
unscheduleDrawable(foreground);
}
foreground = drawable;
if (foreground != null) {
foreground.setBounds(getLeft(), getTop(), getRight(), getBottom());
setWillNotDraw(false);
foreground.setCallback(this);
if (foreground.isStateful()) {
foreground.setState(getDrawableState());
}
} else {
setWillNotDraw(true);
}
invalidate();
}
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
if (foreground != null) {
foreground.draw(canvas);
}
}
@Override
public void drawableHotspotChanged(float x, float y) {
super.drawableHotspotChanged(x, y);
if (foreground != null) {
foreground.setHotspot(x, y);
}
}
}