溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ListView圖片下載優(yōu)化造成的跳變怎么解決

發(fā)布時間:2022-01-04 09:07:26 來源:億速云 閱讀:104 作者:iii 欄目:開發(fā)技術

本篇內容主要講解“ListView圖片下載優(yōu)化造成的跳變怎么解決”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“ListView圖片下載優(yōu)化造成的跳變怎么解決”吧!

public class MainActivity extends Activity {

HashMap<Integer,Bitmap> map = new HashMap<Integer,Bitmap>();

private ListView listview;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

listview = (ListView)findViewById(R.id.listView1);

MyApapter myApapter = new MyApapter();

        listview.setAdapter(myApapter);

}

class User

{

ImageView p_w_picpathview;

ProgressBar pb;

TextView tv;

Button button;

}

 class MyApapter extends BaseAdapter

 {

@Override

 public View getView(int position, View convertView, ViewGroup parent) {

     View view = null;

 User user = new User();

LayoutInflater inflater = getLayoutInflater();

if(convertView==null)

{

 view = inflater.inflate(R.layout.itmp, null);

 user.p_w_picpathview = (ImageView)view.findViewById(R.id.p_w_picpathView1);

 user.pb = (ProgressBar)view.findViewById(R.id.progressBar1);

 user.button = (Button)view.findViewById(R.id.button1); 

 user.tv = (TextView)view.findViewById(R.id.textView1);

 view.setTag(user);

}else

{

view = convertView;

user = (User)view.getTag();

}

//下載圖片

//設置圖片還未開始下載時的狀態(tài)

user.p_w_picpathview.setImageResource(R.drawable.ic_launcher);

Bitmap bitmap = map.get(position);

if (bitmap==null) 

{

MyAsyncTask myp_w_picpathview = new MyAsyncTask(user.p_w_picpathview,position,user.pb);

myp_w_picpathview.execute("http://192.168.56.1:8080/service/qq"+position+".png");

}else

{

user.p_w_picpathview.setImageBitmap(bitmap);

}

return view;

 }

@Override

public int getCount() {

// TODO Auto-generated method stub

return 10;

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return null;

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return 0;

}

 }

 class MyAsyncTask extends AsyncTask<String, Void, Bitmap>

 {

        int position;

        ImageView p_w_picpathview;

        ProgressBar pb;

public MyAsyncTask(ImageView p_w_picpathview,int position,ProgressBar pb) {

this.p_w_picpathview = p_w_picpathview;

this.position = position;

this.pb = pb;

}

@Override

protected Bitmap doInBackground(String... params) {

Bitmap bitmap = null;

            try {

URL url = new URL(params[0]);

URLConnection connection = url.openConnection();

InputStream is = connection.getInputStream();

bitmap = BitmapFactory.decodeStream(is);

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return bitmap;

}

         @Override

        protected void onPostExecute(Bitmap result) {

        //得到當前屏幕可見第一行

         int fvb = listview.getFirstVisiblePosition();

         int lvp = listview.getLastVisiblePosition();

         if (position>=fvb&&position<=lvp) 

         {

         p_w_picpathview.setImageBitmap(result);

 }

         map.put(position, result);

              super.onPostExecute(result);

        }  

 }

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

ListView圖片下載優(yōu)化造成的跳變怎么解決

到此,相信大家對“ListView圖片下載優(yōu)化造成的跳變怎么解決”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI