最近在做一个应用,要循环显示图片,可是图片很大,每个5M左右,如果按图片原来大小显示,在显示两个图片之后必会内存溢出,谁能给个图片压缩显示方法,谢谢!!!
------解决方案--------------------
........5M
这个就算recycle也没用了...
我觉得你可以这样,创建一张全局的bitmap(当然要按最大的创建),然后每个图片读取都按照字节的方式读出来,之后set个体这个全局的bitmap,显示的时候就显示这个全局的,一个用完就清空一下,在读取另一张,这样保证只有一个bitmap在占用内存....
------解决方案--------------------
我有图片压缩的一个类你看看哈:
- Java code
import com.xmobileapp.pictureviewer.R;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.BlurMaskFilter;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.os.Handler;import android.util.AttributeSet;import android.view.GestureDetector;import android.view.KeyEvent;import android.view.MotionEvent;import android.view.View;import android.view.Window;import android.view.GestureDetector.SimpleOnGestureListener;import android.widget.Scroller;public class PictureShow extends Activity { private PictureShowUtils mAllImages = new PictureShowUtils(); boolean mLayoutComplete; boolean mPausing = false; PictureSlideView mPictureSlideView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); mPictureSlideView = (PictureSlideView) findViewById(R.id.grid); mPictureSlideView.requestFocus(); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { return super.onKeyUp(keyCode, event); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { int sel = mPictureSlideView.mSelImageIndex; int count = mAllImages.getCount(); switch (keyCode) { case KeyEvent.KEYCODE_DPAD_UP: if (sel >= 1) { sel--; mPictureSlideView.ScrollUp(); } break; case KeyEvent.KEYCODE_DPAD_DOWN: if (sel < count - 1) { sel++; mPictureSlideView.scrollDown(); } break; default: break; } mPictureSlideView.mSelImageIndex = sel; mPictureSlideView.mImageBlockManager.loadSelBlock(sel); mPictureSlideView.invalidate(); return super.onKeyDown(keyCode, event); } @Override public void onPause() { super.onPause(); mPausing = true; } @Override public void onResume() { super.onResume(); mPausing = false; } public static class PictureSlideView extends View { // 布局信息 class LayoutSpec { LayoutSpec(int cols, int w, int h, int leftEdgePadding, int rightEdgePadding, int intercellSpacing) { mColumns = cols; mCellWidth = w; mCellHeight = h; mLeftEdgePadding = leftEdgePadding; mRightEdgePadding = rightEdgePadding; mCellSpacing = intercellSpacing; } int mColumns; int mCellWidth, mCellHeight; int mLeftEdgePadding, mRightEdgePadding; int mCellSpacing; }; private class ImageBlockManager { private int mBlockCacheFirstBlockNumber = 0; private int mBlockCacheStartOffset = 0; //缩略图缓存 private ImageBlock[] mBlockCache; //选定的缩略图对象 private ImageBlock mSelBlock; //一屏上缩略图的行数 private static final int sRowsPerPage = 4; //缩略图缓存大小参数 private static final int sPagesPreCache = 2; private static final int sPagesPostCache = 2; // 放大预览图的尺寸 private static final int sSelBlockWidth = 200; private static final int sSelBlockHeight = 200; private Bitmap mMissingImageThumbnailBitmap; ImageBlockManager(Context context) { mContext = context; mBlockCache = new ImageBlock[sRowsPerPage * (sPagesPreCache + sPagesPostCache + 1)]; for (int i = 0; i < mBlockCache.length; i++) { mBlockCache[i] = new ImageBlock(); } /* 读取图片 */ loadBlocks(); mSelBlock = new ImageBlock(sSelBlockWidth, sSelBlockHeight); } int blockHeight() { return mCurrentSpec.mCellSpacing + mCurrentSpec.mCellHeight + mCurrentSpec.mCellSpacing; } int blockWidth() { return mCurrentSpec.mCellSpacing + mCurrentSpec.mCellWidth + mCurrentSpec.mCellSpacing; } void loadSelBlock(int pos) { mSelBlock.loadSelImage(pos); } private void paintSelBlock(Canvas canvas) { int pos = mSelImageIndex; if (pos < 0) return; Bitmap b = mSelBlock.mBitmap; if (b == null) { return; } int xPos = 0; int yPos = 0; yPos = mScrollY + mCurrentSpec.mCellSpacing; xPos = mCurrentSpec.mCellWidth + mCurrentSpec.mLeftEdgePadding; canvas.drawBitmap(b, xPos, yPos, mGridViewPaint); } // 获取代表“没有缩略图”的图片对象 public Bitmap getErrorBitmap() { mMissingImageThumbnailBitmap = BitmapFactory.decodeResource( PictureSlideView.this.getResources(), R.drawable.nothumbnail); return mMissingImageThumbnailBitmap; }