1
我想在屏幕上拖動手指時快速地顯示10張不同的圖像。圖像存儲在SD卡中。我使用此代碼在屏幕上顯示圖像,但更改太慢。Android:快速更改圖像(如動畫)
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();
}
}}
什麼我基本上試圖做的就是像動畫的東西。那麼如何讓圖像加載更快?
感謝。它似乎並沒有工作。只顯示一個圖像。以及如何解決'位圖超出虛擬機預算'的問題?當目錄中有3個以上的圖像時,出現錯誤 – 2011-03-28 07:05:15