下面列出了android.view.View#getWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Called each {@link #transformPage(android.view.View, float)} before {{@link #onTransform(android.view.View, float)} is called.
*
* @param view
* @param position
*/
protected void onPreTransform(View view, float position) {
final float width = view.getWidth();
view.setRotationX(0);
view.setRotationY(0);
view.setRotation(0);
view.setScaleX(1);
view.setScaleY(1);
view.setPivotX(0);
view.setPivotY(0);
view.setTranslationY(0);
view.setTranslationX(isPagingEnabled() ? 0f : -width * position);
if (hideOffscreenPages()) {
view.setAlpha(position <= -1f || position >= 1f ? 0f : 1f);
} else {
view.setAlpha(1f);
}
}
/**
* Given that the orientation is {@link Orientation#HORIZONTAL}, apply rotation if enabled.
*/
private void setChildRotationHorizontal(@Gravity int gravity, View child, int radius, Point center) {
if (!rotate) {
child.setRotation(0);
return;
}
boolean childPastCenter = (child.getX() + child.getWidth() / 2) > center.x;
float directionMult;
if (gravity == Gravity.END) {
directionMult = childPastCenter ? 1 : -1;
} else {
directionMult = childPastCenter ? -1 : 1;
}
final float opposite = Math.abs(child.getX() + child.getWidth() / 2.0f - center.x);
child.setRotation((float) (directionMult * Math.toDegrees(Math.asin(opposite / radius))));
}
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
float offset;
final int childWidth = changedView.getWidth();
// This reverses the positioning shown in onLayout.
if (checkDrawerViewGravity(changedView, Gravity.LEFT)) {
offset = (float) (childWidth + left) / childWidth;
} else {
final int width = getWidth();
offset = (float) (width - left) / childWidth;
}
setDrawerViewOffset(changedView, offset);
changedView.setVisibility(offset == 0 ? INVISIBLE : VISIBLE);
invalidate();
}
/**
* Calculates and returns the bottom offset size.
*
* @param orientation The layout orientation.
* @param childPosition The visible child position.
* @return the bottom offset or the last known offset if a child at the position is null.
*/
private int getBottomOffset(int orientation, int childPosition) {
View child = getChildAt(childPosition);
if (child == null) {
return mFallbackBottomOffset;
}
final Rect r = new Rect();
if (getGlobalVisibleRect(r)) {
if (orientation == OrientationHelper.HORIZONTAL) {
mFallbackBottomOffset = r.width() - child.getWidth();
} else {
mFallbackBottomOffset = r.height() - child.getHeight();
}
} else {
if (orientation == OrientationHelper.HORIZONTAL) {
mFallbackBottomOffset = getWidth() - child.getWidth();
} else {
mFallbackBottomOffset = getHeight() - child.getHeight();
}
}
return mFallbackBottomOffset;
}
/**
* Selects a random point int the view
* @param view - The view to use to get the point
*/
private void selectRandomPoint( View view ){
if ( view == null ) throw new IllegalArgumentException("The view is null, cannot get point");
// Get random coordinates
final int MAX_X = view.getWidth();
final int MAX_Y = view.getHeight();
Random randomGenerator = new Random();
final int X = randomGenerator.nextInt(MAX_X + 1);
final int Y = randomGenerator.nextInt(MAX_Y + 1);
// Add the point
mXYCoordinates.add(X);
mXYCoordinates.add(Y);
initHelpingToastsRoutine();
// Notify to the user
showInSnackbarWithDismissAction("Random click X = " + X + " / Y = " + Y);
}
/**
* 计算指定的 View 在屏幕中的坐标。
*/
public static RectF calcViewScreenLocation(View view) {
int[] location = new int[2];
// 获取控件在屏幕中的位置,返回的数组分别为控件左顶点的 x、y 的值
view.getLocationOnScreen(location);
return new RectF(location[0], location[1], location[0] + view.getWidth(),
location[1] + view.getHeight());
}
@Override
protected void onTransform(View view, float position) {
final float width = view.getWidth();
final float height = view.getHeight();
final float rotation = ROT_MOD * position * -1.25f;
ViewHelper.setPivotX(view,width * 0.5f);
ViewHelper.setPivotY(view,height);
ViewHelper.setRotation(view,rotation);
}
public int compare(View lhs, View rhs) {
int lhsHashCode = lhs.hashCode();
int rhsHashCode = rhs.hashCode();
int currentHashCode = AppStateManager.getInstance().getCurrentRootWindowsHashCode();
if (lhsHashCode == currentHashCode) {
return -1;
} else {
return rhsHashCode == currentHashCode ? 1 : rhs.getWidth() * rhs.getHeight() - lhs.getWidth() * lhs.getHeight();
}
}
@Override
protected void onTransform(View view, float position) {
if (position >= -1 || position <= 1) {
// Modify the default slide transition to shrink the page as well
final float height = view.getHeight();
final float width = view.getWidth();
final float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
final float vertMargin = height * (1 - scaleFactor) / 2;
final float horzMargin = width * (1 - scaleFactor) / 2;
// Center vertically
view.setPivotY(0.5f * height);
view.setPivotX(0.5f * width);
if (position < 0) {
view.setTranslationX(horzMargin - vertMargin / 2);
} else {
view.setTranslationX(-horzMargin + vertMargin / 2);
}
// Scale the page down (between MIN_SCALE and 1)
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
// Fade the page relative to its size.
view.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA));
}
}
@BindingAdapter("marginLeft_percent")
public static void bindPercentMarginLeft(View view, float percent) {
View parent = (View) view.getParent();
int leftOffset = (int) (parent.getWidth() * percent) - view.getWidth() / 2;
leftOffset = Math.min(leftOffset, parent.getWidth() - view.getWidth());
leftOffset = Math.max(leftOffset, 0);
BindingAdapters.bindLeftMargin(view, leftOffset);
}
private static void checkBitmap(Object thiz, Drawable drawable) {
if (drawable instanceof BitmapDrawable && thiz instanceof View) {
final Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
if (bitmap != null) {
final View view = (View) thiz;
int width = view.getWidth();
int height = view.getHeight();
if (width > 0 && height > 0) {
// 图标宽高都大于view带下的2倍以上,则警告
if (bitmap.getWidth() >= (width << 1)
&& bitmap.getHeight() >= (height << 1)) {
warn(bitmap.getWidth(), bitmap.getHeight(), width, height, new RuntimeException("Bitmap size too large"));
}
} else {
final Throwable stackTrace = new RuntimeException();
view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
int w = view.getWidth();
int h = view.getHeight();
if (w > 0 && h > 0) {
if (bitmap.getWidth() >= (w << 1)
&& bitmap.getHeight() >= (h << 1)) {
warn(bitmap.getWidth(), bitmap.getHeight(), w, h, stackTrace);
}
view.getViewTreeObserver().removeOnPreDrawListener(this);
}
return true;
}
});
}
}
}
}
/**
* Gets the rectangular position of a Bitmap if it were placed inside a View
* with scale type set to {@link android.widget.ImageView#ScaleType #CENTER_INSIDE}.
*
* @param bitmap the Bitmap
* @param view the parent View of the Bitmap
* @return the rectangular position of the Bitmap
*/
public static Rect getBitmapRectCenterInside(Bitmap bitmap, View view) {
final int bitmapWidth = bitmap.getWidth();
final int bitmapHeight = bitmap.getHeight();
final int viewWidth = view.getWidth();
final int viewHeight = view.getHeight();
return getBitmapRectCenterInsideHelper(bitmapWidth, bitmapHeight, viewWidth, viewHeight);
}
private void updateItem(View child, int width, int height) {
int size,childCenter,containerSize;
if (mIsVertical) {
containerSize = height;
size = child.getHeight();
childCenter = child.getTop() + size / 2;
} else {
containerSize = width;
size = child.getWidth();
childCenter = child.getLeft() + size / 2;
}
final int actionDistance = (containerSize + size) / 2;
final float effectsAmount = Math.min(1.0f, Math.max(-1.0f, (1.0f / actionDistance) * (childCenter - containerSize/2)));
if(mAlpha>0){
child.setAlpha(1-mAlpha*Math.abs(effectsAmount));
}
if(mScaleX>0||mScaleY>0){
child.setScaleX(1-mScaleX*Math.abs(effectsAmount));
child.setScaleY(1-mScaleY*Math.abs(effectsAmount));
}
if(mRotation!=0){
child.setRotation(mRotation * effectsAmount);
}
if(mXTranslate!=0){
child.setTranslationX(mXTranslate * Math.abs( effectsAmount));
}
if(mYTranslate!=0){
child.setTranslationY(mYTranslate * Math.abs( effectsAmount));
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (mTracking) {
return;
}
int width = r - l;
int height = b - t;
View handle = mHandle;
int childWidth = handle.getMeasuredWidth();
int childHeight = handle.getMeasuredHeight();
int childLeft = 0;
int childTop = 0;
View content = mContent;
switch (mOrientation) {
case TOP:
switch (mHandlePos) {
case LEFT:
childLeft = mHandlePad;
break;
case RIGHT:
childLeft = width - childWidth - mHandlePad;
break;
default:
childLeft = (width - childWidth) / 2;
break;
}
childTop = mExpanded ? (height - childHeight - mTopOffset) : -mBottomOffset;
content.layout(0, height - childHeight - mTopOffset - content.getMeasuredHeight(),
content.getMeasuredWidth(), height - childHeight - mTopOffset);
break;
case BOTTOM:
switch (mHandlePos) {
case LEFT:
childLeft = mHandlePad;
break;
case RIGHT:
childLeft = width - childWidth - mHandlePad;
break;
default:
childLeft = (width - childWidth) / 2;
break;
}
childTop = mExpanded ? mTopOffset : ((height - childHeight) + mBottomOffset);
content.layout(0, mTopOffset + childHeight, content.getMeasuredWidth(),
mTopOffset + childHeight + content.getMeasuredHeight());
break;
case RIGHT:
childLeft = mExpanded ? mTopOffset : ((width - childWidth) + mBottomOffset);
switch (mHandlePos) {
case TOP:
childTop = mHandlePad;
break;
case BOTTOM:
childTop = height - childHeight - mHandlePad;
break;
default:
childTop = (height - childHeight) / 2;
break;
}
content.layout(mTopOffset + childWidth, 0,
mTopOffset + childWidth + content.getMeasuredWidth(),
content.getMeasuredHeight());
break;
case LEFT:
childLeft = mExpanded ? (width - childWidth - mTopOffset) : -mBottomOffset;
switch (mHandlePos) {
case TOP:
childTop = mHandlePad;
break;
case BOTTOM:
childTop = height - childHeight - mHandlePad;
break;
default:
childTop = (height - childHeight) / 2;
break;
}
content.layout(width - childWidth - mTopOffset - content.getMeasuredWidth(), 0,
width - childWidth - mTopOffset, content.getMeasuredHeight());
break;
}
handle.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
mHandleHeight = handle.getHeight();
mHandleWidth = handle.getWidth();
}
public ResizeWidthAnimation(View view, int targetWidth) {
this.view = view;
this.targetWidth = targetWidth;
startWidth = view.getWidth();
}
public static Point getViewCenter(View view) {
int centerX = view.getLeft() + view.getWidth() / 2;
int centerY = view.getTop() + view.getHeight() / 2;
return new Point(centerX, centerY);
}
@Override
public void onChildDraw(final Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
// Fade out the view as it is swiped out of the parent's bounds
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
View itemView = viewHolder.itemView;
ALog.d("dx" + dX + "originDx" + origin_dx);
int width = itemView.getWidth();
Bitmap icon;
if (dX > 0) {
origin_dx = dX;
int position = viewHolder.getAdapterPosition();
if (position > -1 && mDatas != null && position < mDatas.size()){//处理越界错误
if (mDatas.get(position).isRead()){
icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_action_operation_uncheck);
}else {
icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_action_operation_check);
}
// Set color for right swipe
p.setColor(ContextCompat.getColor(context, R.color.blue));
// Draw Rect with varying right side, equal to displacement dX
c.drawRect((float) itemView.getLeft() + UIUtil.dpToPx(0), (float) itemView.getTop(), dX + UIUtil.dpToPx(0),
(float) itemView.getBottom(), p);
// Set the image icon for right swipe
c.drawBitmap(icon, (float) itemView.getLeft() + UIUtil.dpToPx(16), (float) itemView.getTop() +
((float) itemView.getBottom() - (float) itemView.getTop() - icon.getHeight()) / 2, p);
icon.recycle();
}
}
if (dX == 0 && origin_dx >0 && origin_dx < width){
//说明滑动了一部分又关闭了,这个时候也要标记已读
origin_dx = 0;
// mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
}
}
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
debugLog("onInterceptTouchEvent");
//if (e.getAction() == MotionEvent.ACTION_DOWN)
{
View itemView = rv.findChildViewUnder(e.getX(), e.getY());
if (itemView==null)
return false;
boolean dragging = false;
if ((dragHandleWidth > 0 ) && (e.getX() < dragHandleWidth))
{
dragging = true;
}
else if (viewHandleId != -1)
{
//Find the handle in the list item
View handleView = itemView.findViewById(viewHandleId);
if (handleView == null)
{
Log.e(TAG, "The view ID " + viewHandleId + " was not found in the RecycleView item");
return false;
}
//View should be visible to drag
if(handleView.getVisibility()!=View.VISIBLE) {
return false;
}
//We need to find the relative position of the handle to the parent view
//Then we can work out if the touch is within the handle
int[] parentItemPos = new int[2];
itemView.getLocationInWindow(parentItemPos);
int[] handlePos = new int[2];
handleView.getLocationInWindow(handlePos);
int xRel = handlePos[0] - parentItemPos[0];
int yRel = handlePos[1] - parentItemPos[1];
Rect touchBounds = new Rect(itemView.getLeft() + xRel, itemView.getTop() + yRel,
itemView.getLeft() + xRel + handleView.getWidth(),
itemView.getTop() + yRel + handleView.getHeight()
);
if (touchBounds.contains((int)e.getX(), (int)e.getY()))
dragging = true;
debugLog("parentItemPos = " + parentItemPos[0] + " " + parentItemPos[1]);
debugLog("handlePos = " + handlePos[0] + " " + handlePos[1]);
}
if (dragging)
{
debugLog("Started Drag");
setIsDragging(true);
floatingItem = createFloatingBitmap(itemView);
fingerAnchorY = (int)e.getY();
fingerOffsetInViewY = fingerAnchorY - itemView.getTop();
fingerY = fingerAnchorY;
selectedDragItemPos = rv.getChildPosition(itemView);
debugLog("selectedDragItemPos = " + selectedDragItemPos);
return true;
}
}
return false;
}
/**
* @param child 计算的view
* @param offset view的滑动偏移量
* @return 计算距离中心轴相对自身的偏移百分比
*/
private float calculateOffsetPercentToCenter(View child, float offset) {
final int distance = calculateDistanceToCenter(child, offset);
final int size = mOrientation == LinearLayout.HORIZONTAL ? child.getWidth() : child.getHeight();
return Math.max(-1.f, Math.min(1.f, distance * 1.f / size));
}
public static boolean pointInView(float x, float y, View v)
{
v.getLocationInWindow(sLocation);
return x >= sLocation[0] && x < (sLocation[0] + v.getWidth())
&& y >= sLocation[1] && y < (sLocation[1] + v.getHeight());
}