2016-12-22 5 views
0

私はここから同様の回答をたくさん探していますが、正確には動作しません。このようAndroid対策ビュー表示領域

enter image description here、黒:私は、カスタムビューの表示領域を計算する、ビューがスクロールビューのエッジによって画面端、またはブロックによって遮断することができ、下の写真を見てみましょう色は私の画面で、赤い色は私のカスタムビューで、少し上にスクロール、私は上記のように

B.

enter image description here

の面積を測定したい、黒い色は私の画面で、赤い色は私のカスタムビューがあり、青色はスクロールビューです。カスタムビューはスクロールビューの子であり、少し上にスクロールします。私はBの面積を測定したいです

1)私は試しましたが、View.getWindowVisibleDisplayFrameView.getLocalVisibleRectView.getGlobalVisibleRectですが、どれも正確に動作しません。彼らは一見一見良い、しかし私は私のビューが画面から消えていくと、何とか、それは私にビューの完全な高さと幅を表示し、ビューは画面内に表示されません。

2)手動でオフセットを計算し、ビューの(および画面の)高さと幅をXY座標とプラスマイナスにしてみましたが、画面の上部には常に余分なメニューがあるため、バーなどを表示し、結果を混乱させます。

3)これは私の状況ではそうではありませんが、私の視点の上に絶対的なレイアウトがあり、それを部分的にブロックしていれば、その領域を見つけることはできますか? (どちらのレイアウトも同じアクティビティにあります)

私の希望するエリアを簡単に正確に計算する方法はありますか?私は同じ問題に直面し、以下のコード

を通じてこの問題を解決するInstabugで私の仕事に新機能「ViewHierarchy」を実施中に

答えて

0

[OK]を、私はオープンソースの広告のフレームワークの1から答えを見つけました:

/** 
* Whether the view is at least certain % visible 
*/ 
boolean isVisible(@Nullable final View rootView, @Nullable final View view, final int minPercentageViewed) { 
    // ListView & GridView both call detachFromParent() for views that can be recycled for 
    // new data. This is one of the rare instances where a view will have a null parent for 
    // an extended period of time and will not be the main window. 
    // view.getGlobalVisibleRect() doesn't check that case, so if the view has visibility 
    // of View.VISIBLE but it's group has no parent it is likely in the recycle bin of a 
    // ListView/GridView and not on screen. 
    if (view == null || view.getVisibility() != View.VISIBLE || rootView.getParent() == null) { 
     return false; 
    } 

    if (!view.getGlobalVisibleRect(mClipRect)) { 
     // Not visible 
     return false; 
    } 

    // % visible check - the cast is to avoid int overflow for large views. 
    final long visibleViewArea = (long) mClipRect.height() * mClipRect.width(); 
    final long totalViewArea = (long) view.getHeight() * view.getWidth(); 

    if (totalViewArea <= 0) { 
     return false; 
    } 

    return 100 * visibleViewArea >= minPercentageViewed * totalViewArea; 
} 

ビューが画面から消えたとき、私は、View.getGlobalVisibleRectを使用していながら、私はミスを犯し、この方法ではあるが、falseを返しますmClipRectオブジェクトは依然として価値を提供しています。上記は正しい使い方です。

0

はこれがないutilのクラスですべてのロジック

