public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private ImageView imgView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
imgView1 = (ImageView) findViewById(R.id.imgView1);
}
public void gifClick(View view){
//assets下文件夹的名字
String folderName = "a";
//获取assets里的图片,然后判断
AssetManager assetManager = getAssets();
String[] names = showImage(assetManager, folderName);
Log.d(TAG, "1111111111onCreate: " + Arrays.asList(names).toString());
if (names == null || names.length <= 0) return;
if (names[0].toLowerCase().contains(".gif")) {
Glide.with(this).load(getString(R.string.asset_path) + folderName + "/" + names[0]).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imgView1);
} else if (names[0].toLowerCase().contains(".png") || names[0].toLowerCase().contains(".jpg")) {
Glide.with(this).load(getString(R.string.asset_path) + folderName + "/" + names[0]).into(imgView1);
}
}
public void pngClick(View view){
String folderName = "b";
AssetManager assetManager = getAssets();
String[] names = showImage(assetManager, folderName);
Log.d(TAG, "1111111111onCreate: " + Arrays.asList(names).toString());
if (names == null || names.length <= 0) return;
if (names[0].toLowerCase().contains(".gif")) {
Glide.with(this).load(getString(R.string.asset_path) + folderName + "/" + names[0]).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imgView1);
} else if (names[0].toLowerCase().contains(".png") || names[0].toLowerCase().contains(".jpg")) {
Glide.with(this).load(getString(R.string.asset_path) + folderName + "/" + names[0]).into(imgView1);
}
}
/**
* @param assetManager
* @param folderName
* @return
*/
private String[] showImage(AssetManager assetManager, String folderName) {
try {
return assetManager.list(folderName);
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "查找" + folderName + "失败:" + e);
return null;
}
}
}
//图片显示插件
compile 'com.github.bumptech.glide:glide:3.7.0'