Android中HorizontalScrollView的使用

本文介绍了如何在Android中使用HorizontalScrollView滚动展示大量图片的方法,包括创建布局、加载图片及实现图片滚动的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

     下面介绍一个HorizontalScrollView中包含许多图片,并且可以滚动浏览的示例
[java]  view plain copy
  1.  @Override  
  2.  protected void onCreate(Bundle savedInstanceState) {  
  3.        super.onCreate(savedInstanceState);  
  4.       setContentView(R.layout. activity_main);  
  5.         
  6.        mLinearLayout = (LinearLayout) findViewById(R.id.mygallery);  
  7.         
  8.       File externalDir = Environment. getExternalStorageDirectory();  
  9.       String photosPath = externalDir.getAbsolutePath() + "/test/";  
  10.       File photosFile = new File(photosPath);  
  11.         
  12.        for (File photoFile : photosFile.listFiles()) {  
  13.               mLinearLayout.addView(getImageView(photoFile.getAbsolutePath()));  
  14.       }  
  15.         
  16. }  
  17.   
  18.  private View getImageView(String absolutePath) {  
  19.         
  20.       Bitmap bitmap = decodeBitmapFromFile(absolutePath, 200200);  
  21.     LinearLayout layout = new LinearLayout(getApplicationContext());  
  22.     layout.setLayoutParams( new LayoutParams(250250));  
  23.     layout.setGravity(Gravity. CENTER);  
  24.       
  25.       ImageView imageView = new ImageView(this);  
  26.       imageView.setLayoutParams( new LayoutParams(200,200));  
  27.       imageView.setScaleType(ImageView.ScaleType. CENTER_CROP);  
  28.       imageView.setImageBitmap(bitmap);  
  29.       layout.addView(imageView);  
  30.         
  31.        return layout;  
  32. }  
  33.   
  34.  private Bitmap decodeBitmapFromFile(String absolutePath, int reqWidth, int reqHeight) {  
  35.     Bitmap bm = null;  
  36.       
  37.      // First decode with inJustDecodeBounds=true to check dimensions  
  38.      final BitmapFactory.Options options = new BitmapFactory.Options();  
  39.      options. inJustDecodeBounds = true ;  
  40.      BitmapFactory. decodeFile(absolutePath, options);  
  41.       
  42.      // Calculate inSampleSize  
  43.      options. inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);  
  44.       
  45.      // Decode bitmap with inSampleSize set  
  46.      options. inJustDecodeBounds = false ;  
  47.      bm = BitmapFactory. decodeFile(absolutePath, options);  
  48.       
  49.      return bm;   
  50. }  
  51.   
  52.  private int calculateInSampleSize(Options options, int reqWidth,  
  53.               int reqHeight) {  
  54.      // Raw height and width of image  
  55.      final int height = options.outHeight;  
  56.      final int width = options.outWidth;  
  57.      int inSampleSize = 1;  
  58.          
  59.      if (height > reqHeight || width > reqWidth) {  
  60.       if (width > height) {  
  61.        inSampleSize = Math. round((float)height / ( float)reqHeight);    
  62.       } else {  
  63.        inSampleSize = Math. round((float)width / ( float)reqWidth);    
  64.       }    
  65.      }  
  66.       
  67.      return inSampleSize;   
  68. }  

     要显示的图片放在外置SDCard中test目录下,上面的示例程序只是显示了一张张大图片的缩略版本,对这方面不懂的可以参看 http://blog.youkuaiyun.com/tibib/article/details/8726486
     HorizontalScrollView还可以设置滚动到一个指定的位置(x,0),它的子控件也会跟随着滚动。
[java]  view plain copy
  1. new Handler().postDelayed(new Runnable() {  
  2.     @Override  
  3.     public void run() {  
  4.         // 水平直接滚动800px,如果想效果更平滑可以使用smoothScrollTo(int x, int y)  
  5.         hsv.scrollTo(8000);  
  6.     }  
  7. }, 2000);  

效果大致如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值