public class ViewFrameInspector { 

private static final String KEY_X = "x"; 
private static final String KEY_Y = "y"; 
private static final String KEY_W = "w"; 
private static final String KEY_H = "h"; 

/** 
* Method emit inspected ViewFrame of passed view, the emit ViewFrame contains inspected ViewFrames fot its children and children of the children and so on 
* by converting the emitted ViewFrame to list of View Frames you can find the a specific view and its frame with easily way 
* 
* @param view the root view 
* @return return ViewFrame observable 
*/ 
public static Observable<ViewFrame> inspectRootViewFrameRx(final View view) { 

    return Observable.defer(new Func0<Observable<ViewFrame>>() { 
     @Override 
     public Observable<ViewFrame> call() { 
      ViewFrame rootViewFrame = new ViewFrame(); 
      rootViewFrame.setRoot(true); 
      rootViewFrame.setView(view); 
      return Observable.just(inspectVisibleViewFrame(rootViewFrame)); 
     } 
    }); 
} 

private static ViewFrame inspectVisibleViewFrame(final ViewFrame viewFrame) { 
    if (viewFrame.getView().getVisibility() == View.VISIBLE) 
     try { 
      viewFrame.setId(inspectViewResourceId(viewFrame.getView().getContext(), viewFrame.getView().getId())); 
      viewFrame.setType(ViewFrameInspector.inspectViewType(viewFrame.getView())); 
      viewFrame.setOriginalRect(ViewFrameInspector.inspectViewOriginalRect(viewFrame.getView())); 
      viewFrame.setVisibleRect(ViewFrameInspector.inspectViewVisibleRect(viewFrame)); 
      viewFrame.setFrame(ViewFrameInspector.inspectViewFrame(viewFrame)); 
      // inspect view children if exist 
      if (viewFrame.getView() instanceof ViewGroup) { 
       viewFrame.setHasChildren(true); 
       inspectViewChildren(viewFrame); 
      } else { 
       viewFrame.setHasChildren(false); 
      } 
     } catch (JSONException e) { 
      Log.e(ActivityViewInspector.class.getSimpleName(), "inspect view frame got error: " + e.getMessage() + ",view id:" + viewFrame.getId() + ", time in MS: " + System.currentTimeMillis(), e); 
     } 
    return viewFrame; 
} 

private static void inspectViewChildren(ViewFrame parentViewFrame) throws JSONException { 
    if (parentViewFrame.getView() instanceof ViewGroup) { 
     ViewGroup parent = (ViewGroup) parentViewFrame.getView(); 
     for (int i = 0; i < parent.getChildCount(); i++) { 
      ViewFrame childViewFrame = new ViewFrame(); 
      childViewFrame.setRoot(false); 
      childViewFrame.setView(parent.getChildAt(i)); 
      childViewFrame.setParent(parentViewFrame); 
      parentViewFrame.addNode(inspectVisibleViewFrame(childViewFrame)); 
     } 
    } 
} 

private static String inspectViewType(View view) { 
    return view.getClass().getSimpleName(); 
} 

private static String inspectViewResourceId(Context context, int id) throws JSONException { 
    try { 
     return context != null && context.getResources() != null && context.getResources().getResourceEntryName(id) != null ? 
       context.getResources().getResourceEntryName(id) : String.valueOf(id); 
    } catch (Resources.NotFoundException e) { 
     return String.valueOf(id); 
    } 
} 

private static Rect inspectViewOriginalRect(View view) { 
    int[] locationOnScreen = new int[2]; 
    view.getLocationOnScreen(locationOnScreen); 
    return new Rect(locationOnScreen[0], 
      locationOnScreen[1], 
      locationOnScreen[0] + view.getWidth(), 
      locationOnScreen[1] + view.getHeight()); 
} 

private static Rect inspectViewVisibleRect(ViewFrame viewFrame) { 
    if (viewFrame.isRoot()) { 
     return viewFrame.getOriginalRect(); 
    } else { 
     Rect viewVisibleRect = new Rect(
       viewFrame.getOriginalRect().left, 
       viewFrame.getOriginalRect().top, 
       viewFrame.getOriginalRect().right, 
       viewFrame.getOriginalRect().bottom); 
     Rect parentAvailableVisibleRect = new Rect(
       inspectViewAvailableX(viewFrame.getParent()), 
       inspectViewAvailableY(viewFrame.getParent()), 
       inspectViewAvailableRight(viewFrame.getParent()), 
       inspectViewAvailableBottom(viewFrame.getParent())); 
     if (viewVisibleRect.intersect(parentAvailableVisibleRect)) { 
      return viewVisibleRect; 
     } else { 
      return new Rect(0, 0, 0, 0); 
     } 
    } 
} 

private static int inspectViewAvailableX(ViewFrame viewFrame) { 
    int visibleLeft, paddingLeft, originalLeft; 
    visibleLeft = viewFrame.getVisibleRect().left; 
    paddingLeft = viewFrame.getView().getPaddingLeft(); 
    originalLeft = viewFrame.getOriginalRect().left; 
    if (paddingLeft == 0) { 
     return visibleLeft; 
    } else { 
     if (visibleLeft > (originalLeft + paddingLeft)) { 
      return visibleLeft; 
     } else { 
      return originalLeft + paddingLeft; 
     } 
    } 
} 

private static int inspectViewAvailableY(ViewFrame viewFrame) { 
    int visibleTop, paddingTop, originalTop; 
    visibleTop = viewFrame.getVisibleRect().top; 
    paddingTop = viewFrame.getView().getPaddingTop(); 
    originalTop = viewFrame.getOriginalRect().top; 
    if (paddingTop == 0) { 
     return visibleTop; 
    } else { 
     if (visibleTop > (originalTop + paddingTop)) { 
      return visibleTop; 
     } else { 
      return originalTop + paddingTop; 
     } 
    } 
} 

private static int inspectViewAvailableRight(ViewFrame viewFrame) { 
    int visibleRight, paddingRight, originalRight; 
    visibleRight = viewFrame.getVisibleRect().right; 
    paddingRight = viewFrame.getView().getPaddingRight(); 
    originalRight = viewFrame.getOriginalRect().right; 
    if (paddingRight == 0) { 
     return visibleRight; 
    } else { 
     if (visibleRight < (originalRight - paddingRight)) { 
      return visibleRight; 
     } else { 
      return originalRight - paddingRight; 
     } 
    } 
} 

private static int inspectViewAvailableBottom(ViewFrame viewFrame) { 
    int visibleBottom, paddingBottom, originalBottom; 
    visibleBottom = viewFrame.getVisibleRect().bottom; 
    paddingBottom = viewFrame.getView().getPaddingBottom(); 
    originalBottom = viewFrame.getOriginalRect().bottom; 
    if (paddingBottom == 0) { 
     return visibleBottom; 
    } else { 
     if (visibleBottom < (originalBottom - paddingBottom)) { 
      return visibleBottom; 
     } else { 
      return originalBottom - paddingBottom; 
     } 
    } 
} 

private static JSONObject inspectViewFrame(ViewFrame viewFrame) throws JSONException { 
    return new JSONObject().put(KEY_X, viewFrame.getVisibleRect().left) 
      .put(KEY_Y, viewFrame.getVisibleRect().top) 
      .put(KEY_W, viewFrame.getVisibleRect().width()) 
      .put(KEY_H, viewFrame.getVisibleRect().height()); 
} 

public static List<ViewFrame> convertViewHierarchyToList(ViewFrame viewFrame) { 
    ArrayList<ViewFrame> viewFrameHierarchies = new ArrayList<>(); 
    if (viewFrame != null) { 
     viewFrameHierarchies.add(viewFrame); 
     if (viewFrame.hasChildren()) { 
      for (ViewFrame childViewHierarchy : viewFrame.getNodes()) { 
       viewFrameHierarchies.addAll(convertViewHierarchyToList(childViewHierarchy)); 
      } 
     } 
    } 
    return viewFrameHierarchies; 
} 

} 

