当前位置: 移动技术网 > IT编程>移动开发>Android > Android中HorizontalScrollView使用方法详解

Android中HorizontalScrollView使用方法详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

南邮教师堕胎门,咕咕网盘,南宁市电单车价格

由于移动设备物理显示空间一般有限,不可能一次性的把所有要显示的内容都显示在屏幕上。所以各大平台一般会提供一些可滚动的视图来向用户展示数据。android平台框架中为我们提供了诸如listview、girdview、scrollview等滚动视图控件,这几个视图控件也是我们平常使用最多的。下面介绍一下horizontalscrollview的使用和需要注意的点:

 horizontalscrollview是一个framelayout  ,这意味着你只能在它下面放置一个子控件,这个子控件可以包含很多数据内容。有可能这个子控件本身就是一个布局控件,可以包含非常多的其他用来展示数据的控件。这个布局控件一般使用的是一个水平布局的linearlayout  。textview也是一个可滚动的视图控件,所以一般不需要horizontalscrollview

下面介绍一个horizontalscrollview中包含许多图片,并且可以滚动浏览的示例

 @override 
 protected void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout. activity_main); 
  
  mlinearlayout = (linearlayout) findviewbyid(r.id.mygallery); 
  
  file externaldir = environment. getexternalstoragedirectory(); 
  string photospath = externaldir.getabsolutepath() + "/test/"; 
  file photosfile = new file(photospath); 
  
  for (file photofile : photosfile.listfiles()) { 
    mlinearlayout.addview(getimageview(photofile.getabsolutepath())); 
  } 
  
} 
 
 private view getimageview(string absolutepath) { 
  
  bitmap bitmap = decodebitmapfromfile(absolutepath, 200, 200); 
 linearlayout layout = new linearlayout(getapplicationcontext()); 
 layout.setlayoutparams( new layoutparams(250, 250)); 
 layout.setgravity(gravity. center); 
  
  imageview imageview = new imageview(this); 
  imageview.setlayoutparams( new layoutparams(200,200)); 
  imageview.setscaletype(imageview.scaletype. center_crop); 
  imageview.setimagebitmap(bitmap); 
  layout.addview(imageview); 
  
  return layout; 
} 
 
 private bitmap decodebitmapfromfile(string absolutepath, int reqwidth, int reqheight) { 
 bitmap bm = null; 
  
  // first decode with injustdecodebounds=true to check dimensions 
  final bitmapfactory.options options = new bitmapfactory.options(); 
  options. injustdecodebounds = true ; 
  bitmapfactory. decodefile(absolutepath, options); 
  
  // calculate insamplesize 
  options. insamplesize = calculateinsamplesize(options, reqwidth, reqheight); 
  
  // decode bitmap with insamplesize set 
  options. injustdecodebounds = false ; 
  bm = bitmapfactory. decodefile(absolutepath, options); 
  
  return bm; 
} 
 
 private int calculateinsamplesize(options options, int reqwidth, 
    int reqheight) { 
  // raw height and width of image 
  final int height = options.outheight; 
  final int width = options.outwidth; 
  int insamplesize = 1; 
   
  if (height > reqheight || width > reqwidth) { 
  if (width > height) { 
  insamplesize = math. round((float)height / ( float)reqheight); 
  } else { 
  insamplesize = math. round((float)width / ( float)reqwidth); 
  } 
  } 
  
  return insamplesize; 
} 

要显示的图片放在外置sdcard中test目录下,上面的示例程序只是显示了一张张大图片的缩略版本,对这方面不懂的可以参看:

android如何高效显示较大的bitmaps

horizontalscrollview还可以设置滚动到一个指定的位置(x,0),它的子控件也会跟随着滚动。

new handler().postdelayed(new runnable() { 
 @override 
 public void run() { 
  // 水平直接滚动800px,如果想效果更平滑可以使用smoothscrollto(int x, int y) 
  hsv.scrollto(800, 0); 
 } 
}, 2000); 

效果图:

以上就是本文的全部内容,希望对大家学习android软件编程有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网