所以基本上我有5个imageViews和5个文件。我想将这些文件分配给每个imageView,而不是每次都输入代码 - 这样效率很低。
我有这个atm:
if(i==1){
File imageFile = new File(path+files.get(i));
checkOrientation(imageFile);
if(files.get(i).endsWith("mp4")) {
Bitmap bMap = ThumbnailUtils.createVideoThumbnail(imageFile.getAbsolutePath(),
MediaStore.Video.Thumbnails.MINI_KIND);
imageView2.setImageBitmap(bMap);
} else {
imageView2.setImageURI(null);
imageView2.setImageURI(Uri.parse(path + files.get(i)));
imageView2.setRotation(rotate);
}
imageView2.invalidate();
Log.d("onCreate", "imageView2 set: " + path+files.get(i) + " rotation: " + rotate);
} if(i==2){
File imageFile = new File(path+files.get(i));
checkOrientation(imageFile);
if(files.get(i).endsWith("mp4")){
Bitmap bMap = ThumbnailUtils.createVideoThumbnail(imageFile.getAbsolutePath(),
MediaStore.Video.Thumbnails.MINI_KIND);
imageView3.setImageBitmap(bMap);
} else {
imageView3.setImageURI(null);
imageView3.setImageURI(Uri.parse(path + files.get(i)));
imageView3.setRotation(rotate);
}
imageView3.invalidate();
Log.d("onCreate", "imageView3 set: " + path+files.get(i) + " rotation: " + rotate);
}
有没有办法可以创建一个使用此代码的方法,但是根据输入参数编辑了正确的imageView? 例如:
public void setImageView(int view, int i, File imageFile){
if(files.get(i).endsWith("mp4")) {
Bitmap bMap = ThumbnailUtils.createVideoThumbnail(imageFile.getAbsolutePath(),
MediaStore.Video.Thumbnails.MINI_KIND);
imageView(view).setImageBitmap(bMap);
} else {
imageView(view).setImageURI(null);
imageView(view).setImageURI(Uri.parse(path + files.get(i)));
imageView(view).setRotation(rotate);
}
imageView(view).invalidate();
Log.d("onCreate", "imageView set: " + path+files.get(i) + " rotation: " + rotate);
}
这有可能吗?我试着像上面那样编写代码,但它不喜欢imageView(view)。
我只是想避免重复编写相同的代码行。
谢谢,
凯西
答案 0 :(得分:0)
在看到@SpringBreaker的回复后,我写了这段代码:
setimageView(imageView1, i, imageFile);
private void setimageView(ImageView imageView, int i, File imageFile) {
if(files.get(i).endsWith("mp4")){
Bitmap bMap = ThumbnailUtils.createVideoThumbnail(imageFile.getAbsolutePath(),
MediaStore.Video.Thumbnails.MINI_KIND);
imageView.setImageBitmap(bMap);
} else {
imageView.setImageURI(null);
imageView.setImageURI(Uri.parse(path + files.get(i)));
imageView.setRotation(rotate);
}
imageView.invalidate();
Log.d("onCreate", "imageView set: " + path+files.get(i) + " rotation: " + rotate);
}