これはあなたにも

このコードのヘルプ検査ビューに関連するすべてのデータを保持するモデルクラス

public class ViewFrame { 

private String id; 
private String type; 
private JSONObject frame; 
private ViewFrame parent; 
private ArrayList<ViewFrame> nodes; 
private boolean hasChildren; 
private boolean isRoot; 
private Rect originalRect; 
private Rect visibleRect; 
private View view; 

public ViewFrame() { 
    nodes = new ArrayList<>(); 
} 

public String getId() { 
    return id; 
} 

public void setId(String id) { 
    this.id = id; 
} 

public String getType() { 
    return type; 
} 

public void setType(String type) { 
    this.type = type; 
} 

public JSONObject getFrame() { 
    return frame; 
} 

public void setFrame(JSONObject frame) { 
    this.frame = frame; 
} 

public ViewFrame getParent() { 
    return parent; 
} 

public void setParent(ViewFrame parent) { 
    this.parent = parent; 
} 

public ArrayList<ViewFrame> getNodes() { 
    return nodes; 
} 

public void addNode(ViewFrame childViewHierarchy) { 
    nodes.add(childViewHierarchy); 
} 

public boolean hasChildren() { 
    return hasChildren; 
} 

public void setHasChildren(boolean hasChildren) { 
    this.hasChildren = hasChildren; 
} 

public boolean isRoot() { 
    return isRoot; 
} 

public void setRoot(boolean root) { 
    isRoot = root; 
} 

public Rect getVisibleRect() { 
    return visibleRect; 
} 

public void setVisibleRect(Rect visibleRect) { 
    this.visibleRect = visibleRect; 
} 

public Rect getOriginalRect() { 
    return originalRect; 
} 

public void setOriginalRect(Rect originalRect) { 
    this.originalRect = originalRect; 
} 

public View getView() { 
    return view; 
} 

public void setView(View view) { 
    this.view = view; 
} 
} 

希望です

関連する問題