下面列出了android.support.v4.view.ViewCompat#getElevation ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void updateSnackbar(CoordinatorLayout parent, View dependency, View child) {
if (dependency instanceof Snackbar.SnackbarLayout) {
if (mSnackbarHeight == -1) {
mSnackbarHeight = dependency.getHeight();
}
int targetPadding = child.getMeasuredHeight();
int shadow = (int) ViewCompat.getElevation(child);
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) dependency.getLayoutParams();
layoutParams.bottomMargin = targetPadding - shadow;
child.bringToFront();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
child.getParent().requestLayout();
((View) child.getParent()).invalidate();
}
}
}
@Override
public void onDraw(Canvas c, RecyclerView recyclerView, View view,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (isCurrentlyActive) {
Object originalElevation = view.getTag();
if (originalElevation == null) {
originalElevation = ViewCompat.getElevation(view);
float newElevation = 1f + findMaxElevation(recyclerView, view);
ViewCompat.setElevation(view, newElevation);
view.setTag(originalElevation);
}
}
super.onDraw(c, recyclerView, view, dX, dY, actionState, isCurrentlyActive);
}
private float findMaxElevation(RecyclerView recyclerView, View itemView) {
final int childCount = recyclerView.getChildCount();
float max = 0;
for (int i = 0; i < childCount; i++) {
final View child = recyclerView.getChildAt(i);
if (child == itemView) {
continue;
}
final float elevation = ViewCompat.getElevation(child);
if (elevation > max) {
max = elevation;
}
}
return max;
}
@Override
public void onDraw(Canvas c, RecyclerView recyclerView, View view,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (isCurrentlyActive) {
Object originalElevation = view.getTag(android.support.v7.recyclerview.R.id.item_touch_helper_previous_elevation);
if (originalElevation == null) {
originalElevation = ViewCompat.getElevation(view);
float newElevation = 1f + findMaxElevation(recyclerView, view);
ViewCompat.setElevation(view, newElevation);
view.setTag(android.support.v7.recyclerview.R.id.item_touch_helper_previous_elevation, originalElevation);
}
}
super.onDraw(c, recyclerView, view, dX, dY, actionState, isCurrentlyActive);
}
private float findMaxElevation(RecyclerView recyclerView, View itemView) {
final int childCount = recyclerView.getChildCount();
float max = 0;
for (int i = 0; i < childCount; i++) {
final View child = recyclerView.getChildAt(i);
if (child == itemView) {
continue;
}
final float elevation = ViewCompat.getElevation(child);
if (elevation > max) {
max = elevation;
}
}
return max;
}
@Override
public void onDraw(Canvas c, RecyclerView recyclerView, View view,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (isCurrentlyActive) {
Object originalElevation = view.getTag();
if (originalElevation == null) {
originalElevation = ViewCompat.getElevation(view);
float newElevation = 1f + findMaxElevation(recyclerView, view);
ViewCompat.setElevation(view, newElevation);
view.setTag(originalElevation);
}
}
super.onDraw(c, recyclerView, view, dX, dY, actionState, isCurrentlyActive);
}
private float findMaxElevation(RecyclerView recyclerView, View itemView) {
final int childCount = recyclerView.getChildCount();
float max = 0;
for (int i = 0; i < childCount; i++) {
final View child = recyclerView.getChildAt(i);
if (child == itemView) {
continue;
}
final float elevation = ViewCompat.getElevation(child);
if (elevation > max) {
max = elevation;
}
}
return max;
}
private float findMaxElevation(RecyclerView recyclerView, View itemView) {
int childCount = recyclerView.getChildCount();
float max = 0.0f;
for (int i = 0; i < childCount; i++) {
View child = recyclerView.getChildAt(i);
if (child != itemView) {
float elevation = ViewCompat.getElevation(child);
if (elevation > max) {
max = elevation;
}
}
}
return max;
}
private float findMaxElevation(RecyclerView recyclerView, View itemView) {
final int childCount = recyclerView.getChildCount();
float max = 0;
for (int i = 0; i < childCount; i++) {
final View child = recyclerView.getChildAt(i);
if (child == itemView) {
continue;
}
final float elevation = ViewCompat.getElevation(child);
if (elevation > max) {
max = elevation;
}
}
return max;
}
private float findMaxElevation(RecyclerView recyclerView) {
final int childCount = recyclerView.getChildCount();
float max = 0;
for (int i = 0; i < childCount; i++) {
final View child = recyclerView.getChildAt(i);
final float elevation = ViewCompat.getElevation(child);
if (elevation > max) {
max = elevation;
}
}
return max;
}
QueueItemViewHolder(@NonNull final View itemView) {
super(itemView);
btnMenu = itemView.findViewById(R.id.btnMenu);
textTitle = itemView.findViewById(R.id.textTitle);
textArtist = itemView.findViewById(R.id.textArtist);
textDuration = itemView.findViewById(R.id.textDuration);
mDefaultBackground = itemView.getBackground();
mDefaultElevation = ViewCompat.getElevation(itemView);
mElevationSelected = itemView.getResources()
.getDimension(R.dimen.list_drag_selected_item_elevation);
}
@Override
public void executePushChangeCancelable(@NonNull final AnimationInfo fromInfo, @NonNull final AnimationInfo toInfo, @NonNull final Runnable endAction, @NonNull CancellationSignal cancellationSignal) {
// Cannot be placed in onAnimationStart, as there it a post interval, it will splash
final View fromView = fromInfo.mSceneView;
final View toView = toInfo.mSceneView;
AnimatorUtility.resetViewStatus(fromView);
AnimatorUtility.resetViewStatus(toView);
fromView.setVisibility(View.VISIBLE);
final float fromViewElevation = ViewCompat.getElevation(fromView);
if (fromViewElevation > 0) {
ViewCompat.setElevation(fromView, 0);
}
// In the case of pushAndClear, it is possible that the Scene come from has been destroyed.
if (fromInfo.mSceneState.value < State.VIEW_CREATED.value) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mAnimationViewGroup.getOverlay().add(fromView);
} else {
mAnimationViewGroup.addView(fromView);
}
}
Runnable animationEndAction = new CountRunnable(2, new Runnable() {
@Override
public void run() {
if (!toInfo.mIsTranslucent) {
fromView.setVisibility(View.GONE);
}
if (fromViewElevation > 0) {
ViewCompat.setElevation(fromView, fromViewElevation);
}
AnimatorUtility.resetViewStatus(fromView);
AnimatorUtility.resetViewStatus(toView);
if (fromInfo.mSceneState.value < State.VIEW_CREATED.value) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mAnimationViewGroup.getOverlay().remove(fromView);
} else {
mAnimationViewGroup.removeView(fromView);
}
}
endAction.run();
}
});
mEnterAnimator.addEndAction(animationEndAction);
mExitAnimator.addEndAction(animationEndAction);
mExitAnimator.start(fromView);
mEnterAnimator.start(toView);
cancellationSignal.setOnCancelListener(new CancellationSignal.OnCancelListener() {
@Override
public void onCancel() {
mEnterAnimator.end();
mExitAnimator.end();
}
});
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (!(widthMode == 1073741824 && heightMode == 1073741824)) {
if (isInEditMode()) {
if (widthMode != Integer.MIN_VALUE) {
if (widthMode == 0) {
widthSize = 300;
}
}
if (heightMode != Integer.MIN_VALUE) {
if (heightMode == 0) {
heightSize = 300;
}
}
} else {
throw new IllegalArgumentException("DrawerLayout must be measured with MeasureSpec.EXACTLY.");
}
}
setMeasuredDimension(widthSize, heightSize);
boolean applyInsets = this.mLastInsets != null && ViewCompat.getFitsSystemWindows(this);
int layoutDirection = ViewCompat.getLayoutDirection(this);
boolean hasDrawerOnLeftEdge = false;
boolean hasDrawerOnRightEdge = false;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
if (child.getVisibility() != 8) {
MarginLayoutParams lp = (LayoutParams) child.getLayoutParams();
if (applyInsets) {
int cgrav = GravityCompat.getAbsoluteGravity(lp.gravity, layoutDirection);
if (ViewCompat.getFitsSystemWindows(child)) {
IMPL.dispatchChildInsets(child, this.mLastInsets, cgrav);
} else {
IMPL.applyMarginInsets(lp, this.mLastInsets, cgrav);
}
}
if (isContentView(child)) {
child.measure(MeasureSpec.makeMeasureSpec((widthSize - lp.leftMargin) - lp.rightMargin, 1073741824), MeasureSpec.makeMeasureSpec((heightSize - lp.topMargin) - lp.bottomMargin, 1073741824));
} else if (isDrawerView(child)) {
if (SET_DRAWER_SHADOW_FROM_ELEVATION && ViewCompat.getElevation(child) != this.mDrawerElevation) {
ViewCompat.setElevation(child, this.mDrawerElevation);
}
int childGravity = getDrawerViewAbsoluteGravity(child) & 7;
boolean isLeftEdgeDrawer = childGravity == 3;
if (!(isLeftEdgeDrawer && hasDrawerOnLeftEdge) && (isLeftEdgeDrawer || !hasDrawerOnRightEdge)) {
if (isLeftEdgeDrawer) {
hasDrawerOnLeftEdge = true;
} else {
hasDrawerOnRightEdge = true;
}
child.measure(getChildMeasureSpec(widthMeasureSpec, (this.mMinDrawerMargin + lp.leftMargin) + lp.rightMargin, lp.width), getChildMeasureSpec(heightMeasureSpec, lp.topMargin + lp.bottomMargin, lp.height));
} else {
throw new IllegalStateException("Child drawer has absolute gravity " + gravityToString(childGravity) + " but this " + TAG + " already has a " + "drawer view along that edge");
}
} else {
throw new IllegalStateException("Child " + child + " at index " + i + " does not have a valid layout_gravity - must be Gravity.LEFT, " + "Gravity.RIGHT or Gravity.NO_GRAVITY");
}
}
}
}
public State onSaveInstanceState() {
state.translationY = view.getTranslationY();
state.elevation = ViewCompat.getElevation(view);
return state;
}
private void calculateLayout(int width, int height) {
rectView.reset();
rectView.addRect(0f, 0f, 1f * getWidth(), 1f * getHeight(), Path.Direction.CW);
if (clipManager != null) {
if (width > 0 && height > 0) {
clipManager.setupClipLayout(width, height);
clipPath.reset();
clipPath.set(clipManager.createMask(width, height));
if (requiresBitmap()) {
if (clipBitmap != null) {
clipBitmap.recycle();
}
clipBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(clipBitmap);
if (drawable != null) {
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas);
} else {
canvas.drawPath(clipPath, clipManager.getPaint());
}
}
//invert the path for android P
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1) {
final boolean success = rectView.op(clipPath, Path.Op.DIFFERENCE);
}
//this needs to be fixed for 25.4.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && ViewCompat.getElevation(this) > 0f) {
try {
setOutlineProvider(getOutlineProvider());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
postInvalidate();
}