Because the physical display space of mobile devices is generally limited, it is impossible to display all the content to be displayed on the screen at a time. Therefore, major platforms generally provide some scrollable views to present data to users. The Android platform framework provides scroll view controls such as listview, girdview, and scrollview. These View Controls are also the most commonly used. The following describes how to use horizontalscrollview and what you need to note: horizontalscrollview is a framelayout
,
This means that you can only place a child control under itThis sub-control can contain a lot of data content. It is possible that this sub-control itself is a layout control, which can contain many other controls used to display data. This layout control generally uses a horizontal layout.
LinearLayout. Textview is also a scrollable view control, so generally there is no need for horizontalscrollview. The following describes an example of a horizontalscrollview that contains many images and can be browsed by scrolling.
@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; }
The image to be displayed is placed in the "test" directory of the external sdcard. The preceding sample program only shows the scaling version of a large image, for this do not understand can refer to the http://blog.csdn.net/tibib/article/details/8726486 horizontalscrollview can also be set to scroll to a specified position (x, 0), its child control will also scroll.
New handler (). postdelayed (New runnable () {@ overridepublic void run () {// scroll directly to 800px horizontally. To make the effect smoother, you can use smoothscrollto (int x, int y) HSV. scrollto (800, 0) ;}}, 2000 );
The effect is roughly as follows:
References http://developer.android.com/reference/android/widget/HorizontalScrollView.htmlhttp://android-er.blogspot.com/2012/07/implement-gallery-like.html