Android中HorizontalScrollView使用方法詳解_Android

來源:互聯網
上載者:User

由於行動裝置物理顯示空間一般有限,不可能一次性的把所有要顯示的內容都顯示在螢幕上。所以各大平台一般會提供一些可滾動的視圖來向使用者展示資料。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軟體編程有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.