Android中HorizontalScrollView使用方法詳解
由于移動(dòng)設(shè)備物理顯示空間一般有限,不可能一次性的把所有要顯示的內(nèi)容都顯示在屏幕上。所以各大平臺(tái)一般會(huì)提供一些可滾動(dòng)的視圖來(lái)向用戶(hù)展示數(shù)據(jù)。Android平臺(tái)框架中為我們提供了諸如ListView、GirdView、ScrollView等滾動(dòng)視圖控件,這幾個(gè)視圖控件也是我們平常使用最多的。下面介紹一下HorizontalScrollView的使用和需要注意的點(diǎn):
HorizontalScrollView是一個(gè)FrameLayout ,這意味著你只能在它下面放置一個(gè)子控件,這個(gè)子控件可以包含很多數(shù)據(jù)內(nèi)容。有可能這個(gè)子控件本身就是一個(gè)布局控件,可以包含非常多的其他用來(lái)展示數(shù)據(jù)的控件。這個(gè)布局控件一般使用的是一個(gè)水平布局的LinearLayout 。TextView也是一個(gè)可滾動(dòng)的視圖控件,所以一般不需要HorizontalScrollView
下面介紹一個(gè)HorizontalScrollView中包含許多圖片,并且可以滾動(dòng)瀏覽的示例
@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目錄下,上面的示例程序只是顯示了一張張大圖片的縮略版本,對(duì)這方面不懂的可以參看:
HorizontalScrollView還可以設(shè)置滾動(dòng)到一個(gè)指定的位置(x,0),它的子控件也會(huì)跟隨著滾動(dòng)。
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// 水平直接滾動(dòng)800px,如果想效果更平滑可以使用smoothScrollTo(int x, int y)
hsv.scrollTo(800, 0);
}
}, 2000);
效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。
- Android自定義HorizontalScrollView實(shí)現(xiàn)qq側(cè)滑菜單
- Android HorizontalScrollView左右滑動(dòng)效果
- Android UI系列-----ScrollView和HorizontalScrollView的詳解
- Android HorizontalScrollView內(nèi)子控件橫向拖拽實(shí)例代碼
- Android利用HorizontalScrollView仿ViewPager設(shè)計(jì)簡(jiǎn)單相冊(cè)
- Android自定義HorizontalScrollView打造超強(qiáng)Gallery效果
- Android使用自定義控件HorizontalScrollView打造史上最簡(jiǎn)單的側(cè)滑菜單
- Android中實(shí)現(xiàn)多行、水平滾動(dòng)的分頁(yè)的Gridview實(shí)例源碼
- android listview 水平滾動(dòng)和垂直滾動(dòng)的小例子
- HorizontalScrollView水平滾動(dòng)控件使用方法詳解
相關(guān)文章
解決android.support.v4.content.FileProvide找不到的問(wèn)題
這篇文章主要介紹了解決android.support.v4.content.FileProvide找不到的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android Studio中配置OpenCV庫(kù)開(kāi)發(fā)環(huán)境的教程
這篇文章主要介紹了Android Studio中配置OpenCV庫(kù)開(kāi)發(fā)環(huán)境的教程,OpenCV有Java接口,因而也經(jīng)常被用來(lái)做安卓開(kāi)發(fā),需要的朋友可以參考下2016-05-05
Android ViewPager的MVP架構(gòu)搭建過(guò)程
本文主要介紹了ViewPager在Android中的作用以及使用場(chǎng)景,如引導(dǎo)頁(yè)、圖片瀏覽器、新聞或文章內(nèi)容的多標(biāo)簽頁(yè)等,同時(shí),還詳細(xì)闡述了如何通過(guò)MVP架構(gòu)來(lái)搭建ViewPager,將視圖和邏輯進(jìn)行解耦,提高代碼的可測(cè)試性、可復(fù)用性,使代碼結(jié)構(gòu)更清晰且易于擴(kuò)展功能2024-10-10
淺談RecyclerView(完美替代ListView,GridView)
RecyclerView絕對(duì)是一款功能強(qiáng)大的控件,涵蓋了ListView,GridView,瀑布流等數(shù)據(jù)表現(xiàn)的形式。本文對(duì)其進(jìn)行系統(tǒng)介紹,有需要的朋友可以看下2016-12-12
Gradle?Build?Cache引發(fā)的Task緩存編譯問(wèn)題
這篇文章主要為大家介紹了Gradle?Build?Cache引發(fā)的Task緩存編譯問(wèn)題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android 實(shí)現(xiàn)銀聯(lián)刷卡機(jī)消費(fèi)后手動(dòng)簽名的功能(示例代碼)
在一些商場(chǎng)購(gòu)物時(shí),不需要用筆在銀聯(lián)機(jī)上簽名了,直接用手指觸摸實(shí)現(xiàn)消費(fèi)簽名,非常方便,下面小編給大家分享Android 實(shí)現(xiàn)銀聯(lián)刷卡機(jī)消費(fèi)后手動(dòng)簽名的功能,需要的朋友參考下吧2017-12-12
android ListActivity顯示圖標(biāo)實(shí)例
在ListActivity中顯示圖標(biāo),好像并不復(fù)雜,實(shí)現(xiàn)起來(lái)卻不輕松,我們下面一步步來(lái)實(shí)現(xiàn)ListActivity中顯示圖標(biāo)2013-11-11

