下面列出了android.view.Gravity#RIGHT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public KlyphDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
final float density = getResources().getDisplayMetrics().density;
mMinDrawerMargin = (int) (MIN_DRAWER_MARGIN * density + 0.5f);
final float minVel = MIN_FLING_VELOCITY * density;
mLeftCallback = new ViewDragCallback(Gravity.LEFT);
mRightCallback = new ViewDragCallback(Gravity.RIGHT);
mLeftDragger = ViewDragHelper.create(this, 0.5f, mLeftCallback);
mLeftDragger.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
mLeftDragger.setMinVelocity(minVel);
mLeftCallback.setDragger(mLeftDragger);
mRightDragger = ViewDragHelper.create(this, 0.5f, mRightCallback);
mRightDragger.setEdgeTrackingEnabled(ViewDragHelper.EDGE_RIGHT);
mRightDragger.setMinVelocity(minVel);
mRightCallback.setDragger(mRightDragger);
// So that we can catch the back button
setFocusableInTouchMode(true);
ViewCompat.setAccessibilityDelegate(this, new AccessibilityDelegate());
ViewGroupCompat.setMotionEventSplittingEnabled(this, false);
}
/**
* @return True, if adjustments were made that require the view to be invalidated.
*/
private boolean adjustBottomLines() {
// Bail out if we have a zero width; lines will be adjusted during next layout.
if (getWidth() == 0) {
return false;
}
int destBottomLines;
textPaint.setTextSize(bottomTextSize);
if (tempErrorText != null || helperText != null) {
Layout.Alignment alignment = (getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL() ?
Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.LEFT) == Gravity.LEFT ?
Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER;
textLayout = new StaticLayout(tempErrorText != null ? tempErrorText : helperText, textPaint, getWidth() - getBottomTextLeftOffset() - getBottomTextRightOffset() - getPaddingLeft() - getPaddingRight(), alignment, 1.0f, 0.0f, true);
destBottomLines = Math.max(textLayout.getLineCount(), minBottomTextLines);
} else {
destBottomLines = minBottomLines;
}
if (bottomLines != destBottomLines) {
getBottomLinesAnimator(destBottomLines).start();
}
bottomLines = destBottomLines;
return true;
}
private void measuredOutContentStart(String content) {
Rect rect = new Rect();
paintOuterText.getTextBounds(content, 0, content.length(), rect);
switch (mGravity) {
case Gravity.CENTER:
if (isOptions || label == null || label.equals("") || !isCenterLabel) {
drawOutContentStart = (int) ((measuredWidth - rect.width()) * 0.5);
} else {//只显示中间label时,时间选择器内容偏左一点,留出空间绘制单位标签
drawOutContentStart = (int) ((measuredWidth - rect.width()) * 0.25);
}
break;
case Gravity.LEFT:
drawOutContentStart = 0;
break;
case Gravity.RIGHT:
drawOutContentStart = measuredWidth - rect.width() - (int) CENTER_CONTENT_OFFSET;
break;
}
}
/**
* @return True, if adjustments were made that require the view to be invalidated.
*/
private boolean adjustBottomLines() {
// Bail out if we have a zero width; lines will be adjusted during next layout.
if (getWidth() == 0) {
return false;
}
int destBottomLines;
textPaint.setTextSize(bottomTextSize);
if (tempErrorText != null || helperText != null) {
Layout.Alignment alignment = (getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL() ?
Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.LEFT) == Gravity.LEFT ?
Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER;
textLayout = new StaticLayout(tempErrorText != null ? tempErrorText : helperText, textPaint, getWidth() - getBottomTextLeftOffset() - getBottomTextRightOffset() - getPaddingLeft() - getPaddingRight(), alignment, 1.0f, 0.0f, true);
destBottomLines = Math.max(textLayout.getLineCount(), minBottomTextLines);
} else {
destBottomLines = minBottomLines;
}
if (bottomLines != destBottomLines) {
getBottomLinesAnimator(destBottomLines).start();
}
bottomLines = destBottomLines;
return true;
}
/**
* Set a simple drawable used for the left or right shadow.
* The drawable provided must have a nonzero intrinsic width.
*
* @param shadowDrawable Shadow drawable to use at the edge of a drawer
* @param gravity Which drawer the shadow should apply to
*/
public void setDrawerShadow(Drawable shadowDrawable, @EdgeGravity int gravity) {
/*
* TODO Someone someday might want to set more complex drawables here.
* They're probably nuts, but we might want to consider registering callbacks,
* setting states, etc. properly.
*/
final int absGravity = GravityCompat.getAbsoluteGravity(gravity,
ViewCompat.getLayoutDirection(this));
if ((absGravity & Gravity.LEFT) == Gravity.LEFT) {
mShadowLeft = shadowDrawable;
invalidate();
}
if ((absGravity & Gravity.RIGHT) == Gravity.RIGHT) {
mShadowRight = shadowDrawable;
invalidate();
}
}
public MarginDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
final float density = getResources().getDisplayMetrics().density;
mMinDrawerMargin = (int) (MIN_DRAWER_MARGIN * density + 0.5f);
final float minVel = MIN_FLING_VELOCITY * density;
mLeftCallback = new ViewDragCallback(Gravity.LEFT);
mRightCallback = new ViewDragCallback(Gravity.RIGHT);
mLeftDragger = ViewDragHelper.create(this, TOUCH_SLOP_SENSITIVITY, mLeftCallback);
mLeftDragger.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
mLeftDragger.setMinVelocity(minVel);
mLeftCallback.setDragger(mLeftDragger);
mRightDragger = ViewDragHelper.create(this, TOUCH_SLOP_SENSITIVITY, mRightCallback);
mRightDragger.setEdgeTrackingEnabled(ViewDragHelper.EDGE_RIGHT);
mRightDragger.setMinVelocity(minVel);
mRightCallback.setDragger(mRightDragger);
// So that we can catch the back button
setFocusableInTouchMode(true);
ViewCompat.setAccessibilityDelegate(this, new AccessibilityDelegate());
ViewGroupCompat.setMotionEventSplittingEnabled(this, false);
}
int getGravity() {
switch (this) {
case TOP_LEFT:
return Gravity.TOP | Gravity.LEFT;
case TOP_CENTER:
return Gravity.TOP | Gravity.CENTER_HORIZONTAL;
case TOP_RIGHT:
return Gravity.TOP | Gravity.RIGHT;
case CENTER_LEFT:
return Gravity.LEFT | Gravity.CENTER_VERTICAL;
case CENTER:
return Gravity.CENTER;
case CENTER_RIGHT:
return Gravity.RIGHT | Gravity.CENTER_VERTICAL;
case BOTTOM_LEFT:
return Gravity.BOTTOM | Gravity.LEFT;
case BOTTOM_CENTER:
return Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
case BOTTOM_RIGHT:
return Gravity.BOTTOM | Gravity.RIGHT;
}
return Gravity.CENTER;
}
private static NavigationBarPosition gravityToNavigationBarPosition(int gravity) {
switch (gravity) {
case Gravity.BOTTOM:
return NavigationBarPosition.BOTTOM;
case Gravity.LEFT:
return NavigationBarPosition.LEFT;
case Gravity.RIGHT:
return NavigationBarPosition.RIGHT;
default:
return NavigationBarPosition.UNKNOWN;
}
}
@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
int gravity = getSideMenuGravity(drawerView);
if (gravity == Gravity.LEFT) {
dispatchSideMenuVisibilityEvents(left, prevLeftSlideOffset, slideOffset);
prevLeftSlideOffset = slideOffset;
} else if (gravity == Gravity.RIGHT) {
dispatchSideMenuVisibilityEvents(right, prevRightSlideOffset, slideOffset);
prevRightSlideOffset = slideOffset;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRootView = findViewById(R.id.layout_root);
Button toogleButton = new Button(this);
toogleButton.setText("切换");
toogleButton.setOnClickListener(this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
addContentView(toogleButton, params);
}
private void applyLayoutParams() {
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
switch (badgePosition) {
case POSITION_TOP_LEFT:
lp.gravity = Gravity.LEFT | Gravity.TOP;
lp.setMargins(badgeMarginH, badgeMarginV, 0, 0);
break;
case POSITION_TOP_RIGHT:
lp.gravity = Gravity.RIGHT | Gravity.TOP;
lp.setMargins(0, badgeMarginV, badgeMarginH, 0);
break;
case POSITION_BOTTOM_LEFT:
lp.gravity = Gravity.LEFT | Gravity.BOTTOM;
lp.setMargins(badgeMarginH, 0, 0, badgeMarginV);
break;
case POSITION_BOTTOM_RIGHT:
lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;
lp.setMargins(0, 0, badgeMarginH, badgeMarginV);
break;
case POSITION_CENTER:
lp.gravity = Gravity.CENTER;
lp.setMargins(0, 0, 0, 0);
break;
default:
break;
}
setLayoutParams(lp);
}
public Builder(Activity activity) {
this.activity = activity;
// Default FloatingActionButton settings
int size = activity.getResources().getDimensionPixelSize(R.dimen.action_button_size);
int margin = activity.getResources().getDimensionPixelSize(R.dimen.action_button_margin);
LayoutParams layoutParams = new LayoutParams(size, size, Gravity.BOTTOM | Gravity.RIGHT);
layoutParams.setMargins(margin, margin, margin, margin);
setLayoutParams(layoutParams);
setTheme(FloatingActionButton.THEME_LIGHT);
setPosition(FloatingActionButton.POSITION_BOTTOM_RIGHT);
}
private int getDotGravity() {
switch (mDotLocation){
case 0:
return Gravity.CENTER;
case 1:
return Gravity.RIGHT;
case -1:
return Gravity.LEFT;
}
return -1;
}
/**
* This method will be invoked when the current page is scrolled, either as part
* of a programmatically initiated smooth scroll or a user initiated touch scroll.
* If you override this method you must call through to the superclass implementation
* (e.g. super.onPageScrolled(position, offset, offsetPixels)) before onPageScrolled
* returns.
*
* @param position Position index of the first page currently being displayed.
* Page position+1 will be visible if positionOffset is nonzero.
* @param offset Value from [0, 1) indicating the offset from the page at position.
* @param offsetPixels Value in pixels indicating the offset from position.
*/
protected void onPageScrolled(int position, float offset, int offsetPixels) {
// Offset any decor views if needed - keep them on-screen at all times.
if (mDecorChildCount > 0) {
final int scrollX = getScrollX();
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
final int width = getWidth();
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (!lp.isDecor) continue;
final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
int childLeft = 0;
switch (hgrav) {
default:
childLeft = paddingLeft;
break;
case Gravity.LEFT:
childLeft = paddingLeft;
paddingLeft += child.getWidth();
break;
case Gravity.CENTER_HORIZONTAL:
childLeft = Math.max((width - child.getMeasuredWidth()) / 2,
paddingLeft);
break;
case Gravity.RIGHT:
childLeft = width - paddingRight - child.getMeasuredWidth();
paddingRight += child.getMeasuredWidth();
break;
}
childLeft += scrollX;
final int childOffset = childLeft - child.getLeft();
if (childOffset != 0) {
child.offsetLeftAndRight(childOffset);
}
}
}
if (mOnPageChangeListener != null) {
mOnPageChangeListener.onPageScrolled(position, offset, offsetPixels);
}
if (mInternalPageChangeListener != null) {
mInternalPageChangeListener.onPageScrolled(position, offset, offsetPixels);
}
mCalledSuper = true;
}
/**
* 创建
*/
@SuppressLint("RtlHardcoded")
public BasePopupWindow create() {
// 判断布局是否为空
if (mContentView == null) {
throw new IllegalArgumentException("are you ok?");
}
// 如果当前没有设置重心,就设置一个默认的重心
if (mGravity == DEFAULT_ANCHORED_GRAVITY) {
mGravity = Gravity.CENTER;
}
// 如果当前没有设置动画效果,就设置一个默认的动画效果
if (mAnimations == AnimAction.NO_ANIM) {
switch (mGravity) {
case Gravity.TOP:
mAnimations = AnimAction.TOP;
break;
case Gravity.BOTTOM:
mAnimations = AnimAction.BOTTOM;
break;
case Gravity.LEFT:
mAnimations = AnimAction.LEFT;
break;
case Gravity.RIGHT:
mAnimations = AnimAction.RIGHT;
break;
default:
mAnimations = AnimAction.DEFAULT;
break;
}
}
mPopupWindow = createPopupWindow(mContext);
mPopupWindow.setContentView(mContentView);
mPopupWindow.setWidth(mWidth);
mPopupWindow.setHeight(mHeight);
mPopupWindow.setAnimationStyle(mAnimations);
mPopupWindow.setTouchable(mTouchable);
mPopupWindow.setFocusable(mFocusable);
mPopupWindow.setOutsideTouchable(mOutsideTouchable);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mPopupWindow.setBackgroundDimAmount(mBackgroundDimAmount);
if (mOnShowListeners != null) {
mPopupWindow.setOnShowListeners(mOnShowListeners);
}
if (mOnDismissListeners != null) {
mPopupWindow.setOnDismissListeners(mOnDismissListeners);
}
for (int i = 0; mClickArray != null && i < mClickArray.size(); i++) {
mContentView.findViewById(mClickArray.keyAt(i)).setOnClickListener(new BasePopupWindow.ViewClickWrapper(mPopupWindow, mClickArray.valueAt(i)));
}
return mPopupWindow;
}
@OnClick({R.id.btn_color, R.id.btn_left, R.id.btn_top, R.id.btn_triangle,
R.id.btn_text1_minus, R.id.btn_text1_plus, R.id.btn_height1_minus, R.id.btn_height1_plus,
R.id.btn_text2_minus, R.id.btn_text2_plus, R.id.btn_height2_minus, R.id.btn_height2_plus})
@Override
public void onClick(View v) {
super.onClick(v);
switch (v.getId()) {
case R.id.btn_color:
labelView.setFillColor(0xff000000 | RandomUtils.getRandom(0, 0xffffff));
break;
case R.id.btn_left:
if (mIsLeft) {
labelView.right();
} else {
labelView.left();
}
mIsLeft = !mIsLeft;
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) labelView.getLayoutParams();
lp.gravity = (mIsLeft ? Gravity.LEFT : Gravity.RIGHT) | (mIsTop ? Gravity.TOP : Gravity.BOTTOM);
labelView.setLayoutParams(lp);
break;
case R.id.btn_top:
if (mIsTop) {
labelView.bottom();
} else {
labelView.top();
}
mIsTop = !mIsTop;
FrameLayout.LayoutParams lp2 = (FrameLayout.LayoutParams) labelView.getLayoutParams();
lp2.gravity = (mIsLeft ? Gravity.LEFT : Gravity.RIGHT) | (mIsTop ? Gravity.TOP : Gravity.BOTTOM);
labelView.setLayoutParams(lp2);
break;
case R.id.btn_triangle:
mIsTriangle = !mIsTriangle;
labelView.triangle(mIsTriangle);
break;
case R.id.btn_text1_minus:
mText1Index = (mText1Index - 1 + TEXTS.length) % TEXTS.length;
labelView.setText1(TEXTS[mText1Index]);
break;
case R.id.btn_text1_plus:
mText1Index = (mText1Index + 1) % TEXTS.length;
labelView.setText1(TEXTS[mText1Index]);
break;
case R.id.btn_height1_minus:
if (mText1Height < 8) break;
mText1Height -= 2;
convertPx = SizeUtils.spConvertPx(mText1Height);
labelView.setTextHeight1(convertPx);
labelView.setPaddingTop(convertPx);
labelView.setPaddingCenter(convertPx / 3);
labelView.setPaddingBottom(convertPx / 3);
break;
case R.id.btn_height1_plus:
if (mText1Height > 30) break;
mText1Height += 2;
convertPx = SizeUtils.spConvertPx(mText1Height);
labelView.setTextHeight1(convertPx);
labelView.setPaddingTop(convertPx);
labelView.setPaddingCenter(convertPx / 3);
labelView.setPaddingBottom(convertPx / 3);
break;
case R.id.btn_text2_minus:
mText2Index = (mText2Index + 5 - 1) % 5;
labelView.setText2("1234567890".substring(0, mText2Index));
break;
case R.id.btn_text2_plus:
mText2Index = (mText2Index + 5 + 1) % 5;
labelView.setText2("1234567890".substring(0, mText2Index));
break;
case R.id.btn_height2_minus:
if (mText2Height < 4) break;
mText2Height -= 2;
convertPx = SizeUtils.spConvertPx(mText2Height);
labelView.setTextHeight2(convertPx);
break;
case R.id.btn_height2_plus:
if (mText2Height > 20) break;
mText2Height += 2;
convertPx = SizeUtils.spConvertPx(mText2Height);
labelView.setTextHeight2(convertPx);
break;
}
}
@Override
public void onParseValueFinished() {
super.onParseValueFinished();
mNative.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
mNative.setBorderColor(mBorderColor);
mNative.setBorderWidth(mBorderWidth);
mNative.setBorderTopLeftRadius(mBorderTopLeftRadius);
mNative.setBorderTopRightRadius(mBorderTopRightRadius);
mNative.setBorderBottomLeftRadius(mBorderBottomLeftRadius);
mNative.setBorderBottomRightRadius(mBorderBottomRightRadius);
mNative.setBackgroundColor(mBackground);
mNative.setTextColor(mTextColor);
int flag = Paint.ANTI_ALIAS_FLAG;
if (0 != (mTextStyle & TextBaseCommon.BOLD)) {
flag |= Paint.FAKE_BOLD_TEXT_FLAG;
}
if (0 != (mTextStyle & TextBaseCommon.STRIKE)) {
flag |= Paint.STRIKE_THRU_TEXT_FLAG;
}
if (0 != (mTextStyle & TextBaseCommon.UNDERLINE)) {
flag |= Paint.UNDERLINE_TEXT_FLAG;
}
mNative.setPaintFlags(flag);
if (0 != (mTextStyle & TextBaseCommon.ITALIC)) {
mNative.setTypeface(null, Typeface.BOLD_ITALIC);
}
if (mLines > 0) {
mNative.setLines(mLines);
}
if (mEllipsize >= 0) {
mNative.setEllipsize(TextUtils.TruncateAt.values()[mEllipsize]);
}
int gravity = 0;
if (0 != (mGravity & ViewBaseCommon.LEFT)) {
gravity |= Gravity.LEFT;
} else if (0 != (mGravity & ViewBaseCommon.RIGHT)) {
gravity |= Gravity.RIGHT;
} else if (0 != (mGravity & ViewBaseCommon.H_CENTER)) {
gravity |= Gravity.CENTER_HORIZONTAL;
}
if (0 != (mGravity & ViewBaseCommon.TOP)) {
gravity |= Gravity.TOP;
} else if (0 != (mGravity & ViewBaseCommon.BOTTOM)) {
gravity |= Gravity.BOTTOM;
} else if (0 != (mGravity & ViewBaseCommon.V_CENTER)) {
gravity |= Gravity.CENTER_VERTICAL;
}
mNative.setGravity(gravity);
mNative.setLineSpacing(mLineSpaceExtra, mLineSpaceMultipiler);
if (!TextUtils.isEmpty(mText)) {
setRealText(mText);
} else {
setRealText("");
}
}
@Override
public void onCreate ()
{
super.onCreate ();
this.wm = (WindowManager) this.getSystemService (WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) this.getSystemService (Service.LAYOUT_INFLATER_SERVICE);
this.layout = (LinearLayout) inflater.inflate (R.layout.service_launcher, null, false);
this.llLauncher = (LinearLayout) this.layout.findViewById (R.id.llLauncher);
this.llListenerContainer = (LinearLayout) this.layout.findViewById (R.id.llListenerContainer);
this.llListener = (LinearLayout) this.layout.findViewById (R.id.llListener);
this.llShadow = (LinearLayout) this.layout.findViewById (R.id.llShadow);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 0;
be.robinj.distrohopper.desktop.launcher.AppLauncher lalBfb = (be.robinj.distrohopper.desktop.launcher.AppLauncher) this.layout.findViewById (R.id.lalBfb);
lalBfb.setColour (R.color.transparentblack80);
lalBfb.init ();
SharedPreferences prefs = Preferences.getSharedPreferences(this, Preferences.PREFERENCES);
boolean right = prefs.getInt(Preference.LAUNCHER_EDGE.getName(), Location.LEFT.n) == Location.RIGHT.n;
this.touchListener = new TouchListener (this, right);
if (right)
{
params.gravity = Gravity.TOP | Gravity.RIGHT;
this.layout.removeView (this.llLauncher);
this.layout.removeView (this.llListenerContainer);
this.layout.removeView (this.llShadow);
this.llShadow.setBackgroundResource (R.drawable.launcherservice_shadow_right);
this.layout.addView (this.llShadow);
this.layout.addView (this.llListenerContainer);
this.layout.addView (this.llLauncher);
}
lalBfb.setOnTouchListener (this.touchListener);
this.llListener.setOnTouchListener (this.touchListener);
this.llShadow.setOnTouchListener (this.touchListener);
/*
LayoutTransition layout_transition = new LayoutTransition ();
layout_transition.setStartDelay (LayoutTransition.APPEARING, 0);
layout_transition.setStartDelay (LayoutTransition.DISAPPEARING, 0);
this.layout.setLayoutTransition (layout_transition);
*/
this.wm.addView (this.layout, params);
}
protected void layoutOtherView(View child, int parentRight, int parentBottom) {
final int width = child.getMeasuredWidth();
final int height = child.getMeasuredHeight();
int childLeft, childTop;
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final int gravity = lp.gravity;
final int layoutDirection = ViewCompat.getLayoutDirection(this);
final int absoluteGravity = GravityCompat.getAbsoluteGravity(gravity, layoutDirection);
final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.CENTER_HORIZONTAL:
childLeft =
(int)
(getPaddingLeft()
+ (parentRight - getPaddingLeft() - width) / 2f
+ lp.leftMargin
- lp.rightMargin);
break;
case Gravity.RIGHT:
childLeft = parentRight - width - lp.rightMargin;
break;
default:
childLeft = getPaddingLeft() + lp.leftMargin;
}
switch (verticalGravity) {
case Gravity.CENTER_VERTICAL:
childTop =
(int)
(getPaddingTop()
+ (parentBottom - getPaddingTop() - height) / 2f
+ lp.topMargin
- lp.bottomMargin);
break;
case Gravity.BOTTOM:
childTop = parentBottom - height - lp.bottomMargin;
break;
default:
childTop = getPaddingTop() + lp.topMargin;
}
child.layout(childLeft, childTop, childLeft + width, childTop + height);
if (sDebug) {
Log.d(
TAG,
String.format(
"onLayout(): child: %d %d %d %d",
childLeft, childTop, childLeft + width, childTop + height));
}
}
/**
* Creates an approximated cubic gradient using a multi-stop linear gradient. See
* <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
* details.
*/
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {
// Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
int cacheKeyHash = baseColor;
cacheKeyHash = 31 * cacheKeyHash + numStops;
cacheKeyHash = 31 * cacheKeyHash + gravity;
Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
if (cachedGradient != null) {
return cachedGradient;
}
numStops = Math.max(numStops, 2);
PaintDrawable paintDrawable = new PaintDrawable();
paintDrawable.setShape(new RectShape());
final int[] stopColors = new int[numStops];
int red = Color.red(baseColor);
int green = Color.green(baseColor);
int blue = Color.blue(baseColor);
int alpha = Color.alpha(baseColor);
for (int i = 0; i < numStops; i++) {
float x = i * 1f / (numStops - 1);
/* float opacity = MathUtil.constrain(0, 1, (float) Math.pow(x, 3));
stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);*/
}
final float x0, x1, y0, y1;
switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.LEFT: x0 = 1; x1 = 0; break;
case Gravity.RIGHT: x0 = 0; x1 = 1; break;
default: x0 = 0; x1 = 0; break;
}
switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
case Gravity.TOP: y0 = 1; y1 = 0; break;
case Gravity.BOTTOM: y0 = 0; y1 = 1; break;
default: y0 = 0; y1 = 0; break;
}
paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
LinearGradient linearGradient = new LinearGradient(
width * x0,
height * y0,
width * x1,
height * y1,
stopColors, null,
Shader.TileMode.CLAMP);
return linearGradient;
}
});
cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
return paintDrawable;
}