我正嘗試使用horizontalscrollview做一個圖片庫的簡單示例並動態添加圖片。我搜索了一些例子,但大多數都太複雜。有一個簡單的例子來說明如何做到這一點?帶有水平滾動視圖的圖片庫
回答
Here是一些簡單的例子已經實施水平滾動視圖看起來像圖片庫
這將幫助你
添加HorizontalScrollView佈局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/mygallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
/>
</HorizontalScrollView>
</LinearLayout>
主要的Java代碼:
package com.example.androidhorizontalscrollviewgallery;
import java.io.File;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
LinearLayout myGallery;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myGallery = (LinearLayout)findViewById(R.id.mygallery);
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/test/";
Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
for (File file : files){
myGallery.addView(insertPhoto(file.getAbsolutePath()));
}
}
View insertPhoto(String path){
Bitmap bm = decodeSampledBitmapFromUri(path, 220, 220);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setLayoutParams(new LayoutParams(250, 250));
layout.setGravity(Gravity.CENTER);
ImageView imageView = new ImageView(getApplicationContext());
imageView.setLayoutParams(new LayoutParams(220, 220));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(bm);
layout.addView(imageView);
return layout;
}
public Bitmap decodeSampledBitmapFromUri(String path, 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(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
public int calculateInSampleSize(
BitmapFactory.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;
}
}
注意:在這個ex充足的情況下,即使不在屏幕上,HorizontalScrollView中的位圖也不會被刪除。所以如果加載的位圖太多,就會拋出java.lang.OutOfMemoryError的錯誤!
你能幫我在這[問題](http://stackoverflow.com/questions/33219942/horitzontalscrollview-or-carrousel)? :d –
你可以創建一個div並將溢出設置爲隱藏,然後嵌套另一個裏面的所有圖像的高度或寬度取決於你想要水平的動畫,以寬度乘以圖像的數量並記住在此包括邊際。然後,您編寫一個jQuery的動畫對大格將其移動到左側或右側再次運行根據自己的喜好
eg $('pic').animate({left:'_____ <- enter width of single image together with margin here ' +'px','slow' <-- whatever speed you prefer);
,然後用它從那裏玩。您的處理程序可以再次單擊另一個按鈕或圖像...偏好。它沒有太多的編碼是非常靈活的,它不是最好的選擇。如果這不適合你,那麼創建一個圖像數組並循環遍歷它們,而不是使用這些特定的尺寸進行滾動。祝你好運。希望我已經幫助你。
jquery不會在這裏幫助在android – roarster
- 1. 試圖有一個水平滾動視圖動態圖片
- 2. Android中的水平滾動圖片庫
- 3. 帶有圖片的在水平滾動視圖的位置問題
- 4. 水平滾動視圖
- 5. android水平滾動視圖
- 6. 水平滾動視圖iOS
- 7. 水平滾動頁面中的水平滾動視圖
- 8. 帶有可鎖定列和水平滾動的表視圖?
- 9. 在水平滾動視圖中替代水平滾動視圖在android
- 10. 水平滾動視圖繪製圖
- 11. 水平滾動圖像視圖列表
- 12. 具有expandablelistview父級水平滾動視圖子視頻庫
- 13. 滾動視圖與web視圖不滾動水平
- 14. 內部自定義視圖水平滾動視圖不滾動
- 15. 帶編輯文本的水平滑動滾動視圖
- 16. Javascript:水平圖像滾動條/帶有字幕的滑塊等每張圖片
- 17. android:chipview上的水平滾動視圖
- 18. ListView項中的水平滾動視圖?
- 19. ViewPager內部的水平滾動視圖
- 20. 網格視圖中的水平滾動
- 21. 水平滾動的自定義視圖
- 22. 手機中的水平滾動視圖
- 23. iOS中的水平滾動視圖?
- 24. 水平滾動視圖迅速的Xcode
- 25. 表/集合視圖的水平滾動
- 26. 水平滾動的jQuery圖庫
- 27. 水平圖像視圖庫
- 28. 帶有TabLayout的水平日曆視圖?
- 29. 水平滾動視圖中的圖像視圖?
- 30. 滾動查看滾動視圖時的水平滾動
發佈您的代碼,如果您遇到困難,那麼我們可以解決問題,我們不在這裏工作的任何方式。 –