第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

求解 使用 LruCache緩存 后出現(xiàn)的問題

使用 LruCache緩存 后 第一屏圖片加載 不出來 ,來回滑動(dòng)后 圖片才能正常顯示 請(qǐng)問這是什么原因呢?

new ImageLoder().showImageByAsyncTask(viewHolder.ivIcon,url);?

替換成成

loder.showImageByAsyncTask(viewHolder.ivIcon,url);

出現(xiàn)的問題么

正在回答

2 回答

?private?class?ImgNetByAsyncTask?extends?AsyncTask<String,?Void,?Bitmap>{

?????????

????????private?ImageView?tImageView;

?????????private String?mUrl;

????????public?ImgNetByAsyncTask(ImageView?imageview,String?url){

????????????tImageView=imageview;

????????????mUrl=url;

????????}

? 少定義了一個(gè) url??private String?mUrl;


0 回復(fù) 有任何疑惑可以回復(fù)我~
這個(gè)是加載圖片的代碼

package?com.example.asynctask2;

import?java.io.BufferedInputStream;
import?java.io.IOException;
import?java.io.InputStream;
import?java.net.HttpURLConnection;
import?java.net.URL;
import?java.net.URLConnection;

import?android.annotation.SuppressLint;
import?android.graphics.Bitmap;
import?android.graphics.BitmapFactory;
import?android.os.AsyncTask;
import?android.os.Handler;
import?android.os.Message;
import?android.util.Log;
import?android.util.LruCache;
import?android.widget.ImageView;

?
@SuppressLint("NewApi")
public?class?ImageLoder?{
	private?ImageView?mImageView;
	
	//圖片緩存
	private?LruCache<String,Bitmap>?mcache;
????private?String?mUrl;
?
????public??ImageLoder(){
????	//獲取最大可用內(nèi)存
????	int?maxMemory=(int)?Runtime.getRuntime().maxMemory();
????	int?cacheSize=maxMemory/4;
????	//Log.i("maxMemory",""+cacheSize);
????	mcache?=?new?LruCache<String,?Bitmap>(cacheSize){??
????		??
?????????????//必須重寫此方法,來測(cè)量Bitmap的大小??
?????????????@Override??
?????????????protected?int?sizeOf(String?key,?Bitmap?value)?{??
?????????????????return?value.getRowBytes();??
?????????????}??
?????????????
????	};
????}
????
????/**?
?????*?添加Bitmap到內(nèi)存緩存?
?????*?@param?key?
?????*?@param?bitmap?
?????*/??
????public?void?addBitmapToMemoryCache(String?key,?Bitmap?bitmap)?{????
????????if?(getBitmapFromMemCache(key)?==?null?&&?bitmap?!=?null)?{????
????????	mcache.put(key,?bitmap);????
????????}????
????}????
???????
????/**?
?????*?從內(nèi)存緩存中獲取一個(gè)Bitmap?
?????*?@param?key?
?????*?@return?
?????*/??
????public?Bitmap?getBitmapFromMemCache(String?key)?{????
????????return?mcache.get(key);????
????}???
????

	Handler?handler=new?Handler(){
		public?void?handleMessage(android.os.Message?msg)?{
			super.handleMessage(msg);
			if(mImageView.getTag().equals(mUrl)){
				mImageView.setImageBitmap((Bitmap)?msg.obj);
			}
			
		};
	};
	
	public?void?showImageByThread(ImageView?imageview,final?String?url){
		
		mImageView=imageview;
		mUrl=url;
		new?Thread(){
			
			public?void?run()?{
				super.run();
			????Bitmap?bitmap=getBitmapFromURL(url);
			????Message?msg=Message.obtain();
			????msg.obj=bitmap;
			????handler.sendMessage(msg);
			}
		}.start();
	}
	
	/**
	?*?使用異步任務(wù)獲取網(wǎng)絡(luò)圖片
	?*?@param?imageview
	?*?@param?url
	?*/
	public?void?showImageByAsyncTask(ImageView?imageview,final?String?url){
		Bitmap?bitmap?=?getBitmapFromMemCache(url);??
		if(bitmap==null){
			new?ImgNetByAsyncTask(imageview,url).execute(url);
		}else{
			imageview.setImageBitmap(bitmap);?
		}
?	
	}
?	
	private?class?ImgNetByAsyncTask?extends?AsyncTask<String,?Void,?Bitmap>{
????????
		private?ImageView?tImageView;
		
		public?ImgNetByAsyncTask(ImageView?imageview,String?url){
			tImageView=imageview;
			mUrl=url;
		}
?
		protected?Bitmap?doInBackground(String...?params)?{
			String?url=params[0];
			Bitmap?bitmap?=?getBitmapFromURL(url);
			
????????????if(bitmap!=null){
????????????	addBitmapToMemoryCache(url,bitmap);
????????????}?
????????????return?bitmap;??
		}
		
????????protected?void?onPostExecute(Bitmap?bitmap)?{??
??????????????super.onPostExecute(bitmap);???
??????????????if(tImageView.getTag().equals(mUrl)){
????????????	??tImageView.setImageBitmap(bitmap);??
??			??}
????????}?

	}
	
?
	public?Bitmap?getBitmapFromURL(String?urlString){
		Bitmap?bitmap;
		InputStream?is=null;
		try?{
			URL?url=new?URL(urlString);
			HttpURLConnection?connection=(HttpURLConnection)?url.openConnection();
			is=new?BufferedInputStream(connection.getInputStream());
			bitmap=BitmapFactory.decodeStream(is);
			connection.disconnect();
			return?bitmap;
			
		}?catch?(java.io.IOException?e)?{
			//?TODO?Auto-generated?catch?block
			e.printStackTrace();
		}finally{
			
			try?{
				is.close();
			}?catch?(IOException?e)?{
				//?TODO?Auto-generated?catch?block
				e.printStackTrace();
			}
		}
		
		return?null;
	}
}


0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消
Android必學(xué)-異步加載
  • 參與學(xué)習(xí)       50617    人
  • 解答問題       326    個(gè)

了解Android中的異步加載處理方法,這是面試問的最多的知識(shí)點(diǎn)

進(jìn)入課程

求解 使用 LruCache緩存 后出現(xiàn)的問題

我要回答 關(guān)注問題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)