下面列出了android.view.View#setElevation ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected static void setOriginalSharedElementState(ArrayList<View> sharedElements,
ArrayList<SharedElementOriginalState> originalState) {
for (int i = 0; i < originalState.size(); i++) {
View view = sharedElements.get(i);
SharedElementOriginalState state = originalState.get(i);
if (view instanceof ImageView && state.mScaleType != null) {
ImageView imageView = (ImageView) view;
imageView.setScaleType(state.mScaleType);
if (state.mScaleType == ImageView.ScaleType.MATRIX) {
imageView.setImageMatrix(state.mMatrix);
}
}
view.setElevation(state.mElevation);
view.setTranslationZ(state.mTranslationZ);
int widthSpec = View.MeasureSpec.makeMeasureSpec(state.mMeasuredWidth,
View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(state.mMeasuredHeight,
View.MeasureSpec.EXACTLY);
view.measure(widthSpec, heightSpec);
view.layout(state.mLeft, state.mTop, state.mRight, state.mBottom);
}
}
/**
* Set elevation if supported.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static boolean setElevation(View view, float elevationValue) {
if (!isElevationSupported()) return false;
view.setElevation(elevationValue);
return true;
}
private void setFloatingElevationVisibility(final View floatingConfirmLayout, final boolean visible) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final float elevationInPixels =
visible ? getBaseContext().getResources().getDimension(R.dimen.px_xxs_margin) : 0;
floatingConfirmLayout.setElevation(elevationInPixels);
}
}
@Override
public void onViewDragStateChanged(int state) {
if (state == ViewDragHelper.STATE_IDLE) {
View capturedView = dragHelper.getCapturedView();
if (capturedView != null) {
capturedView.setElevation(capturedView.getElevation() - 1);
}
}
}
protected void setItemViewProperty(View itemView, float targetOffset) {
float scale = calculateScale(targetOffset + mSpaceMain);
itemView.setScaleX(scale);
itemView.setScaleY(scale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
itemView.setElevation(0);
}
final float rotation = calRotation(targetOffset);
if (getOrientation() == HORIZONTAL) {
itemView.setRotationY(rotation);
} else {
itemView.setRotationX(-rotation);
}
}
private void bindCommonDynamicProp(int key, DynamicValue<?> value, View target) {
switch (key) {
case KEY_ALPHA:
target.setAlpha(DynamicPropsManager.<Float>resolve(value));
break;
case KEY_TRANSLATION_X:
target.setTranslationX(DynamicPropsManager.<Float>resolve(value));
break;
case KEY_TRANSLATION_Y:
target.setTranslationY(DynamicPropsManager.<Float>resolve(value));
break;
case KEY_SCALE_X:
target.setScaleX(DynamicPropsManager.<Float>resolve(value));
break;
case KEY_SCALE_Y:
target.setScaleY(DynamicPropsManager.<Float>resolve(value));
break;
case KEY_ELEVATION:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
target.setElevation(DynamicPropsManager.<Float>resolve(value));
}
break;
case KEY_BACKGROUND_COLOR:
target.setBackgroundColor(DynamicPropsManager.<Integer>resolve(value));
break;
case KEY_ROTATION:
target.setRotation(DynamicPropsManager.<Float>resolve(value));
break;
}
}
/**
* Sets the base elevation of this view, in pixels.
*/
@TargetApi(21)
public static void setElevation(View view, float elevation) {
if (Build.VERSION.SDK_INT >= 21) {
view.setElevation(elevation);
}
}
/**
* Adds an arrow view pointing at the original icon.
* @param horizontalOffset the horizontal offset of the arrow, so that it
* points at the center of the original icon
*/
private View addArrowView(int horizontalOffset, int verticalOffset, int width, int height) {
LayoutParams layoutParams = new LayoutParams(width, height);
if (mIsLeftAligned) {
layoutParams.gravity = Gravity.START;
layoutParams.leftMargin = horizontalOffset;
} else {
layoutParams.gravity = Gravity.END;
layoutParams.rightMargin = horizontalOffset;
}
if (mIsAboveIcon) {
layoutParams.topMargin = verticalOffset;
} else {
layoutParams.bottomMargin = verticalOffset;
}
View arrowView = new View(getContext());
if (Gravity.isVertical(((FrameLayout.LayoutParams) getLayoutParams()).gravity)) {
// This is only true if there wasn't room for the container next to the icon,
// so we centered it instead. In that case we don't want to show the arrow.
arrowView.setVisibility(INVISIBLE);
} else {
ShapeDrawable arrowDrawable = new ShapeDrawable(TriangleShape.create(
width, height, !mIsAboveIcon));
Paint arrowPaint = arrowDrawable.getPaint();
// Note that we have to use getChildAt() instead of getItemViewAt(),
// since the latter expects the arrow which hasn't been added yet.
PopupItemView itemAttachedToArrow = (PopupItemView)
(getChildAt(mIsAboveIcon ? getChildCount() - 1 : 0));
arrowPaint.setColor(itemAttachedToArrow.getArrowColor(mIsAboveIcon));
// The corner path effect won't be reflected in the shadow, but shouldn't be noticeable.
int radius = getResources().getDimensionPixelSize(R.dimen.popup_arrow_corner_radius);
arrowPaint.setPathEffect(new CornerPathEffect(radius));
arrowView.setBackground(arrowDrawable);
arrowView.setElevation(getElevation());
}
addView(arrowView, mIsAboveIcon ? getChildCount() : 0, layoutParams);
return arrowView;
}
public SaturationValuePicker(Context context, AttributeSet attrs) {
super(context, attrs);
mColor = new Color();
setClipChildren(false);
setClipToPadding(false);
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
mDensity = metrics.density;
mSelectorRadius = SELECTOR_RADIUS_DP * mDensity;
View v = new View(context);
v.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
));
// We want the selector to protrude as minimally as possible from the selection drawable and
// we want to inset the gradients in the drawable so that the color under the selector's
// midpoint always reflects the current color (thus the limits of the saturation-value
// gradients have to be clamped at this inset. The inset value is thus the shorter side of
// a 45 degree triangle (1 / root 2) with hypotnus equal to the radius of the selector.
mInset = mSelectorRadius / (float) Math.sqrt(2);
mSVDrawable = new SaturationValueDrawable(mInset);
v.setBackground(mSVDrawable);
addView(v);
mSelectorParams = new LayoutParams(
Math.round(mSelectorRadius * 2), Math.round(mSelectorRadius * 2)
);
mSelector = new View(context);
mSelector.setLayoutParams(mSelectorParams);
mSelectorDrawable = (GradientDrawable) ContextCompat.getDrawable(
context, R.drawable.drawable_selector);
mSelector.setBackground(mSelectorDrawable);
mSelector.setElevation(SELECTOR_ELEVATION_DP * mDensity);
addView(mSelector);
}
@Override
public void initialize(ToggleGroupDelegate groupView, Context context,
ColorStateList backgroundColor, float radius, float elevation, float maxElevation) {
final RoundRectDrawable background = new RoundRectDrawable(backgroundColor, radius);
groupView.setGroupBackground(background);
View view = groupView.getToggleGroup();
view.setClipToOutline(true);
view.setElevation(elevation);
setMaxElevation(groupView, maxElevation);
}
public HuePicker(Context context, AttributeSet attrs) {
super(context, attrs);
mListeners = new ArrayList<OnHueChangeListener>();
setClipChildren(false);
setClipToPadding(false);
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
mDensity = metrics.density;
mSelectorRadius = SELECTOR_RADIUS_DP * mDensity;
// We want the selector to protrude as minimally as possible from the background drawable
// and we want to inset the gradients in the drawable so that the color under the
// selector's midpoint always reflects the current color (thus the limits of the background
// gradient have to be clamped at this inset. The inset value is thus the base side of a
// triangle extending from the midpoint of the selector placed in the desired position, a
// hypotenuse equal to the selector radius and a height side equal to half of the height of
// the background drawable.
float height = BACKGROUND_HEIGHT_DP * mDensity / 2.0f;
mInset = Math.round(
Math.sqrt(mSelectorRadius * mSelectorRadius - height * height)
);
mSelectorParams = new LayoutParams(
Math.round(mSelectorRadius * 2), Math.round(mSelectorRadius * 2)
);
mBackground = new View(context);
mBackground.setBackground(new HueDrawable());
addView(mBackground);
mSelector = new View(getContext());
mSelector.setLayoutParams(mSelectorParams);
mSelectorDrawable = (GradientDrawable) ContextCompat.getDrawable(
getContext(),
R.drawable.drawable_selector);
mSelector.setBackground(mSelectorDrawable);
mSelector.setElevation(SELECTOR_ELEVATION_DP * mDensity);
addView(mSelector);
// Full saturation / full value color used for the selector background
mHueColor = new Color(0xffff0000);
updateSelectorColor();
}
/**
* Show specific status UI
* @param status status
* @see #showLoading()
* @see #showLoadFailed()
* @see #showLoadSuccess()
* @see #showEmpty()
*/
public void showLoadingStatus(int status) {
if (curState == status || !validate()) {
return;
}
curState = status;
//first try to reuse status view
View convertView = mStatusViews.get(status);
if (convertView == null) {
//secondly try to reuse current status view
convertView = mCurStatusView;
}
try {
//call customer adapter to get UI for specific status. convertView can be reused
View view = mAdapter.getView(this, convertView, status);
if (view == null) {
printLog(mAdapter.getClass().getName() + ".getView returns null");
return;
}
if (view != mCurStatusView || mWrapper.indexOfChild(view) < 0) {
if (mCurStatusView != null) {
mWrapper.removeView(mCurStatusView);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.setElevation(Float.MAX_VALUE);
}
mWrapper.addView(view);
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp != null) {
lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
}
} else if (mWrapper.indexOfChild(view) != mWrapper.getChildCount() - 1) {
// make sure loading status view at the front
view.bringToFront();
}
mCurStatusView = view;
mStatusViews.put(status, view);
} catch(Exception e) {
if (DEBUG) {
e.printStackTrace();
}
}
}
/**
* Show specific status UI
* @param status status
* @see #showLoading()
* @see #showLoadFailed()
* @see #showLoadSuccess()
* @see #showEmpty()
*/
public void showLoadingStatus(int status) {
if (curState == status || !validate()) {
return;
}
curState = status;
//first try to reuse status view
View convertView = mStatusViews.get(status);
if (convertView == null) {
//secondly try to reuse current status view
convertView = mCurStatusView;
}
try {
//call customer adapter to get UI for specific status. convertView can be reused
View view = mAdapter.getView(this, convertView, status);
if (view == null) {
printLog(mAdapter.getClass().getName() + ".getView returns null");
return;
}
if (view != mCurStatusView || mWrapper.indexOfChild(view) < 0) {
if (mCurStatusView != null) {
mWrapper.removeView(mCurStatusView);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.setElevation(Float.MAX_VALUE);
}
mWrapper.addView(view);
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp != null) {
lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
}
} else if (mWrapper.indexOfChild(view) != mWrapper.getChildCount() - 1) {
// 确保加载状态视图在前面
view.bringToFront();
}
mCurStatusView = view;
mStatusViews.put(status, view);
} catch(Exception e) {
if (DEBUG) {
e.printStackTrace();
}
}
}
public ViewHolder(@NonNull View itemView, int itemViewType) {
super(itemView);
if (itemViewType == SMART_PLAYLIST) {
if (shortSeparator != null) {
shortSeparator.setVisibility(View.GONE);
}
itemView.setBackgroundColor(ATHUtil.resolveColor(activity, R.attr.cardBackgroundColor));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
itemView.setElevation(activity.getResources().getDimensionPixelSize(R.dimen.card_elevation));
}
}
if (image != null) {
int iconPadding = activity.getResources().getDimensionPixelSize(R.dimen.list_item_image_icon_padding);
image.setPadding(iconPadding, iconPadding, iconPadding, iconPadding);
image.setColorFilter(ATHUtil.resolveColor(activity, R.attr.iconColor), PorterDuff.Mode.SRC_IN);
}
if (menu != null) {
menu.setOnClickListener(view -> {
final Playlist playlist = dataSet.get(getAdapterPosition());
final PopupMenu popupMenu = new PopupMenu(activity, view);
popupMenu.inflate(getItemViewType() == SMART_PLAYLIST ? R.menu.menu_item_smart_playlist : R.menu.menu_item_playlist);
if (playlist instanceof LastAddedPlaylist) {
popupMenu.getMenu().findItem(R.id.action_clear_playlist).setVisible(false);
}
popupMenu.setOnMenuItemClickListener(item -> {
if (item.getItemId() == R.id.action_clear_playlist) {
if (playlist instanceof AbsSmartPlaylist) {
ClearSmartPlaylistDialog.create((AbsSmartPlaylist) playlist).show(activity.getSupportFragmentManager(), "CLEAR_SMART_PLAYLIST_" + playlist.name);
return true;
}
}
return PlaylistMenuHelper.handleMenuClick(
activity, dataSet.get(getAdapterPosition()), item);
});
popupMenu.show();
});
}
}
@Override
public void onViewCaptured(@NonNull View capturedChild, int activePointerId) {
capturedChild.setElevation(getElevation() + 1);
capturedLeft = capturedChild.getLeft();
capturedTop = capturedChild.getTop();
}
@Override
public void setRadiusAndShadow(int radius, @ILayout.HideRadiusSide int hideRadiusSide, int shadowElevation, float shadowAlpha) {
View owner = mOwner.get();
if (owner == null) {
return;
}
mRadius = radius;
mHideRadiusSide = hideRadiusSide;
if (mRadius > 0) {
if (hideRadiusSide == HIDE_RADIUS_SIDE_TOP) {
mRadiusArray = new float[]{0, 0, 0, 0, mRadius, mRadius, mRadius, mRadius};
} else if (hideRadiusSide == HIDE_RADIUS_SIDE_RIGHT) {
mRadiusArray = new float[]{mRadius, mRadius, 0, 0, 0, 0, mRadius, mRadius};
} else if (hideRadiusSide == HIDE_RADIUS_SIDE_BOTTOM) {
mRadiusArray = new float[]{mRadius, mRadius, mRadius, mRadius, 0, 0, 0, 0};
} else if (hideRadiusSide == HIDE_RADIUS_SIDE_LEFT) {
mRadiusArray = new float[]{0, 0, mRadius, mRadius, mRadius, mRadius, 0, 0};
} else {
mRadiusArray = null;
}
}
mShadowElevation = shadowElevation;
mShadowAlpha = shadowAlpha;
if (useFeature()) {
if (mShadowElevation == 0 || isRadiusWithSideHidden()) {
owner.setElevation(0);
} else {
owner.setElevation(mShadowElevation);
}
owner.setOutlineProvider(new ViewOutlineProvider() {
@Override
@TargetApi(21)
public void getOutline(View view, Outline outline) {
int w = view.getWidth(), h = view.getHeight();
if (w == 0 || h == 0) {
return;
}
if (isRadiusWithSideHidden()) {
int left = 0, top = 0, right = w, bottom = h;
if (mHideRadiusSide == HIDE_RADIUS_SIDE_LEFT) {
left -= mRadius;
} else if (mHideRadiusSide == HIDE_RADIUS_SIDE_TOP) {
top -= mRadius;
} else if (mHideRadiusSide == HIDE_RADIUS_SIDE_RIGHT) {
right += mRadius;
} else if (mHideRadiusSide == HIDE_RADIUS_SIDE_BOTTOM) {
bottom += mRadius;
}
outline.setRoundRect(left, top,
right, bottom, mRadius);
return;
}
int top = mOutlineInsetTop, bottom = Math.max(top + 1, h - mOutlineInsetBottom),
left = mOutlineInsetLeft, right = w - mOutlineInsetRight;
if (mIsOutlineExcludePadding) {
left += view.getPaddingLeft();
top += view.getPaddingTop();
right = Math.max(left + 1, right - view.getPaddingRight());
bottom = Math.max(top + 1, bottom - view.getPaddingBottom());
}
outline.setAlpha(mShadowAlpha);
if (mRadius <= 0) {
outline.setRect(left, top,
right, bottom);
} else {
outline.setRoundRect(left, top,
right, bottom, mRadius);
}
}
});
owner.setClipToOutline(mRadius > 0);
}
owner.invalidate();
}
public static void setElevation(View view, float elevation) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
view.setElevation(elevation);
}
public ViewHolder(@NonNull View itemView, int itemViewType) {
super(itemView);
itemView.setOnLongClickListener(null);
if (itemViewType != HEADER) {
itemView.setBackgroundColor(ATHUtil.resolveColor(activity, R.attr.cardBackgroundColor));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
itemView.setElevation(activity.getResources().getDimensionPixelSize(R.dimen.card_elevation));
}
if (shortSeparator != null) {
shortSeparator.setVisibility(View.GONE);
}
}
if (menu != null) {
if (itemViewType == SONG) {
menu.setVisibility(View.VISIBLE);
menu.setOnClickListener(new SongMenuHelper.OnClickSongMenu(activity) {
@Override
public Song getSong() {
return (Song) dataSet.get(getAdapterPosition());
}
});
} else {
menu.setVisibility(View.GONE);
}
}
switch (itemViewType) {
case ALBUM:
setImageTransitionName(activity.getString(R.string.transition_album_art));
break;
case ARTIST:
setImageTransitionName(activity.getString(R.string.transition_artist_image));
break;
case SONG:
break;
default:
View container = itemView.findViewById(R.id.image_container);
if (container != null) {
container.setVisibility(View.GONE);
}
break;
}
}
public ViewHolder(@NonNull View itemView, int itemViewType) {
super(itemView);
if (itemViewType == SMART_PLAYLIST) {
if (shortSeparator != null) {
shortSeparator.setVisibility(View.GONE);
}
itemView.setBackgroundColor(ATHUtil.resolveColor(activity, R.attr.cardBackgroundColor));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
itemView.setElevation(activity.getResources().getDimensionPixelSize(R.dimen.card_elevation));
}
}
if (image != null) {
int iconPadding = activity.getResources().getDimensionPixelSize(R.dimen.list_item_image_icon_padding);
image.setPadding(iconPadding, iconPadding, iconPadding, iconPadding);
image.setColorFilter(ATHUtil.resolveColor(activity, R.attr.iconColor), PorterDuff.Mode.SRC_IN);
}
if (menu != null) {
menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Playlist playlist = dataSet.get(getAdapterPosition());
final PopupMenu popupMenu = new PopupMenu(activity, view);
popupMenu.inflate(getItemViewType() == SMART_PLAYLIST ? R.menu.menu_item_smart_playlist : R.menu.menu_item_playlist);
if (playlist instanceof LastAddedPlaylist) {
popupMenu.getMenu().findItem(R.id.action_clear_playlist).setVisible(false);
}
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(@NonNull MenuItem item) {
if (item.getItemId() == R.id.action_clear_playlist) {
if (playlist instanceof AbsSmartPlaylist) {
ClearSmartPlaylistDialog.create((AbsSmartPlaylist) playlist).show(activity.getSupportFragmentManager(), "CLEAR_SMART_PLAYLIST_" + playlist.name);
return true;
}
}
return PlaylistMenuHelper.handleMenuClick(
activity, dataSet.get(getAdapterPosition()), item);
}
});
popupMenu.show();
}
});
}
}
public static void setElevation(View view, float elevation){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
view.setElevation(elevation);
}
}