下面列出了android.view.View#setX ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates and returns a layout listener, which allows to position a tab, which is a neighbor of
* the currently selected tab, when swiping horizontally.
*
* @param neighbor
* The tab item, which corresponds to the neighboring tab, as an instance of the class
* {@link TabItem}. The tab item may not be null
* @param dragDistance
* The distance of the swipe gesture in pixels as a {@link Float} value
* @return The layout listener, which has been created, as an instance of the type {@link
* OnGlobalLayoutListener}. The layout listener may not be null
*/
@NonNull
private OnGlobalLayoutListener createSwipeNeighborLayoutListener(
@NonNull final TabItem neighbor, final float dragDistance) {
return new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
View view = contentViewRecycler.getView(neighbor.getTab());
if (view != null) {
float position;
if (dragDistance > 0) {
position = -getTabSwitcher().getWidth() + dragDistance - swipedTabDistance;
} else {
position = getTabSwitcher().getWidth() + dragDistance + swipedTabDistance;
}
view.setX(position);
}
}
};
}
/**
* Creates and returns a layout listener, which allows to show a tab as the currently selected
* one, once it view has been inflated.
*
* @param item
* The item, which corresponds to the tab, which has been added, as an instance of the
* class {@link AbstractItem}. The item may not be null
* @return The listener, which has been created, as an instance of the type {@link
* OnGlobalLayoutListener}. The listener may not be null
*/
@NonNull
private OnGlobalLayoutListener createAddSelectedTabLayoutListener(
@NonNull final AbstractItem item) {
return new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
View view = item.getView();
FrameLayout.LayoutParams layoutParams =
(FrameLayout.LayoutParams) view.getLayoutParams();
view.setAlpha(1f);
getArithmetics().setPivot(Axis.DRAGGING_AXIS, item,
getArithmetics().getPivot(Axis.DRAGGING_AXIS, item, DragState.NONE));
getArithmetics().setPivot(Axis.ORTHOGONAL_AXIS, item,
getArithmetics().getPivot(Axis.ORTHOGONAL_AXIS, item, DragState.NONE));
view.setX(layoutParams.leftMargin);
view.setY(layoutParams.topMargin);
getArithmetics().setScale(Axis.DRAGGING_AXIS, item, 1);
getArithmetics().setScale(Axis.ORTHOGONAL_AXIS, item, 1);
}
};
}
private boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
dX = v.getX() - event.getRawX();
dY = v.getY() - event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
v.setX(event.getRawX() + dX);
v.setY(event.getRawY() + dY);
break;
case MotionEvent.ACTION_UP:
break;
default:
return false;
}
return true;
}
private void onActionMove(MotionEvent event, View view) {
if (lastX >= 0 && lastY > 0) {
float newX = event.getX();
float newY = event.getY();
int xDiffInt = (int) (newX - lastX);
int yDiffInt = (int) (newY - lastY);
Log.d(TAG, "Swiping - Xdiff " + newX + " - " + lastX + " = " + xDiffInt + " Ydiff: " + newY + "-" + lastY + " = " + yDiffInt);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
//If moving right, stop if image's left reach screen left
if (xDiffInt > 0) {
if (layoutParams.leftMargin < 0) {
if (view.getX() >= 0) {
view.setX(0);
} else {
view.setX(view.getX() + xDiffInt);
}
}
}
//If moving left, stop if image's right reach screen right
else if (xDiffInt < 0) {
if (layoutParams.rightMargin < 0) {
if (view.getX() + view.getWidth() <= getScreenWidth()) {
view.setX(getScreenWidth() - view.getWidth());
} else {
view.setX(view.getX() + xDiffInt);
}
}
}
//Disabled vertical movements
//view.setY(view.getY() + yDiffInt);
}
}
/**
* Creates and returns an animation listener, which allows to inflate or remove the views, which
* are used to visualize tabs, when an animation, which is used to hide the tab switcher, has
* been finished.
*
* @return The animation listener, which has been created, as an instance of the type {@link
* AnimatorListener}. The listener may not be null
*/
@NonNull
private AnimatorListener createHideSwitcherAnimationListener() {
return new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(final Animator animation) {
super.onAnimationEnd(animation);
AbstractItemIterator iterator =
new ItemIterator.Builder(getTabSwitcher(), tabViewRecycler).create();
AbstractItem item;
while ((item = iterator.next()) != null) {
if (((TabItem) item).getTab() == getModel().getSelectedTab()) {
Pair<View, Boolean> pair = tabViewRecycler.inflate(item);
View view = pair.first;
FrameLayout.LayoutParams layoutParams =
(FrameLayout.LayoutParams) view.getLayoutParams();
view.setAlpha(1f);
getArithmetics().setScale(Axis.DRAGGING_AXIS, item, 1);
getArithmetics().setScale(Axis.ORTHOGONAL_AXIS, item, 1);
view.setX(layoutParams.leftMargin);
view.setY(layoutParams.topMargin);
} else {
tabViewRecycler.remove(item);
}
}
tabViewRecycler.clearCache();
tabRecyclerAdapter.clearCachedPreviews();
tabViewBottomMargin = -1;
}
};
}
/**
* Every TimeAnimator frame, nudge each bouncing view along.
*/
public void onTimeUpdate(TimeAnimator animation, long elapsed, long dt_ms) {
final float dt = dt_ms / 1000f; // seconds
for (int i = 0; i < getChildCount(); i++) {
final View view = getChildAt(i);
final PointF v = (PointF) view.getTag();
// step view for velocity * time
view.setX(view.getX() + v.x * dt);
view.setY(view.getY() + v.y * dt);
// handle reflections
final float l = view.getX();
final float t = view.getY();
final float r = l + view.getWidth();
final float b = t + view.getHeight();
boolean flipX = false, flipY = false;
if (r > mWidth) {
view.setX(view.getX() - 2 * (r - mWidth));
flipX = true;
} else if (l < 0) {
view.setX(-l);
flipX = true;
}
if (b > mHeight) {
view.setY(view.getY() - 2 * (b - mHeight));
flipY = true;
} else if (t < 0) {
view.setY(-t);
flipY = true;
}
if (flipX) v.x *= -1;
if (flipY) v.y *= -1;
}
}
private void addWaterView(List<WaterModel> modelList) {
int[] xRandom = randomCommon(1, 8, modelList.size());
int[] yRandom = randomCommon(1, 7, modelList.size());
if (xRandom == null || yRandom == null) {
return;
}
for (int i = 0; i < modelList.size(); i++) {
WaterModel waterModel = modelList.get(i);
final View view = mLayoutInflater.inflate(R.layout.item_water, this, false);
view.setX((float) ((mWidth * xRandom[i] * 0.11)));
view.setY((float) ((mHeight * yRandom[i] * 0.08)));
view.setTag(waterModel);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Object tag = v.getTag();
if (tag instanceof WaterModel) {
if (mOnWaterItemListener != null) {
mOnWaterItemListener.onItemClick((WaterModel) tag);
collectAnimator(view);
}
}
}
});
view.setTag(R.string.isUp, mRandom.nextBoolean());
setOffset(view);
addView(view);
addShowViewAnimation(view);
start(view);
}
}
@Override
public void computeScroll() {
mScrimOpacity = 1 - mScrollPercent;
if (mDragHelper.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
int count = getChildCount();
if (mScrimOpacity >= 0 && mCapturedView != null && count > 1) {
int leftOffset = (int) ((mCapturedView.getLeft() - getWidth()) * mParallaxOffset * mScrimOpacity);
View underlying = getChildAt(count - 2);
underlying.setX(leftOffset > 0 ? 0 : leftOffset);
}
}
@Synthetic
void startDrag(float startX, float startY) {
if (mVibrationEnabled) {
mVibrator.vibrate(VIBRATE_DURATION_MS * 2);
}
Toast.makeText(getContext(), R.string.hint_drag, Toast.LENGTH_SHORT).show();
//noinspection Convert2Lambda
super.setOnTouchListener(new OnTouchListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_MOVE:
mMoved = true;
view.setX(motionEvent.getRawX() - startX); // TODO compensate shift
view.setY(motionEvent.getRawY() - startY);
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
bindNavigationPad();
if (mMoved) {
persistPosition();
}
break;
default:
return false;
}
return true;
}
});
}
@Override
public final void onSwitchingBetweenTabs(final int selectedTabIndex, final float distance) {
TabItem tabItem = TabItem.create(getModel(), getTabViewRecycler(), selectedTabIndex);
View view = contentViewRecycler.getView(tabItem.getTab());
if (view != null) {
if (distance == 0 || (distance > 0 && selectedTabIndex < getModel().getCount() - 1) ||
(distance < 0 && selectedTabIndex > 0)) {
view.setX(distance);
if (distance != 0) {
TabItem neighbor = TabItem.create(getModel(), getTabViewRecycler(),
distance > 0 ? selectedTabIndex + 1 : selectedTabIndex - 1);
if (Math.abs(distance) >= swipedTabDistance) {
inflateContent(neighbor.getTab(),
createSwipeNeighborLayoutListener(neighbor, distance));
} else {
contentViewRecycler.remove(neighbor.getTab());
}
}
} else {
float position = (float) Math.pow(Math.abs(distance), 0.75);
position = distance < 0 ? position * -1 : position;
view.setX(position);
}
getLogger().logVerbose(getClass(),
"Swiping content of tab at index " + selectedTabIndex +
". Current swipe distance is " + distance + " pixels");
}
}
private void resetSubButton(Point center, SubButton subButton, View subButtonView) {
if (subButton != null) {
int floatingMenuButtonWidth = floatingMenuButton.getWidth();
int floatingMenuButtonHeight = floatingMenuButton.getHeight();
int xResetPos = center.x + floatingMenuButtonWidth / 2;
int yResetPos = center.y - floatingMenuButtonHeight / 2;
subButtonView.setX(xResetPos);
subButtonView.setY(yResetPos);
subButtonView.setScaleX(0);
subButtonView.setScaleY(0);
subButton.setX(xResetPos);
subButton.setY(yResetPos);
subButton.setAlpha(0);
}
}
@Override
public void show(final FrameLayout previewView, final PreviewBar previewBar) {
if (previewBar.getMax() == 0 || isShowing) {
return;
}
isHiding = false;
isShowing = true;
final View overlayView = getOrCreateOverlayView(previewView);
final View morphView = getOrCreateMorphView(previewView, previewBar);
cancelPendingAnimations(previewView, overlayView, morphView);
// If we were still moving to hide the preview,
// we can resume from there instead
if (isMovingToHide || isMorphingToHide) {
isMovingToHide = false;
isMorphingToHide = false;
startCircularReveal(previewView, overlayView, morphView);
return;
}
tintViews(previewBar, morphView, overlayView);
morphView.setY(getMorphStartY(previewBar));
morphView.setX(getMorphStartX(previewBar, getOffset(previewBar)));
morphView.setScaleX(0f);
morphView.setScaleY(0f);
morphView.setAlpha(1.0f);
startShowTranslation(previewView, previewBar, overlayView, morphView);
}
private void computeScroll(@Nullable View view) {
if (view == null) {
return;
}
view.setX(0);
view.setY(0);
View capturedView = mDragHelper.getCapturedView();
if (capturedView == null) {
return;
}
int xOffset = 0;
int yOffset = 0;
switch (mCurrentSwipeOrientation) {
case ViewDragHelper.EDGE_LEFT:
xOffset = (capturedView.getLeft() - getWidth());
break;
case ViewDragHelper.EDGE_RIGHT:
xOffset = (capturedView.getLeft() + getWidth());
break;
case ViewDragHelper.EDGE_TOP:
yOffset = (capturedView.getTop() - getHeight());
break;
case ViewDragHelper.EDGE_BOTTOM:
yOffset = (capturedView.getTop() + getHeight());
break;
}
if (mParallaxOffset >= 0) {
xOffset *= mScrimOpacity * mParallaxOffset;
yOffset *= mScrimOpacity * mParallaxOffset;
}
view.setX(xOffset);
view.setY(yOffset);
}
@Override
public void showMoreKeysPanel(final View parentView, final Controller controller,
final int pointX, final int pointY, final KeyboardActionListener listener) {
mController = controller;
mListener = listener;
final View container = getContainerView();
// The coordinates of panel's left-top corner in parentView's coordinate system.
// We need to consider background drawable paddings.
final int x = pointX - getDefaultCoordX() - container.getPaddingLeft() - getPaddingLeft();
final int y = pointY - container.getMeasuredHeight() + container.getPaddingBottom()
+ getPaddingBottom();
parentView.getLocationInWindow(mCoordinates);
// Ensure the horizontal position of the panel does not extend past the parentView edges.
final int maxX = parentView.getMeasuredWidth() - container.getMeasuredWidth();
final int panelX = Math.max(0, Math.min(maxX, x)) + CoordinateUtils.x(mCoordinates);
final int panelY = y + CoordinateUtils.y(mCoordinates);
container.setX(panelX);
container.setY(panelY);
mOriginX = x + container.getPaddingLeft();
mOriginY = y + container.getPaddingTop();
controller.onShowMoreKeysPanel(this);
final MoreKeysKeyboardAccessibilityDelegate accessibilityDelegate = mAccessibilityDelegate;
if (accessibilityDelegate != null
&& AccessibilityUtils.getInstance().isAccessibilityEnabled()) {
accessibilityDelegate.onShowMoreKeysKeyboard();
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
for (int targetId : mGuideViewsMap.keySet()) {
int targetX = 0, targetY = 0;
if (targetId != NO_TARGET_GUIDE_ID) {
View targetView = mTargetViewMap.get(targetId);
Rect rect = getTargetViewRect(targetView);
targetX = rect.left;
targetY = rect.top;
}
for (View guideView : mGuideViewsMap.get(targetId)) {
int relativeX = 0;
int relativeY = 0;
if (mGuideRelativePosMap.containsKey(guideView.hashCode())) {
relativeX = mGuideRelativePosMap.get(guideView.hashCode()).get("x");
relativeY = mGuideRelativePosMap.get(guideView.hashCode()).get("y");
}
LayoutParams params = (LayoutParams) guideView.getLayoutParams();
//尝试兼容View中设定的水平居中或垂直居中属性
int gravity = params.gravity;
int absoluteGravity = 0;
if (Build.VERSION.SDK_INT >= 17) {
final int layoutDirection = getLayoutDirection();
absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
}
final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
if (absoluteGravity == Gravity.CENTER_HORIZONTAL) {
guideView.setX((screenWidth - guideView.getMeasuredWidth()) / 2);
}
else {
guideView.setX(targetX + params.leftMargin + relativeX);
}
if (verticalGravity == Gravity.CENTER_VERTICAL) {
guideView.setY((screenHeight - guideView.getMeasuredHeight()) / 2);
}
else {
guideView.setY(targetY + params.topMargin + relativeY);
}
}
}
}
static void setX(View view, float x) {
view.setX(x);
}
public static void set(final View view, final float x, final float y, final float alpha) {
view.setAlpha(alpha);
view.setX(x);
view.setY(y);
}
/**
* This method handles setting the property values directly in the View object's fields.
* propertyConstant tells it which property should be set, value is the value to set
* the property to.
*
* @param propertyConstant The property to be set
* @param value The value to set the property to
*/
private void setValue(int propertyConstant, float value) {
//final View.TransformationInfo info = mView.mTransformationInfo;
View v = mView.get();
if (v != null) {
switch (propertyConstant) {
case TRANSLATION_X:
//info.mTranslationX = value;
v.setTranslationX(value);
break;
case TRANSLATION_Y:
//info.mTranslationY = value;
v.setTranslationY(value);
break;
case ROTATION:
//info.mRotation = value;
v.setRotation(value);
break;
case ROTATION_X:
//info.mRotationX = value;
v.setRotationX(value);
break;
case ROTATION_Y:
//info.mRotationY = value;
v.setRotationY(value);
break;
case SCALE_X:
//info.mScaleX = value;
v.setScaleX(value);
break;
case SCALE_Y:
//info.mScaleY = value;
v.setScaleY(value);
break;
case X:
//info.mTranslationX = value - v.mLeft;
v.setX(value);
break;
case Y:
//info.mTranslationY = value - v.mTop;
v.setY(value);
break;
case ALPHA:
//info.mAlpha = value;
v.setAlpha(value);
break;
}
}
}
public static void setX(View view, float value) {
view.setX(value);
}
@Override
public void setValue(View view, float value) {
view.setX(value);
}