Android:快速更改图像(如动画)

时间:2011-03-25 09:16:17

标签: android image

当我在屏幕上拖动手指时,我想快速地拍摄10张不同的图像。图像存储在SD卡中。我使用此代码在屏幕上显示图像,但更改太慢。

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();
}

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();
    } 
}}

我基本上想要做的是制作类似动画的东西。那么如何让图像加载更快呢?

1 个答案:

答案 0 :(得分:1)

您可以等待加载完成&amp;只显示图像视图上的图像。为此删除语句image.setImageBitmap(b);从for循环中创建一个位图的arrayList来存储图像。这样你可以确保图像已经加载,因为你想要显示它们。

尝试以下:

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;
}

}