android 判断控件是否在屏幕范围内 是否可见

              Point p=new Point();
getWindowManager().getDefaultDisplay().getSize(p);
screenWidth=p.x;
screenHeight=p.y;


 Rect  rect=new Rect(0,0,screenWidth,screenHeight ); 

   
ImageView imageView = imageViewList.get(i);

int[] location = new int[2];
imageView.getLocationInWindow(location);

System.out.println(Arrays.toString(location));  


                      // Rect ivRect=new Rect(imageView.getLeft(),imageView.getTop(),imageView.getRight(),imageView.getBottom());



if (imageView.getLocalVisibleRect(rect)) {/*rect.contains(ivRect)*/ 
System.out.println("---------控件在屏幕可见区域-----显现-----------------");
} else {
imageView.setImageResource(R.drawable.p);
System.out.println("--------- 控件已不在屏幕可见区域(已滑出屏幕)-----隐去-----------------");
}

你可能感兴趣的:(android 判断控件是否在屏幕范围内 是否可见)