下面列出了android.view.View#getPaddingLeft ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void drawPadding(View view, Canvas canvas) {
int width = view.getWidth();
int height = view.getHeight();
int lPad = view.getPaddingLeft();
int tPad = view.getPaddingTop();
int rPad = view.getPaddingRight();
int bPad = view.getPaddingBottom();
Rect lRect = new Rect(0, 0, lPad, height);
Rect tRect = new Rect(lPad, 0, width - rPad, tPad);
Rect rRect = new Rect(width - rPad, 0, width, height);
Rect bRect = new Rect(lPad, height - bPad, width - rPad, height);
Paint paint = new Paint();
paint.setColor(PADDING_COLOR);
canvas.drawRect(lRect, paint);
canvas.drawRect(tRect, paint);
canvas.drawRect(rRect, paint);
canvas.drawRect(bRect, paint);
}
public void showAnimation(ShineButton shineButton) {
btnWidth = shineButton.getWidth();
btnHeight = shineButton.getHeight();
thirdLength = getThirdLength(btnHeight, btnWidth);
int[] location = new int[2];
shineButton.getLocationInWindow(location);
centerAnimX = location[0] + shineButton.getWidth() / 2;
centerAnimY = location[1] + shineButton.getHeight() / 2;
if ( shineButton.mFixDialog != null && shineButton.mFixDialog.getWindow() != null ) {
View decor = shineButton.mFixDialog.getWindow().getDecorView();
centerAnimX = centerAnimX - decor.getPaddingLeft();
centerAnimY = centerAnimY - decor.getPaddingTop();
}
shineAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
value = (float) valueAnimator.getAnimatedValue();
if (shineSize != 0 && shineSize > 0) {
paint.setStrokeWidth((shineSize) * (shineDistanceMultiple - value));
paintSmall.setStrokeWidth(((float) shineSize / 3 * 2) * (shineDistanceMultiple - value));
} else {
paint.setStrokeWidth((btnWidth / 2) * (shineDistanceMultiple - value));
paintSmall.setStrokeWidth((btnWidth / 3) * (shineDistanceMultiple - value));
}
rectF.set(centerAnimX - (btnWidth / (3 - shineDistanceMultiple) * value), centerAnimY - (btnHeight / (3 - shineDistanceMultiple) * value), centerAnimX + (btnWidth / (3 - shineDistanceMultiple) * value), centerAnimY + (btnHeight / (3 - shineDistanceMultiple) * value));
rectFSmall.set(centerAnimX - (btnWidth / ((3 - shineDistanceMultiple) + distanceOffset) * value), centerAnimY - (btnHeight / ((3 - shineDistanceMultiple) + distanceOffset) * value), centerAnimX + (btnWidth / ((3 - shineDistanceMultiple) + distanceOffset) * value), centerAnimY + (btnHeight / ((3 - shineDistanceMultiple) + distanceOffset) * value));
invalidate();
}
});
shineAnimator.startAnim();
clickAnimator.start();
}
public static void resetViewBottomPadding(@Nullable View view, boolean scroll) {
if (view == null) return;
Context context = ContextHelper.getBaseContext(view);
int orientation = context.getResources().getConfiguration().orientation;
int left = view.getPaddingLeft();
int right = view.getPaddingRight();
int bottom = view.getPaddingTop();
int top = view.getPaddingTop();
int navBar = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
boolean tabletMode = context.getResources().getBoolean(R.bool.android_helpers_tablet_mode);
if (tabletMode || orientation == Configuration.ORIENTATION_PORTRAIT) {
navBar = getNavigationBarHeight(context);
}
if (!scroll) {
navBar += getStatusBarHeight(context);
}
}
if (!scroll) {
navBar += getToolbarHeight(context);
}
view.setPadding(left, top, right, (bottom + navBar));
}
public void setGeometry(final View previewTextView) {
final int previewWidth = previewTextView.getMeasuredWidth();
final int previewHeight = mPreviewHeight;
// The width and height of visible part of the key preview background. The content marker
// of the background 9-patch have to cover the visible part of the background.
mVisibleWidth = previewWidth - previewTextView.getPaddingLeft()
- previewTextView.getPaddingRight();
mVisibleHeight = previewHeight - previewTextView.getPaddingTop()
- previewTextView.getPaddingBottom();
// The distance between the top edge of the parent key and the bottom of the visible part
// of the key preview background.
setVisibleOffset(mPreviewOffset - previewTextView.getPaddingBottom());
}
@Override
public void prepare(View target) {
float x = target.getPaddingLeft();
float y = target.getHeight() - target.getPaddingBottom();
getAnimatorAgent().playTogether(
ObjectAnimator.ofFloat(target, "rotation", 90, 0),
ObjectAnimator.ofFloat(target, "alpha", 0, 1),
ObjectAnimator.ofFloat(target, "pivotX", x, x),
ObjectAnimator.ofFloat(target, "pivotY", y, y)
);
}
/**
* @see android.view.View#getPaddingStart()
*/
public static int getPaddingStart(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return view.getPaddingStart();
} else {
// Before JB MR1, all layouts are left-to-right, so start == left.
return view.getPaddingLeft();
}
}
@Override
public void prepare(View target) {
float x = target.getPaddingLeft();
float y = target.getHeight() - target.getPaddingBottom();
getAnimatorAgent().playTogether(
ObjectAnimator.ofFloat(target, "alpha", 1, 0),
ObjectAnimator.ofFloat(target,"rotation",0,-90),
ObjectAnimator.ofFloat(target,"pivotX",x,x),
ObjectAnimator.ofFloat(target,"pivotY",y,y)
);
}
public void setGeometry(final View previewTextView) {
final int previewWidth = previewTextView.getMeasuredWidth();
final int previewHeight = mPreviewHeight;
// The width and height of visible part of the key preview background. The content marker
// of the background 9-patch have to cover the visible part of the background.
mVisibleWidth = previewWidth - previewTextView.getPaddingLeft()
- previewTextView.getPaddingRight();
mVisibleHeight = previewHeight - previewTextView.getPaddingTop()
- previewTextView.getPaddingBottom();
// The distance between the top edge of the parent key and the bottom of the visible part
// of the key preview background.
setVisibleOffset(mPreviewOffset - previewTextView.getPaddingBottom());
}
public static void setBackgroundKeepPadding(@NonNull View view, @Nullable Drawable drawable) {
final Rect padding = new Rect(
view.getPaddingLeft(), view.getPaddingTop(),
view.getPaddingRight(), view.getPaddingBottom());
setBackground(view, drawable);
view.setPadding(padding.left, padding.top, padding.right, padding.bottom);
}
@Override
public void prepare(View target) {
float x = (target.getWidth() - target.getPaddingLeft() - target.getPaddingRight()) / 2
+ target.getPaddingLeft();
float y = target.getHeight() - target.getPaddingBottom();
getAnimatorAgent().playTogether(
ObjectAnimator.ofFloat(target, "rotation", 12, -12, 3, -3, 0),
ObjectAnimator.ofFloat(target, "pivotX", x, x, x, x, x),
ObjectAnimator.ofFloat(target, "pivotY", y, y, y, y, y)
);
}
@Override
protected void execute(View view, int val)
{
int l = view.getPaddingLeft();
int t = view.getPaddingTop();
int r = view.getPaddingRight();
int b = val;
view.setPadding(l, t, r, b);
}
@Override
public boolean dispatchGenericMotionEvent(MotionEvent ev) {
if (isLocked()) {
View content = getChildAt(0);
Rect rect = new Rect();
content.getHitRect(rect);
rect.left += content.getPaddingLeft();
rect.right -= content.getPaddingRight();
if (rect.contains((int) ev.getX(), (int) ev.getY()))
return content.dispatchGenericMotionEvent(ev);
}
return super.dispatchGenericMotionEvent(ev);
}
@Override
public void prepare(View target) {
float x = target.getPaddingLeft();
float y = target.getPaddingTop();
getAnimatorAgent().playTogether(
Glider.glide(Skill.SineEaseInOut, 1300, ObjectAnimator.ofFloat(target, "rotation", 0, 80, 60, 80, 60, 60)),
ObjectAnimator.ofFloat(target, "translationY", 0, 0, 0, 0, 0, 700),
ObjectAnimator.ofFloat(target, "alpha", 1, 1, 1, 1, 1, 0),
ObjectAnimator.ofFloat(target, "pivotX", x, x, x, x, x, x),
ObjectAnimator.ofFloat(target, "pivotY", y, y, y, y, y, y)
);
setDuration(1300);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
if (mDrawDivider) {
int left = view.getPaddingLeft();
int right = view.getPaddingRight();
int top = view.getPaddingTop();
int bottom = view.getPaddingBottom();
view.setBackground(DividerDrawable.create(getContext()));
view.setPadding(left, top, right, bottom);
}
SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.switch_widget);
// On BLU Life Play devices SwitchPreference.setWidgetLayoutResource() does nothing. As a
// result, the user will see a non-material Switch and switchView will be null, hence the
// null check below. http://crbug.com/451447
if (switchView != null) {
switchView.setChecked(isChecked());
}
TextView title = (TextView) view.findViewById(android.R.id.title);
title.setSingleLine(false);
if (!mDontUseSummaryAsTitle && TextUtils.isEmpty(getTitle())) {
TextView summary = (TextView) view.findViewById(android.R.id.summary);
title.setText(summary.getText());
title.setVisibility(View.VISIBLE);
summary.setVisibility(View.GONE);
}
if (mManagedPrefDelegate != null) mManagedPrefDelegate.onBindViewToPreference(this, view);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int childCount = getChildCount();
final int parentLeft = getPaddingLeft();
final int parentTop = getPaddingTop();
final int parentRight = r - l - getPaddingRight();
final int parentBottom = b - t - getPaddingBottom();
final int parentWidth = parentRight - parentLeft;
final int parentHeight = parentBottom - parentTop;
int unpaddedWidth, unpaddedHeight, parentUnpaddedWidth, parentUnpaddedHeight;
int childPaddingLeft, childPaddingTop, childPaddingRight, childPaddingBottom;
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
// Fit and center the child within the parent. Make sure not to consider padding
// as part of the child's aspect ratio.
childPaddingLeft = child.getPaddingLeft();
childPaddingTop = child.getPaddingTop();
childPaddingRight = child.getPaddingRight();
childPaddingBottom = child.getPaddingBottom();
unpaddedWidth = child.getMeasuredWidth() - childPaddingLeft - childPaddingRight;
unpaddedHeight = child.getMeasuredHeight() - childPaddingTop - childPaddingBottom;
parentUnpaddedWidth = parentWidth - childPaddingLeft - childPaddingRight;
parentUnpaddedHeight = parentHeight - childPaddingTop - childPaddingBottom;
if (parentUnpaddedWidth * unpaddedHeight > parentUnpaddedHeight * unpaddedWidth) {
// The child view should be left/right letterboxed.
final int scaledChildWidth = unpaddedWidth * parentUnpaddedHeight
/ unpaddedHeight + childPaddingLeft + childPaddingRight;
child.layout(
parentLeft + (parentWidth - scaledChildWidth) / 2,
parentTop,
parentRight - (parentWidth - scaledChildWidth) / 2,
parentBottom);
} else {
// The child view should be top/bottom letterboxed.
final int scaledChildHeight = unpaddedHeight * parentUnpaddedWidth
/ unpaddedWidth + childPaddingTop + childPaddingBottom;
child.layout(
parentLeft,
parentTop + (parentHeight - scaledChildHeight) / 2,
parentRight,
parentTop + (parentHeight + scaledChildHeight) / 2);
}
}
}
/**
* Update the backgrounds for the buttons to account for their current positioning.
* The primary and secondary buttons are special-cased in that their backgrounds change to
* create the illusion of a single-stroke boundary between them.
*/
private void updateBackgroundsForButtons() {
boolean bothButtonsExist = findViewById(R.id.button_primary) != null
&& findViewById(R.id.button_secondary) != null;
for (int row = 0; row < mIndicesOfRows.size() - 1; row++) {
final int rowStart = mIndicesOfRows.get(row);
final int rowEnd = mIndicesOfRows.get(row + 1);
final int rowSize = rowEnd - rowStart;
for (int i = rowStart; i < rowEnd; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == View.GONE || !isButton(child)) continue;
// Determine which background we need to show.
int background;
if (row == 0) {
// Button will be floating.
background = mBackgroundFloating;
} else if (rowSize == 1 || !bothButtonsExist) {
// Button takes up the full width of the screen.
background = mBackgroundFullRight;
} else if (mLayoutRTL) {
// Primary button will be to the left of the secondary.
background = child.getId() == R.id.button_primary
? mBackgroundFullLeft : mBackgroundFullRight;
} else {
// Primary button will be to the right of the secondary.
background = child.getId() == R.id.button_primary
? mBackgroundFullRight : mBackgroundFullLeft;
}
// Update the background.
LayoutParams params = (LayoutParams) child.getLayoutParams();
if (params.background != background) {
params.background = background;
// Save the padding; Android decides to overwrite it on some builds.
int paddingLeft = child.getPaddingLeft();
int paddingTop = child.getPaddingTop();
int paddingRight = child.getPaddingRight();
int paddingBottom = child.getPaddingBottom();
int buttonWidth = child.getMeasuredWidth();
int buttonHeight = child.getMeasuredHeight();
// Set the background, then restore the padding.
child.setBackgroundResource(background);
child.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
// Re-measuring is necessary to correct the text gravity.
int specWidth = MeasureSpec.makeMeasureSpec(buttonWidth, MeasureSpec.EXACTLY);
int specHeight = MeasureSpec.makeMeasureSpec(buttonHeight, MeasureSpec.EXACTLY);
measureChild(child, specWidth, specHeight);
}
}
}
}
static int getPaddingHorizontally(View v) {
if (v == null) {
return 0;
}
return v.getPaddingLeft() + v.getPaddingRight();
}
static int getPaddingHorizontally(View v) {
if (v == null) {
return 0;
}
return v.getPaddingLeft() + v.getPaddingRight();
}
/**
* Swap gravity:
* left for bottom
* right for top
* center horizontal for center vertical
* etc
* <p>
* also swap left|right padding for bottom|top
*/
private void fixGravityAndPadding(int direction)
{
for (int i = 0; i < getChildCount(); i++)
{
// gravity swap
View v = getChildAt(i);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) v.getLayoutParams();
int gravity = layoutParams.gravity;
if (direction == LinearLayout.VERTICAL)
{
if ((gravity & Gravity.LEFT) != 0)
{ // if gravity left is set . . .
gravity &= ~Gravity.LEFT; // unset left
gravity |= Gravity.BOTTOM; // and set bottom
}
} else
{
if ((gravity & Gravity.BOTTOM) != 0)
{ // etc
gravity &= ~Gravity.BOTTOM;
gravity |= Gravity.LEFT;
}
}
if (direction == LinearLayout.VERTICAL)
{
if ((gravity & Gravity.RIGHT) != 0)
{
gravity &= ~Gravity.RIGHT;
gravity |= Gravity.TOP;
}
} else
{
if ((gravity & Gravity.TOP) != 0)
{
gravity &= ~Gravity.TOP;
gravity |= Gravity.RIGHT;
}
}
// don't mess with children that are centered in both directions
if ((gravity & Gravity.CENTER) != Gravity.CENTER)
{
if (direction == LinearLayout.VERTICAL)
{
if ((gravity & Gravity.CENTER_VERTICAL) != 0)
{
gravity &= ~Gravity.CENTER_VERTICAL;
gravity |= Gravity.CENTER_HORIZONTAL;
}
} else
{
if ((gravity & Gravity.CENTER_HORIZONTAL) != 0)
{
gravity &= ~Gravity.CENTER_HORIZONTAL;
gravity |= Gravity.CENTER_VERTICAL;
}
}
}
layoutParams.gravity = gravity;
// padding swap
int paddingLeft = v.getPaddingLeft();
int paddingTop = v.getPaddingTop();
int paddingRight = v.getPaddingRight();
int paddingBottom = v.getPaddingBottom();
v.setPadding(paddingBottom, paddingRight, paddingTop, paddingLeft);
}
}
/**
* 对 View 设置 paddingLeft
*
* @param view 需要被设置的 View
* @param value 设置的值
*/
public static void setPaddingLeft(View view, int value) {
if (value != view.getPaddingLeft()) {
view.setPadding(value, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom());
}
}