您好,登錄后才能下訂單哦!
package com.example.ex_day_0611_day07_listview;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.concurrent.Executor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
HashMap<Integer,Bitmap> map = new HashMap<Integer,Bitmap>();
HashMap<Integer,Boolean> hashmap = new HashMap<Integer,Boolean>();
HashMap<Integer,MyAsyncTask2> myapa = new HashMap<Integer,MyAsyncTask2>();
HashMap<Integer,Integer> myint = new HashMap<Integer,Integer>();
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();
}
//下載圖片
//設(shè)置圖片還未開始下載時的狀態(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);
}
//實現(xiàn)按鈕點擊事件
final Button button1 = user.button;
final int pos = position;
final ProgressBar pb1 = user.pb;
final TextView tv = user.tv;
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
button1.setText("下載中");
button1.setEnabled(false);
MyAsyncTask2 myasyctask2 = new MyAsyncTask2(button1,pb1,tv,pos);
myapa.put(pos, myasyctask2);
myasyctask2.execute("http://192.168.56.1:8080/service/qq"+pos+".zip");
hashmap.put(pos, null);
}
});
MyAsyncTask2 task2 = myapa.get(pos);
ProgressBar pb2 = user.pb;
Button button2 = user.button;
TextView tv1 = user.tv;
//按鈕點擊事件中設(shè)置進度條。
//下文件
if(hashmap.get(position)==null)
{
button1.setText("下載");
button1.setEnabled(true);
user.tv.setText("==========");
pb1.setProgress(0);
}
else if(hashmap.get(position) == false)
{
button1.setText("下載中");
button1.setEnabled(false);
user.pb.setMax(myint.get(pos));
task2.setReplace(pb2,button2,tv1);
}else if(hashmap.get(position)==true)
{
button1.setText("下載完成");
button1.setEnabled(true);
user.tv.setText("==========");
pb1.setMax(100);
pb1.setProgress(100);
}
return view;
}
@Override
public int getCount() {
return 20;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
class MyAsyncTask2 extends AsyncTask<String, Integer, String>
{
Button button1;
ProgressBar pb;
TextView tv;
int position;
public MyAsyncTask2(Button button1, ProgressBar pb,TextView tv,int position)
{
this.button1 = button1;
this.pb = pb;
this.tv = tv;
this.position = position;
}
public void setReplace(ProgressBar pb,Button button1,TextView tv)
{
this.pb = pb;
this.button1 = button1;
this.tv = tv;
}
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(params[0]);
URLConnection openConnection = url.openConnection();
int contentLength = openConnection.getContentLength();
myint.put(position, contentLength);
pb.setMax(contentLength);
InputStream inputStream = openConnection.getInputStream();
byte[] buffer = new byte[1024];
int sum = 0;
int len = 0 ;
hashmap.put(position, false);
while(-1 != (len = inputStream.read(buffer)))
{
sum += len;
Thread.sleep(100);
publishProgress(sum);
}
hashmap.put(position, true);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return "下載完成";
}
@Override
protected void onPostExecute(String result) {
button1.setText(result);
button1.setEnabled(true);
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Integer... values) {
int fp = listview.getFirstVisiblePosition();
int lp = listview.getLastVisiblePosition();
if (position>=fp&&position <= lp) {
pb.setProgress(values[0]);
tv.setText((int)((double)values[0]/(double)pb.getMax()*100)+"%");
}
super.onProgressUpdate(values);
}
}
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) {
e.printStackTrace();
} catch (IOException e) {
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;
}
}
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。