下面列出了怎么用android.view.ViewOutlineProvider的API类实例代码及写法,或者点击链接到github查看源代码。
private void updateCorners() {
if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
setClipToOutline(true);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
if (Carbon.isShapeRect(shapeModel, boundsRect)) {
outline.setRect(0, 0, getWidth(), getHeight());
} else {
shadowDrawable.setBounds(0, 0, getWidth(), getHeight());
shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER);
shadowDrawable.getOutline(outline);
}
}
});
}
boundsRect.set(shadowDrawable.getBounds());
shadowDrawable.getPathForSize(getWidth(), getHeight(), cornersMask);
}
/**
* Constructor.
* @param context the application context.
* @param attrs attributes.
* @param defStyleAttr attribute in the current theme that contains a
* reference to a style resource that supplies defaults
* values for the StyledAttributes
* @param defStyleRes a resource identifier of a style resource that
* supplies default values for the StyledAttributes,
* used only if defStyleAttr is 0 or can not be found
* in the theme
*/
public FloatingActionButton(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr);
setClickable(true);
// Set the outline provider for this view. The provider is given the
// outline which it can then modify as needed. In this case we set the
// outline to be an oval fitting the height and width.
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, getWidth(), getHeight());
}
});
// Finally, enable clipping to the outline, using the provider we set
// above
setClipToOutline(true);
}
private void init(Context context) {
selectedItemColor = AppUtils.fetchContextColor(context, R.attr.colorAccent);
unselectedItemColor = Color.LTGRAY;
barBackgroundColor = Color.WHITE;
badgeColor = Color.parseColor("#FF3B30");
setLayoutParams(new ViewGroup.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)));
LayoutInflater inflater = LayoutInflater.from(getContext());
View parentView = inflater.inflate(R.layout.nav_tab_bar_container, this, true);
container = parentView.findViewById(R.id.nav_tab_bar_container);
tabContainer = parentView.findViewById(R.id.nav_tab_bar_item_container);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
this.setOutlineProvider(ViewOutlineProvider.BOUNDS);
}
ViewCompat.setElevation(this, 0);
setClipToPadding(false);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public ViewOutlineProvider getOutlineProvider() {
return new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
if (clipManager != null && !isInEditMode()) {
final Path shadowConvexPath = clipManager.getShadowConvexPath();
if (shadowConvexPath != null) {
try {
outline.setConvexPath(shadowConvexPath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
};
}
private void init() {
setWillNotDraw(false);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.FILL);
mPath = new Path();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mViewOutlineProvider = new ViewOutlineProvider() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void getOutline(View view, Outline outline) {
if (mPath.isConvex()) outline.setConvexPath(mPath);
}
};
}
}
private void updateCorners() {
if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
setClipToOutline(true);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
if (Carbon.isShapeRect(shapeModel, boundsRect)) {
outline.setRect(0, 0, getWidth(), getHeight());
} else {
shadowDrawable.setBounds(0, 0, getWidth(), getHeight());
shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER);
shadowDrawable.getOutline(outline);
}
}
});
}
boundsRect.set(shadowDrawable.getBounds());
shadowDrawable.getPathForSize(getWidth(), getHeight(), cornersMask);
}
private void initView() {
ViewCompat.setElevation(this, DensityUtil.dp2px(getContext(), 8));
ViewCompat.setBackground(this, getResources().getDrawable(R.drawable.shape_audio_bar));
setOutlineProvider(ViewOutlineProvider.BACKGROUND);
ivCover.setOnClickListener(this::startPlayerActivity);
btnPause.setOnClickListener(v -> startPlayerActivity(ivCover));
final OnLongClickListener longClickListener = v -> {
AudioBookPlayService.stop(getContext());
return true;
};
ivCover.setOnLongClickListener(longClickListener);
btnPause.setOnLongClickListener(longClickListener);
}
private void updateCorners() {
if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
setClipToOutline(true);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
if (Carbon.isShapeRect(shapeModel, boundsRect)) {
outline.setRect(0, 0, getWidth(), getHeight());
} else {
shadowDrawable.setBounds(0, 0, getWidth(), getHeight());
shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER);
shadowDrawable.getOutline(outline);
}
}
});
}
boundsRect.set(shadowDrawable.getBounds());
shadowDrawable.getPathForSize(getWidth(), getHeight(), cornersMask);
}
HeaderElevationController(View header) {
mHeader = header;
final Resources res = mHeader.getContext().getResources();
mMaxElevation = res.getDimension(R.dimen.all_apps_header_max_elevation);
mScrollToElevation = res.getDimension(R.dimen.all_apps_header_scroll_to_elevation);
// We need to provide a custom outline so the shadow only appears on the bottom edge.
// The top, left and right edges are all extended out, and the shadow is clipped
// by the parent.
final ViewOutlineProvider vop = new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
final View parent = (View) mHeader.getParent();
final int left = parent.getLeft(); // Use the parent to account for offsets
final int top = view.getTop();
final int right = left + view.getWidth();
final int bottom = view.getBottom();
final int offset = Utilities.pxFromDp(mMaxElevation, res.getDisplayMetrics());
outline.setRect(left - offset, top - offset, right + offset, bottom);
}
};
mHeader.setOutlineProvider(vop);
}
private void updateCorners() {
if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
setClipToOutline(true);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
if (Carbon.isShapeRect(shapeModel, boundsRect)) {
outline.setRect(0, 0, getWidth(), getHeight());
} else {
shadowDrawable.setBounds(0, 0, getWidth(), getHeight());
shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER);
shadowDrawable.getOutline(outline);
}
}
});
}
boundsRect.set(shadowDrawable.getBounds());
shadowDrawable.getPathForSize(getWidth(), getHeight(), cornersMask);
}
private void updateCorners() {
if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
setClipToOutline(true);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
if (Carbon.isShapeRect(shapeModel, boundsRect)) {
outline.setRect(0, 0, getWidth(), getHeight());
} else {
shadowDrawable.setBounds(0, 0, getWidth(), getHeight());
shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER);
shadowDrawable.getOutline(outline);
}
}
});
}
boundsRect.set(shadowDrawable.getBounds());
shadowDrawable.getPathForSize(getWidth(), getHeight(), cornersMask);
}
@Override
protected void onSizeChanged(final int w, final int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mDrawable.setBounds(0, 0, w, h);
mWidth = w;
mHeight = h;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setOutlineProvider(new ViewOutlineProvider() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, view.getWidth(), view.getHeight());
}
});
setClipToOutline(true);
}
}
/**
* This method initiates the bottomNavigationBar and handles layout related values
*/
private void init() {
// MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) getContext().getResources().getDimension(R.dimen.bottom_navigation_padded_height)));
// marginParams.setMargins(0, (int) getContext().getResources().getDimension(R.dimen.bottom_navigation_top_margin_correction), 0, 0);
setLayoutParams(new ViewGroup.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)));
LayoutInflater inflater = LayoutInflater.from(getContext());
View parentView = inflater.inflate(R.layout.bottom_navigation_bar_container, this, true);
mBackgroundOverlay = (FrameLayout) parentView.findViewById(R.id.bottom_navigation_bar_overLay);
mContainer = (FrameLayout) parentView.findViewById(R.id.bottom_navigation_bar_container);
mTabContainer = (LinearLayout) parentView.findViewById(R.id.bottom_navigation_bar_item_container);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
this.setOutlineProvider(ViewOutlineProvider.BOUNDS);
} else {
//to do
}
ViewCompat.setElevation(this, mElevation);
setClipToPadding(false);
}
private void updateCorners() {
if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
setClipToOutline(true);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
if (Carbon.isShapeRect(shapeModel, boundsRect)) {
outline.setRect(0, 0, getWidth(), getHeight());
} else {
shadowDrawable.setBounds(0, 0, getWidth(), getHeight());
shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER);
shadowDrawable.getOutline(outline);
}
}
});
}
boundsRect.set(shadowDrawable.getBounds());
shadowDrawable.getPathForSize(getWidth(), getHeight(), cornersMask);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public ViewOutlineProvider getOutlineProvider() {
shadowpath = new Path();
if (config.getRadius() == 0) {
shadowpath = path;
} else {
rect = new Rect(0, 0, (int) width, (int) height);
RectF r = new RectF(rect);
shadowpath.addRoundRect(r, config.getRadius(), config.getRadius(), Path.Direction.CCW);
shadowpath.op(path, shadowpath, Path.Op.INTERSECT);
}
return new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
if (path.isConvex()) {
outline.setConvexPath(shadowpath);
}
}
};
}
public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr);
setClickable(true);
// Set the outline provider for this view. The provider is given the outline which it can
// then modify as needed. In this case we set the outline to be an oval fitting the height
// and width.
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, getWidth(), getHeight());
}
});
// Finally, enable clipping to the outline, using the provider we set above
setClipToOutline(true);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[]{-android.R.attr.state_enabled}, createCircleDrawable(mColorDisabled));
drawable.addState(new int[]{android.R.attr.state_pressed}, createCircleDrawable(mColorPressed));
drawable.addState(new int[]{}, createCircleDrawable(mColorNormal));
if (Util.hasLollipop()) {
RippleDrawable ripple = new RippleDrawable(new ColorStateList(new int[][]{{}},
new int[]{mColorRipple}), drawable, null);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, view.getWidth(), view.getHeight());
}
});
setClipToOutline(true);
mBackgroundDrawable = ripple;
return ripple;
}
mBackgroundDrawable = drawable;
return drawable;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[]{android.R.attr.state_pressed}, createRectDrawable(mColorPressed));
drawable.addState(new int[]{}, createRectDrawable(mColorNormal));
if (Util.hasLollipop()) {
RippleDrawable ripple = new RippleDrawable(new ColorStateList(new int[][]{{}},
new int[]{mColorRipple}), drawable, null);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, view.getWidth(), view.getHeight());
}
});
setClipToOutline(true);
mBackgroundDrawable = ripple;
return ripple;
}
mBackgroundDrawable = drawable;
return drawable;
}
private void updateCorners() {
if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
setClipToOutline(true);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
if (Carbon.isShapeRect(shapeModel, boundsRect)) {
outline.setRect(0, 0, getWidth(), getHeight());
} else {
shadowDrawable.setBounds(0, 0, getWidth(), getHeight());
shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER);
shadowDrawable.getOutline(outline);
}
}
});
}
boundsRect.set(shadowDrawable.getBounds());
shadowDrawable.getPathForSize(getWidth(), getHeight(), cornersMask);
}
private void updateCorners() {
if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
setClipToOutline(true);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
if (Carbon.isShapeRect(shapeModel, boundsRect)) {
outline.setRect(0, 0, getWidth(), getHeight());
} else {
shadowDrawable.setBounds(0, 0, getWidth(), getHeight());
shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER);
shadowDrawable.getOutline(outline);
}
}
});
}
boundsRect.set(shadowDrawable.getBounds());
shadowDrawable.getPathForSize(getWidth(), getHeight(), cornersMask);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
outline.set(0, 0, getMeasuredWidth(), getMeasuredHeight());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setClipToOutline(true);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline viewOutline) {
viewOutline.setRoundRect(
(int) outline.left,
(int) outline.top,
(int) outline.right,
(int) outline.bottom,
outline.height() / 2
);
}
});
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setClipToOutline(true);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setRoundRect(
0,
0,
view.getMeasuredWidth(),
view.getMeasuredHeight(),
view.getMeasuredHeight() / 2f
);
}
});
}
}
private void updateCorners() {
if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
setClipToOutline(true);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
if (Carbon.isShapeRect(shapeModel, boundsRect)) {
outline.setRect(0, 0, getWidth(), getHeight());
} else {
shadowDrawable.setBounds(0, 0, getWidth(), getHeight());
shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER);
shadowDrawable.getOutline(outline);
}
}
});
}
boundsRect.set(shadowDrawable.getBounds());
shadowDrawable.getPathForSize(getWidth(), getHeight(), cornersMask);
}
private void initOutlineProvider() {
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
setOutlineProvider(
new ViewOutlineProvider() {
@Override
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void getOutline(View view, @NonNull Outline outline) {
if (chipDrawable != null) {
chipDrawable.getOutline(outline);
} else {
outline.setAlpha(0.0f);
}
}
});
}
}
private void updateCorners() {
if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
setClipToOutline(true);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
if (Carbon.isShapeRect(shapeModel, boundsRect)) {
outline.setRect(0, 0, getWidth(), getHeight());
} else {
shadowDrawable.setBounds(0, 0, getWidth(), getHeight());
shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER);
shadowDrawable.getOutline(outline);
}
}
});
}
boundsRect.set(shadowDrawable.getBounds());
shadowDrawable.getPathForSize(getWidth(), getHeight(), cornersMask);
}
private void initialize() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setOutlineProvider(new ViewOutlineProvider() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void getOutline(View view, Outline outline) {
outline.setOval(
view.getPaddingLeft(),
view.getPaddingTop(),
view.getWidth() - view.getPaddingRight(),
view.getHeight() - view.getPaddingBottom()
);
}
});
setClipToOutline(true);
}
}
private void updateCorners() {
if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
setClipToOutline(true);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
if (Carbon.isShapeRect(shapeModel, boundsRect)) {
outline.setRect(0, 0, getWidth(), getHeight());
} else {
shadowDrawable.setBounds(0, 0, getWidth(), getHeight());
shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER);
shadowDrawable.getOutline(outline);
}
}
});
}
boundsRect.set(shadowDrawable.getBounds());
shadowDrawable.getPathForSize(getWidth(), getHeight(), cornersMask);
}
private void updateCorners() {
if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
setClipToOutline(true);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
if (Carbon.isShapeRect(shapeModel, boundsRect)) {
outline.setRect(0, 0, getWidth(), getHeight());
} else {
shadowDrawable.setBounds(0, 0, getWidth(), getHeight());
shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER);
shadowDrawable.getOutline(outline);
}
}
});
}
boundsRect.set(shadowDrawable.getBounds());
shadowDrawable.getPathForSize(getWidth(), getHeight(), cornersMask);
}
/**
* This method initiates the bottomNavigationBar and handles layout related values
*/
private void init() {
// MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) getContext().getResources().getDimension(R.dimen.bottom_navigation_padded_height)));
// marginParams.setMargins(0, (int) getContext().getResources().getDimension(R.dimen.bottom_navigation_top_margin_correction), 0, 0);
setLayoutParams(new ViewGroup.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)));
LayoutInflater inflater = LayoutInflater.from(getContext());
View parentView = inflater.inflate(R.layout.bottom_navigation_bar_container, this, true);
mBackgroundOverlay = parentView.findViewById(R.id.bottom_navigation_bar_overLay);
mContainer = parentView.findViewById(R.id.bottom_navigation_bar_container);
mTabContainer = parentView.findViewById(R.id.bottom_navigation_bar_item_container);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
this.setOutlineProvider(ViewOutlineProvider.BOUNDS);
} else {
//to do
}
ViewCompat.setElevation(this, mElevation);
setClipToPadding(false);
}
private void updateCorners() {
if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
setClipToOutline(true);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
if (Carbon.isShapeRect(shapeModel, boundsRect)) {
outline.setRect(0, 0, getWidth(), getHeight());
} else {
shadowDrawable.setBounds(0, 0, getWidth(), getHeight());
shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER);
shadowDrawable.getOutline(outline);
}
}
});
}
boundsRect.set(shadowDrawable.getBounds());
shadowDrawable.getPathForSize(getWidth(), getHeight(), cornersMask);
}