如何从画廊的动态影像卸载? [英] How to dynamic unload images from gallery?

查看:94
本文介绍了如何从画廊的动态影像卸载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自定义的ImageView:

I have custom ImageView:

public class ShadowedImageView extends ImageView {

private Paint mPaint;
public Bitmap bitmap = null;
private Bitmap tempBitmap;
private Bitmap thumb;
private Bitmap image;
public float shadowRadius = 2f;
public float shadowDx = 1f;
public float shadowDy = 1f;
public int shadowColor = Color.BLACK;
public int scale = 1;
public int requiredWidth = 768;
public int requiredHeight = 1024;
public int loaded = 0;

public ShadowedImageView(Context context) {
    super(context);
    setShadow();
    loaded = 0;
}

public ShadowedImageView(Context context, AttributeSet attrs){
    super(context, attrs);
    setShadow();
    loaded = 0;
}

public void setShadow(){
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setShadowLayer(shadowRadius, shadowDx, shadowDy, shadowColor);
    Resources res = getResources();
    Drawable drawable = res.getDrawable(R.drawable.pl);
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pl);
    setImageDrawable(drawable);
}

public void refreshShadow(){
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setShadowLayer(shadowRadius, shadowDx, shadowDy, shadowColor);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
}

@Override
protected void onMeasure(int w, int h) {
    super.onMeasure(w,h);

    int mH, mW;
    mW = getSuggestedMinimumWidth() < getMeasuredWidth()? getMeasuredWidth() : getSuggestedMinimumWidth();
    mH = getSuggestedMinimumHeight() < getMeasuredHeight()? getMeasuredHeight() : getSuggestedMinimumHeight();
    setMeasuredDimension(mW + 5, mH + 5);

}

public void downloadThumb(final Publication pub){
    OnClickListener theCommonListener = new OnClickListener(){
        @Override
        public void onClick(View v) {               
            Intent intent = new Intent(v.getContext(), PublicationReader.class);
            intent.putExtra("pubUrl", pub.publicationUrl);
            intent.putExtra("pubPages", pub.publicationPages);
            intent.putExtra("pubId", pub.id);

            v.getContext().startActivity(intent);
        }
    };
    this.setOnClickListener(theCommonListener);
    download(pub.thumbImageUrl);
}

public void download(String fileUrl){
    downloadTask t = new downloadTask();
    t.loadData(fileUrl, this);
    t.execute();
}

private class downloadTask extends AsyncTask<String, Void, Long> {
    String fileUrl;
    ShadowedImageView imView;

    public void loadData(String u, ShadowedImageView i) {
        fileUrl = u;
        imView = i;
    }

    @Override
    protected Long doInBackground(String... params) {
        try{
            Bitmap bmImg = null;
            URL myFileUrl = null;
            try {
                if(fileUrl!=null){
                    myFileUrl= new URL(fileUrl);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            try {
                if(myFileUrl!=null){
                    HttpURLConnection conn=(HttpURLConnection)myFileUrl.openConnection();
                    conn.setDoInput(true);
                    conn.connect();
                    InputStream is = conn.getInputStream();
                    decodeStream(is);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Long result){
        setImageBitmap(bitmap);
        invalidate();

    }
}

private void decodeStream(InputStream is){
    try {
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        bitmap = BitmapFactory.decodeStream(is, null, o2);
        refreshShadow();
        loaded = 1;
        invalidate();
    } catch (Exception e) {
        e.printStackTrace();
    }
}   
}

和适配器画廊:

public class AddImgAdp extends BaseAdapter {
    int GalItemBg;
    private Context cont;

    private ShadowedImageView[] Imgid;

    public AddImgAdp(Context c) {
        Imgid = pagesArray;
        cont = c;
        TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
        GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        typArray.recycle();
    }

    public int getCount() {
        return pagesArray.length;
    }

    public Object getItem(int position) {
        return pagesArray[position];
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ShadowedImageView imgView = pagesArray[position];
        pagesArray[position].scale = 1;
        pagesArray[position].download(URL_TO_LOAD_IMAGE);

        DisplayMetrics dm = new DisplayMetrics();

        imgView.setScaleType(ImageView.ScaleType.FIT_XY);
        imgView.setBackgroundResource(GalItemBg);
        return imgView;
    }
}

图片大,当应用程序读取他们中的很多,我得到了内存。我想删除内存中的所有不可见图像,例如:

Images is big and when app reads a lot of them, I get out of memory. I want to delete all invisible images from memory, for example:

                +-------------+
                |    screen   |
[1] [2] [3] [4] | [5] [6] [7] | [8] [9] [10]
                |             |
                +-------------+

图像1-3和9-10是不必要的 - 所以我想释放内存。怎么办呢?

images 1-3 and 9-10 are unnecessary - so I want to release memory. How to do it?

推荐答案

我有同样的问题,你需要做的,是建立在画廊中使用自定义适配器,并使用getView函数回收位图

I had the same issue, what you need to do, is to create a Custom adapter used with the gallery, and to use the getView function to recycle to bitmaps :

public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(this.myContext);
        Bitmap tempBitmap = null;

        //Check if my PhotoList at this position is not null...

        //Free previous photos
        if ((position-3)>=0 && listBitmap.size()-3 >=0 && listBitmap(position-3) != null){
            //Here is to free for 1 photo, you can free more ... (3 in your case)
            listBitmap(position-3).recycle();
            listBitmap(position-3).setBitmap(null);
            Log.w(TAG, "Delete Photo-3");
        }

这篇关于如何从画廊的动态影像卸载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