Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从库中动态卸载图像?_Android - Fatal编程技术网

Android 如何从库中动态卸载图像?

Android 如何从库中动态卸载图像?,android,Android,我有自定义图像视图: 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; publi

我有自定义图像视图:

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();
    }
}   
}
图像很大,当应用程序读取很多图像时,我的内存就会耗尽。我想从内存中删除所有不可见的图像,例如:

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

图像1-3和9-10是不必要的-所以我想释放内存。如何操作?

我也遇到了同样的问题,您需要做的是创建一个用于gallery的自定义适配器,并使用getView函数循环使用位图:

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");
        }
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");
        }