在app下面的build.gradle文件中引用第三方的类库:
buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } sourceSets { main { jniLibs.srcDirs = ['libs'] } } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' compile 'com.veinhorn.scrollgalleryview:library:1.0.2' }
在xml里面添加控件
<com.veinhorn.scrollgalleryview.ScrollGalleryView android:id="@+id/scrollView" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@color/colorAccent" />
在java代码里面添加:
public class PhotoActivity extends FragmentActivity { private ScrollGalleryView scrollGalleryView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_photo); scrollGalleryView = (ScrollGalleryView)findViewById(R.id.scrollView); scrollGalleryView .setThumbnailSize(100) .setZoom(true) .setFragmentManager(getSupportFragmentManager()) .addImage(R.mipmap.a1) .addImage(R.mipmap.a2) .addImage(R.mipmap.a3) .addImage(R.mipmap.a4) .addImage(R.mipmap.a2) .addImage(R.mipmap.a3) .addImage(convertDrawableToBitmap(R.mipmap.a4)); } private Bitmap convertDrawableToBitmap(int image) { return ((BitmapDrawable)getResources().getDrawable(image)).getBitmap(); } }
总结:
这是android studio的一个第三方的控件,需要导入库文件,本例子只是展示资源文件夹的图片,如果需要实现手机效果,需要获取手机相册的路径,然后使用addImage方法添加图片资源即可
运行效果截图: