本篇内容主要讲解“ListView图片下载优化造成的跳变怎么解决”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“ListView图片下载优化造成的跳变怎么解决”吧!
安远网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。创新互联于2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。public class MainActivity extends Activity {
HashMap
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();
}
//下载图片
//设置图片还未开始下载时的状态
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("/upload/otherpic14/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
{
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图片下载优化造成的跳变怎么解决”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文标题:ListView图片下载优化造成的跳变怎么解决-创新互联
当前网址:http://lswzjz.com/article/desssp.html