下面列出了android.view.View#getAlpha ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Determines if the supplied {@link View} is visible to the user, which requires that it be
* marked visible, that all its parents are visible, that it and all parents have alpha greater
* than 0, and that it has non-zero size. This code attempts to replicate the protected method
* {@code View.isVisibleToUser}.
*
* @param view The {@link View} to evaluate
* @return {@code true} if {@code view} is visible to the user
*/
@RequiresApi(Build.VERSION_CODES.HONEYCOMB) // Uses View#getAlpha
public static boolean isVisibleToUser(View view) {
if (view == null) {
return false;
}
Object current = view;
while (current instanceof View) {
View currentView = (View) current;
if ((currentView.getAlpha() <= 0) || (currentView.getVisibility() != View.VISIBLE)) {
return false;
}
current = currentView.getParent();
}
return view.getGlobalVisibleRect(new Rect());
}
/**
* View 自身是否可见
*
* @return View 宽、高、透明度 有一个 < 0 时,或 getLocalVisibleRect 为 false 时;返回 false 。
* View getVisibility 不可见,且有 Animation getFillAfter 为 false 时;返回 false 。
* View 无 Animation 时 getVisibility 不可见时返回 false 。
*/
@RequiresApi(api = 11)
public static boolean isViewSelfVisible(View view) {
if (view == null) {
return false;
}
if (view.getWidth() <= 0 || view.getHeight() <= 0 || view.getAlpha() <= 0.0f || !view.getLocalVisibleRect(new Rect())) {
return false;
}
return (view.getVisibility() != VISIBLE && view.getAnimation() != null && view.getAnimation().getFillAfter()) || view.getVisibility() == VISIBLE;
}
@NonNull
public static AnimatorInfo captureViewStatus(@NonNull View view) {
return new AnimatorInfo(view.getTranslationX(),
view.getTranslationY(),
view.getScaleX(),
view.getScaleY(),
view.getRotation(),
view.getRotationX(),
view.getRotationY(),
view.getAlpha());
}
/**
* 获取 View 透明度
* @param view View
* @return 透明度
*/
public static float getAlpha(final View view) {
if (view != null) {
return view.getAlpha();
}
return 1.0f;
}
/**
* This method gets the value of the named property from the View object.
*
* @param propertyConstant The property whose value should be returned
* @return float The value of the named property
*/
private float getValue(int propertyConstant) {
//final View.TransformationInfo info = mView.mTransformationInfo;
View v = mView.get();
if (v != null) {
switch (propertyConstant) {
case TRANSLATION_X:
//return info.mTranslationX;
return v.getTranslationX();
case TRANSLATION_Y:
//return info.mTranslationY;
return v.getTranslationY();
case ROTATION:
//return info.mRotation;
return v.getRotation();
case ROTATION_X:
//return info.mRotationX;
return v.getRotationX();
case ROTATION_Y:
//return info.mRotationY;
return v.getRotationY();
case SCALE_X:
//return info.mScaleX;
return v.getScaleX();
case SCALE_Y:
//return info.mScaleY;
return v.getScaleY();
case X:
//return v.mLeft + info.mTranslationX;
return v.getX();
case Y:
//return v.mTop + info.mTranslationY;
return v.getY();
case ALPHA:
//return info.mAlpha;
return v.getAlpha();
}
}
return 0;
}
public static void startAnimators(final View view, int startOffsetX, int startOffsetY, long delay) {
if (view.getVisibility() == View.VISIBLE && view.getAlpha() != 0f) {
view.clearAnimation();
view.animate().cancel();
final Resources res = view.getResources();
final float endAlpha = view.getAlpha();
final float endTranslateX = view.getTranslationX();
final float endTranslateY = view.getTranslationY();
view.setAlpha(0);
final Animator fade = ObjectAnimator.ofFloat(view, View.ALPHA, endAlpha);
fade.setDuration(res.getInteger(R.integer.material_in_fade_anim_duration));
fade.setInterpolator(new AccelerateInterpolator());
fade.setStartDelay(delay);
fade.start();
ViewPropertyAnimator slide = view.animate();
if (startOffsetY != 0) {
view.setTranslationY(startOffsetY);
slide.translationY(endTranslateY);
} else {
view.setTranslationX(startOffsetX);
slide.translationX(endTranslateX);
}
slide.setInterpolator(new DecelerateInterpolator(2));
slide.setDuration(res.getInteger(R.integer.material_in_slide_anim_duration));
slide.setStartDelay(delay);
slide.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationCancel(Animator animation) {
if (fade.isStarted()) {
fade.cancel();
}
view.setAlpha(endAlpha);
view.setTranslationX(endTranslateX);
view.setTranslationY(endTranslateY);
}
});
slide.start();
}
}
/**
* This method gets the value of the named property from the View object.
*
* @param propertyConstant The property whose value should be returned
* @return float The value of the named property
*/
private float getValue(int propertyConstant) {
//final View.TransformationInfo info = mView.mTransformationInfo;
View v = mView.get();
if (v != null) {
switch (propertyConstant) {
case TRANSLATION_X:
//return info.mTranslationX;
return v.getTranslationX();
case TRANSLATION_Y:
//return info.mTranslationY;
return v.getTranslationY();
case ROTATION:
//return info.mRotation;
return v.getRotation();
case ROTATION_X:
//return info.mRotationX;
return v.getRotationX();
case ROTATION_Y:
//return info.mRotationY;
return v.getRotationY();
case SCALE_X:
//return info.mScaleX;
return v.getScaleX();
case SCALE_Y:
//return info.mScaleY;
return v.getScaleY();
case X:
//return v.mLeft + info.mTranslationX;
return v.getX();
case Y:
//return v.mTop + info.mTranslationY;
return v.getY();
case ALPHA:
//return info.mAlpha;
return v.getAlpha();
}
}
return 0;
}
/** 初始化数据 */
private void prepare(CoordinatorLayout parent, View child, View dependency) {
mDependStartX = (int) dependency.getX();
mDependStartY = (int) dependency.getY();
mDependStartWidth = dependency.getWidth();
mDependStartHeight = dependency.getHeight();
mStartX = (int) child.getX();
mStartY = (int) child.getY();
mStartWidth = child.getWidth();
mStartHeight = child.getHeight();
mStartAlpha = child.getAlpha();
mStartRotateX = child.getRotationX();
mStartRotateY = child.getRotationY();
//特殊处理y方向变化
if (mDependTargetY == UNSPECIFIED_INT && dependency instanceof AppBarLayout) {
mDependTargetY = ((AppBarLayout) dependency).getTotalScrollRange();
}
// 背景颜色渐变
if (child.getBackground() instanceof ColorDrawable) mStartBackgroundColor = ((ColorDrawable) child.getBackground()).getColor();
// 自定义动画
if (mAnimationId != 0) {
mAnimation = AnimationUtils.loadAnimation(child.getContext(), mAnimationId);
mAnimation.initialize(child.getWidth(), child.getHeight(), parent.getWidth(), parent.getHeight());
}
// 兼容5.0以上的沉浸模式
if (Build.VERSION.SDK_INT > 16 && parent.getFitsSystemWindows() && targetY != UNSPECIFIED_INT) {
targetY += getStatusBarHeight(parent.getContext());
}
isPrepared = true;
}
public SubButton(View view, int width, int height) {
this.view = view;
this.width = width;
this.height = height;
this.alpha = view.getAlpha();
this.x = 0;
this.y = 0;
}
/**
* Draw the view into a bitmap.
*/
Bitmap getViewBitmap(View v) {
v.clearFocus();
v.setPressed(false);
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
// Reset the drawing cache background color to fully transparent
// for the duration of this operation
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
float alpha = v.getAlpha();
v.setAlpha(1.0f);
if (color != 0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setAlpha(alpha);
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
static float getAlpha(View view) {
return view.getAlpha();
}
@Override
public float getValue(View view) {
return view.getAlpha();
}
@Override
public boolean matchesSafely(View view) {
return view.getAlpha() == alpha;
}
static float getAlpha(View view) {
return view.getAlpha();
}
@Override
public float getAlpha(View view) {
return view.getAlpha();
}
public static float getAlpha(View view) {
return view.getAlpha();
}
private static void unsetAlpha(View view, NodeInfo nodeInfo) {
if (nodeInfo.isAlphaSet() && view.getAlpha() != 1) {
view.setAlpha(1);
}
}
static float getAlpha(View view) {
return view.getAlpha();
}
private void updateSections(RecyclerView listView, boolean checkTopBottom) {
int count = listView.getChildCount();
int minPositionDateHolder = Integer.MAX_VALUE;
View minDateChild = null;
float padding = listView.getPaddingTop() + actionBar.getTranslationY();
int minTop = Integer.MAX_VALUE;
int maxBottom = 0;
for (int a = 0; a < count; a++) {
View view = listView.getChildAt(a);
int bottom = view.getBottom();
minTop = Math.min(minTop, view.getTop());
maxBottom = Math.max(bottom, maxBottom);
if (bottom <= padding) {
continue;
}
int position = view.getBottom();
if (view instanceof SharedMediaSectionCell || view instanceof GraySectionCell) {
if (view.getAlpha() != 1.0f) {
view.setAlpha(1.0f);
}
if (position < minPositionDateHolder) {
minPositionDateHolder = position;
minDateChild = view;
}
}
}
if (minDateChild != null) {
if (minDateChild.getTop() > padding) {
if (minDateChild.getAlpha() != 1.0f) {
minDateChild.setAlpha(1.0f);
}
} else {
if (minDateChild.getAlpha() != 0.0f) {
minDateChild.setAlpha(0.0f);
}
}
}
if (checkTopBottom) {
if (maxBottom != 0 && maxBottom < (listView.getMeasuredHeight() - listView.getPaddingBottom())) {
resetScroll();
} else if (minTop != Integer.MAX_VALUE && minTop > listView.getPaddingTop() + actionBar.getTranslationY()) {
scrollWithoutActionBar(listView, -listView.computeVerticalScrollOffset());
resetScroll();
}
}
}
public float getTransitionAlpha(@NonNull View v) {
return v.getAlpha();
}