+ -
当前位置:首页 → 问答吧 → 如果在在不同的Activity 各开启一个Gallery 记忆体释放 oom

如果在在不同的Activity 各开启一个Gallery 记忆体释放 oom

时间:2011-10-02

来源:互联网

我的情况是

我有两个页面分别为 A & B

进入顺序是 程式开启→ A page → 点击A page 上的一个button → B page

→ 在B page上按返Button finish掉B → A page

这两个页面都会有一个Gallery来呈现图片

由於我不想把 B page 关掉后, 重新读取 A page的图片

所以在页面转移到 B page的时候 我并没有对 A page 做finish()的动作

问题出现在记忆体释放

在我重复尝试

A → B

B → A

的过程中, 我发现虽然我使用了finish指令 结束掉B

来回的过程记忆体还是越吃越大最后就会 OOM

因为要呈现画廊有使用
ImageAdapter extends BaseAdapter
GalleryFlow extends Gallery

这两个类别 请问我该怎么做才能在关掉B的同时释放记忆体?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
 
import android.content.Context;
//import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
//import android.widget.ImageView.ScaleType;
 
public class ImageAdapter extends BaseAdapter {
  int mGalleryItemBackground;
  // 接收存放URL阵列之字串阵列
  private String[] strImageURL;
  private Context mContext;
  // URL转成图片后存放View的阵列
  HashMap<Integer,ImageView> dataCache = new HashMap<Integer,ImageView>();
 
  public ImageAdapter(Context context, String[] ImageIds) {
    mContext = context;
    strImageURL = ImageIds;
    
  }
  /*一定要覆写的方法getView,传回一View物件*/
  public View getView(int position, View convertView, ViewGroup parent) {
    final int reflectionGap = 0;
    /*产生ImageView物件*/
    ImageView imageView = new ImageView(mContext);
      try{
     // new URL物件将网址传入 
        // 无限循环-第1点改进, 通过取余数来循环
     // 非 无限循环-原码如下
     //URL aryURI = new URL(strImageURL[position]);
     URL aryURI = new URL(strImageURL[position%strImageURL.length]);
          // 取得连线 
     URLConnection conn = aryURI.openConnection();
     conn.connect();
     // 取得回传的InputStream
     InputStream is = conn.getInputStream();
     // 将InputStream变成Bitmap
     Bitmap originalImage = BitmapFactory.decodeStream(is);
     // 关闭InputStream
     is.close();
     // 设定Bitmap於ImageView中
     imageView.setImageBitmap(originalImage);
          // 以下开始处理图片
     int width = originalImage.getWidth();
     int height = originalImage.getHeight();
 
     Matrix matrix = new Matrix();
     // 图片影子
     matrix.preScale(1, -1);
     // 图片影子大小
     Bitmap reflectionImage = Bitmap.createBitmap(originalImage,
         0, height/2, width, height/2, matrix, false);
     // 图片大小
     Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
          (height + height / 2), Config.ARGB_8888);
 
     Canvas canvas = new Canvas(bitmapWithReflection);
     // 影子的宽
     canvas.drawBitmap(originalImage, 0, 0, null);
 
     Paint deafaultPaint = new Paint();
     canvas.drawRect(0, height, width, height + reflectionGap,
          deafaultPaint);
 
     canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
 
     Paint paint = new Paint();
     LinearGradient shader = new LinearGradient(0,
         originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
         + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
 
     paint.setShader(shader);
 
     paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
 
     canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
          + reflectionGap, paint);
 
      
     imageView.setImageBitmap(bitmapWithReflection);
     // 布景大小
     imageView.setLayoutParams(new GalleryFlow.LayoutParams(200, 320));
     // 图片皆同size (会切图)
     //imageView.setScaleType(ScaleType.MATRIX);
     dataCache.put(position, imageView);
             } catch (IOException e){
 
          e.printStackTrace();
        }
    return dataCache.get(position);
  }
  
 
  /*private Resources getResources() {
  // TODO Auto-generated method stub
  return null;}*/
  
  public int getCount() {
    // 无限循环-第2点改进, 传回一个很的数值,例如,Integer.MAX_VALUE
    return Integer.MAX_VALUE;
    // 非 无限循环-原码应如下
    //return strImageURL.length;
  }
  /*一定要覆写的方法getItemId,传回position*/
  public Object getItem(int position) {
    return position;
  }
  /*一定要覆写的方法getItem,传回position*/
  public long getItemId(int position) {
    return position;
  }
  
  /* 依据距离中央的位移量 利用getScale回传Views的大小(0.0f to 1.0f) */
  public float getScale(boolean focused, int offset) {
    return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
  }
 
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;
import android.widget.ImageView;
public class GalleryFlow extends Gallery {
  private Camera mCamera = new Camera(); private int mMaxRotationAngle = 60; private int mMaxZoom = -120; private int mCoveflowCenter; public GalleryFlow(Context context) { super(context); this.setStaticTransformationsEnabled(true); }
  public GalleryFlow(Context context, AttributeSet attrs) { super(context, attrs); this.setStaticTransformationsEnabled(true); }
  public GalleryFlow(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.setStaticTransformationsEnabled(true); }
  public int getMaxRotationAngle() { return mMaxRotationAngle; }
  public void setMaxRotationAngle(int maxRotationAngle) { mMaxRotationAngle = maxRotationAngle; }
  public int getMaxZoom() { return mMaxZoom; }
  public void setMaxZoom(int maxZoom) { mMaxZoom = maxZoom; }
  private int getCenterOfCoverflow() { return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft(); } //显示视窗大小 private static int getCenterOfView(View view) { return view.getLeft() + view.getWidth() / 2; }
  protected boolean getChildStaticTransformation(View child, Transformation t) {
  final int childCenter = getCenterOfView(child); final int childWidth = child.getWidth(); int rotationAngle = 0;
  t.clear(); t.setTransformationType(Transformation.TYPE_MATRIX);
  if (childCenter == mCoveflowCenter) { transformImageBitmap((ImageView) child, t, 0); } else { rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle); if (Math.abs(rotationAngle) > mMaxRotationAngle) { rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle; } transformImageBitmap((ImageView) child, t, rotationAngle); }
  return true; }
  protected void onSizeChanged(int w, int h, int oldw, int oldh) { mCoveflowCenter = getCenterOfCoverflow(); super.onSizeChanged(w, h, oldw, oldh); }
  private void transformImageBitmap(ImageView child, Transformation t, int rotationAngle) { mCamera.save(); final Matrix imageMatrix = t.getMatrix(); final int imageHeight = child.getLayoutParams().height; final int imageWidth = child.getLayoutParams().width; final int rotation = Math.abs(rotationAngle);
  // 图片的上边界
   // 在Z轴上正向移动camera的视角,实际效果为放大图片

作者: polo54786   发布时间: 2011-10-02

可能得看一下你Activity怎么写的

看起来你adapter跟gallery没有leak的状况

所以可能是Activity内有点问题?

另外提供一个方式可能有点花时间但是也能让你找到问题
使用Eclipse DDMS有个符号可以dump hprof file,
你帮Eclipse装上memory analyzer http://www.eclipse.org/mat/
就可以蛮方便帮你检查物件间的关系..

koji

作者: koji   发布时间: 2011-10-02

热门下载

更多