Android: Changing images rapidly (like animation)
I want to diplsay 10 different images rapidly when I drag my finger on the screen. The images are stored in the sdcard. Im using this code to display the images on the screen, but the change is too slow.
public class ImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.image);
File images = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File[] imagelist = images.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name) {
return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
}
});
mFiles = new String[imagelist.length];
for(int i= 0 ; i< imagelist.length; i++) {
mFiles[i] = imagelist[i].toString();
}
getImageFromSDCard()开发者_StackOverflow中文版;
}
public void getImageFromSDCard() {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
try {
if(mFilePosition < mFiles.length) {
Log.d("", mFiles[mFilePosition]);
Bitmap b = BitmapFactory.decodeFile(mFiles[mFilePosition], null);
image.setImageBitmap(b);
mFilePosition = 0;
}
} catch (Exception e) {
//Log error
}
b = null;
System.gc();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
getImageFromSDCard();
}
}}
What Im basically trying to do is make something like that of an animation. So how do I make the images load faster?
You can wait for the loading to complete & only the display the images on the image view . For this remove the statement image.setImageBitmap(b); from the for loop and create an arrayList of bitmaps to store the images..this way u can ensure that images have loaded befor u want to display them.
try following:
public class Pics extends Activity {
private ImageView image;
private String[] mFiles;
private int mFilePosition = 0;
private ArrayList<Bitmap> bitArray;
private Bitmap b;
private int i;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.image);
bitArray = new ArrayList<Bitmap>();
File images = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
try {
images.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e("file path", images.getAbsolutePath());
File[] imagelist = images.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return ((name.endsWith(".jpg")) || (name.endsWith(".png")));
}
});
mFiles = new String[imagelist.length];
Log.e("size", imagelist.length + "");
for (int i = 0; i < imagelist.length; i++) {
mFiles[i] = imagelist[i].toString();
}
getImageFromSDCard();
}
public void getImageFromSDCard() {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
try {
for (int j = 0; j < mFiles.length; j++) {
Log.d("", mFiles[mFilePosition]);
b = BitmapFactory.decodeFile(mFiles[j], null);
bitArray.add(b);
}
} catch (Exception e) {
// Log error
}
b = null;
System.gc();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
MathHelper.showToast(getApplicationContext(), bitArray.size() + "");
for (i = 0; i < bitArray.size()-1; i++) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() { // TODO Auto-generated method stub
image.setImageBitmap(bitArray.get(i));
}
}, 1000);
}
}
return true;
}
}
精彩评论