下面列出了android.view.View#setTranslationZ ( ) 实例代码,或者点击链接到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);
}
}
/**
* Helper function to disable a ViewGroup and all it's children. This draws a new view with z-ordering of integer max
* that consumes all touch events.
* @param viewGroup
*/
public static void disable(ViewGroup viewGroup){
View view = new View(viewGroup.getContext());
viewGroup.addView(view);
view.setTag(DISABLE);
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = MATCH_PARENT;
params.width = MATCH_PARENT;
view.setLayoutParams(params);
view.setClickable(true);
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.setBackgroundColor(Color.TRANSPARENT);
if (Build.VERSION.SDK_INT >= 21) {
view.setTranslationZ(Integer.MAX_VALUE);
}
viewGroup.bringChildToFront(view);
}
@Override
public void addView(View child) {
child.setAlpha(1f);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
child.setTranslationZ(0f);
}
ExpandedRecyclerView.ViewHolder holder = ExpandedRecyclerView.getChildViewHolderInt(child);
if (holder instanceof OmegaExpandableRecyclerView.Adapter.ChildViewHolder) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
child.setTranslationZ(DEFAULT_CHILD_Z);
}
int adapterPosition = holder.getAdapterPosition();
if (mAddedRange.contains(adapterPosition)) {
((OmegaExpandableRecyclerView.Adapter.ChildViewHolder) holder).contentView.setAlpha(0f);
}
}
super.addView(child);
}
@TargetApi(VERSION_CODES.LOLLIPOP)
private void createElevationAnimation(
View dependency,
@NonNull View child,
boolean expanded,
boolean currentlyAnimating,
@NonNull FabTransformationSpec spec,
@NonNull List<Animator> animations,
List<AnimatorListener> unusedListeners) {
float translationZ = ViewCompat.getElevation(child) - ViewCompat.getElevation(dependency);
Animator animator;
if (expanded) {
if (!currentlyAnimating) {
child.setTranslationZ(-translationZ);
}
animator = ObjectAnimator.ofFloat(child, View.TRANSLATION_Z, 0f);
} else {
animator = ObjectAnimator.ofFloat(child, View.TRANSLATION_Z, -translationZ);
}
MotionTiming timing = spec.timings.getTiming("elevation");
timing.apply(animator);
animations.add(animator);
}
private static void setTransforms(View view, float translationX, float translationY,
float translationZ, float scaleX, float scaleY, float rotationX,
float rotationY, float rotationZ) {
view.setTranslationX(translationX);
view.setTranslationY(translationY);
view.setTranslationZ(translationZ);
view.setScaleX(scaleX);
view.setScaleY(scaleY);
view.setRotationX(rotationX);
view.setRotationY(rotationY);
view.setRotation(rotationZ);
}
@Override
public void transformPage(View page, float position) {
float scale= 1 - (Math.abs(position) * 0.25f);
page.setScaleX(scale);
page.setScaleY(scale);
if (Math.abs(position) > 0.1) {
page.setTranslationZ(-Math.abs(position));
}else {
page.setTranslationZ(position);
}
page.setTranslationX(-50 * position);
}
private void restoreView(View v) {
v.setVisibility(endVisibility);
v.setPivotY(v.getHeight() / 2);
v.setPivotX(v.getWidth() / 2);
v.setRotation(0);
v.setRotationX(0);
v.setRotationY(0);
v.setTranslationX(0);
v.setTranslationY(0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
v.setTranslationZ(0);
}
}
public static void setTranslationZ(View view, float translationZ) {
view.setTranslationZ(translationZ);
}
/**
* Adds a touch interceptor to the layout when needed.
* The interceptor wil take care of touch events occurring
* on the content view when the drawer is open.
*/
private void addTouchInterceptor() {
View touchInterceptor = mLayoutInflater.inflate(R.layout.duo_overlay, this, false);
touchInterceptor.setTag(TAG_OVERLAY);
touchInterceptor.setOnTouchListener(new OnTouchListener() {
float startX;
float startY;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (mLockMode != LOCK_MODE_LOCKED_OPEN) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
break;
case MotionEvent.ACTION_UP:
float endX = event.getX();
float endY = event.getY();
if (touchIsClick(startX, endX, startY, endY)) {
closeDrawer();
}
break;
case MotionEvent.ACTION_MOVE:
mViewDragCallback.mIsEdgeDrag = true;
int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
mViewDragHelper.captureChildView(mContentView, pointerIndex);
break;
}
return true;
} else return true;
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
touchInterceptor.setTranslationZ(MAX_ATTRIBUTE_MULTIPLIER);
}
addView(touchInterceptor);
}
private void reorderItems() {
for (int x = 0; x < getChildCount(); x++) {
View childView = getChildAt(x);
int topViewIndex = getChildCount() - 1;
int distanceToViewAbove = (topViewIndex * mViewSpacing) - (x * mViewSpacing);
int newPositionX = (getWidth() - childView.getMeasuredWidth()) / 2;
int newPositionY = distanceToViewAbove + getPaddingTop();
childView.layout(
newPositionX,
getPaddingTop(),
newPositionX + childView.getMeasuredWidth(),
getPaddingTop() + childView.getMeasuredHeight());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
childView.setTranslationZ(x);
}
boolean isNewView = (boolean) childView.getTag(R.id.new_view);
float scaleFactor = (float) Math.pow(mScaleFactor, getChildCount() - x);
if (x == topViewIndex) {
mSwipeHelper.unregisterObservedView();
mTopView = childView;
mSwipeHelper.registerObservedView(mTopView, newPositionX, newPositionY);
}
if (!mIsFirstLayout) {
if (isNewView) {
childView.setTag(R.id.new_view, false);
childView.setAlpha(0);
childView.setY(newPositionY);
childView.setScaleY(scaleFactor);
childView.setScaleX(scaleFactor);
}
childView.animate()
.y(newPositionY)
.scaleX(scaleFactor)
.scaleY(scaleFactor)
.alpha(1)
.setDuration(mAnimationDuration);
} else {
childView.setTag(R.id.new_view, false);
childView.setY(newPositionY);
childView.setScaleY(scaleFactor);
childView.setScaleX(scaleFactor);
}
}
}
private void reorderItems() {
for (int x = 0; x < getChildCount(); x++) {
View childView = getChildAt(x);
int topViewIndex = getChildCount() - 1;
int distanceToViewAbove = (topViewIndex * mViewSpacing) - (x * mViewSpacing);
int newPositionX = (getWidth() - childView.getMeasuredWidth()) / 2;
int newPositionY = distanceToViewAbove + getPaddingTop();
childView.layout(
newPositionX,
getPaddingTop(),
newPositionX + childView.getMeasuredWidth(),
getPaddingTop() + childView.getMeasuredHeight());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
childView.setTranslationZ(x);
}
boolean isNewView = (boolean) childView.getTag(R.id.new_view);
float scaleFactor = (float) Math.pow(mScaleFactor, getChildCount() - x);
if (x == topViewIndex) {
mSwipeCardsHelper.unregisterObservedView();
mTopView = childView;
mSwipeCardsHelper.registerObservedView(mTopView, newPositionX, newPositionY);
}
if (!mIsFirstLayout) {
if (isNewView) {
childView.setTag(R.id.new_view, false);
childView.setAlpha(0);
childView.setY(newPositionY);
childView.setScaleY(scaleFactor);
childView.setScaleX(scaleFactor);
}
childView.animate()
.y(newPositionY)
.scaleX(scaleFactor)
.scaleY(scaleFactor)
.alpha(1)
.setDuration(mAnimationDuration);
} else {
childView.setTag(R.id.new_view, false);
childView.setY(newPositionY);
childView.setScaleY(scaleFactor);
childView.setScaleX(scaleFactor);
}
}
}
public static void setTranslationZ(View view, float translationZ) {
view.setTranslationZ(translationZ);
}
@Override
public void setValue(View view, float value) {
if (isZSupported()) {
view.setTranslationZ(value);
}
}
@Override
public void setTranslationZ(@NonNull View view, float z) {
view.setTranslationZ(z);
}