2011-03-25 119 views
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(); 
    } 
}} 

什麼我基本上試圖做的就是像動畫的東西。那麼如何讓圖像加載更快?

回答

1

您可以等待加載完成&只顯示圖像視圖上的圖像。爲此刪除語句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; 
} 

}

+1

感謝。它似乎並沒有工作。只顯示一個圖像。以及如何解決'位圖超出虛擬機預算'的問題?當目錄中有3個以上的圖像時,出現錯誤 – 2011-03-28 07:05:15