本地和网络获得轮播图资源:
Posted 孔筱艾Studio
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了本地和网络获得轮播图资源:相关的知识,希望对你有一定的参考价值。
MainActivity:
public class MainActivity extends Activity {
Handler hd = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
int currentItem = vp.getCurrentItem();
currentItem++;
vp.setCurrentItem(currentItem);
setMove();
}
};
};
private ViewPager vp;
private LinearLayout ll;
private ArrayList<ImageView> dl;
private List<Integer> list;
private int arg2;
private String path;
private List<Details> ld;
private MyAsynTask mt;
private ListView gv;
private Weixin weixin;
private ArrayList<Details> ds;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = (ViewPager) findViewById(R.id.vp);
ll = (LinearLayout) findViewById(R.id.ll);
gv = (ListView) findViewById(R.id.gv);
MyAsynTask mt = new MyAsynTask();
mt.execute();
list = new ArrayList<Integer>();
list.add(R.drawable.beaut);
list.add(R.drawable.bottles);
list.add(R.drawable.pills);
list.add(R.drawable.pink);
//设置本地图片资源
//vp.setAdapter(new MyVpAdapter(MainActivity.this, list, hd));
//设置当前条目为
//vp.setCurrentItem(list.size() * 10000);
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
for (int i = 0; i < dl.size(); i++) {
//position1
if (arg0 % dl.size() == i) {
dl.get(i).setImageResource(R.drawable.dots_checked);
} else {
dl.get(i).setImageResource(R.drawable.dots_normal);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
// 实例化小点儿
initDots();
// 调用handler
setMove();
}
private void setMove() {
// TODO Auto-generated method stub
hd.sendEmptyMessageDelayed(1, 1500);
}
private void initDots() {
//创建小点儿的集合
dl = new ArrayList<ImageView>();
//移除内容
ll.removeAllViews();
//清空集合
dl.clear();
for (int i = 0; i < list.size(); i++) {
ImageView imageView = new ImageView(this);
// 加点儿
if (i == 0) {
// 默认为亮点儿
imageView.setImageDrawable(getResources().getDrawable(
R.drawable.dots_checked));
} else {
imageView.setImageDrawable(getResources().getDrawable(
R.drawable.dots_normal));
}
LayoutParams params = new LayoutParams(20, 20);
ll.addView(imageView, params);
dl.add(imageView);
}
}
class MyAsynTask extends AsyncTask<Void, Void, String> {
private MyAdapter ma;
private ProgressDialog pd;
/*
* // 线程之前进行初始化操作
*
* @Override protected void onPreExecute() { // TODO Auto-generated
* method stub super.onPreExecute(); pd = new
* ProgressDialog(MainActivity.this); pd.setTitle("正在加载中"); pd.show(); }
*/
// 后台执行耗时操作
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
int position = 0;
HttpClient httpClient = new DefaultHttpClient();
path = "http://v.juhe.cn/weixin/query?key=0917c812f187b92e025c9b7c2d4e59b5"
+ "&pno="
+ URLEncoder.encode((position + 1) + "", "utf-8");
HttpGet httpGet = new HttpGet(path);
HttpResponse httpResponse = httpClient.execute(httpGet);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200) {
HttpEntity entity = httpResponse.getEntity();
InputStream inputStream = entity.getContent();
String str = streamToStr(inputStream);
return str;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
// 展示后台获取的结果
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// TODO Auto-generated method stub
// 解析
Gson gson = new Gson();
weixin = gson.fromJson(result, Weixin.class);
ma = new MyAdapter(weixin);
gv.setAdapter(ma);
ds = weixin.result.list;
vp.setCurrentItem(ds.size()*10000);
vp.setAdapter(new VpAdapter(MainActivity.this, ds, hd));
}
}
/**
* 流转串
*
* @param inputStream
* @return
*/
public static String streamToStr(InputStream inputStream) {
try {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
arrayOutputStream.write(buffer, 0, len);
}
return arrayOutputStream.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// 定义适配器
class MyAdapter extends BaseAdapter {
Weixin weixin;
private ArrayList<Details> list;
public MyAdapter(Weixin weixin) {
// TODO Auto-generated constructor stub
this.weixin = weixin;
list = weixin.result.list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder vh = null;
if (convertView == null) {
convertView = View.inflate(MainActivity.this, R.layout.lv_item,
null);
vh = new ViewHolder();
convertView.setTag(vh);
vh.iv = (ImageView) convertView.findViewById(R.id.lv_imageView);
vh.tv = (TextView) convertView.findViewById(R.id.lv_textView);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.tv.setText(list.get(position).title);
String url = list.get(position).firstImg;
BitmapUtils bu = new BitmapUtils(MainActivity.this);
bu.display(vh.iv, url);
return convertView;
}
class ViewHolder {
ImageView iv;
TextView tv;
}
}
}
设置本地图片资源的适配器:
//设置本地图片的轮播
public class MyVpAdapter extends PagerAdapter {
MainActivity mainActivity;
List<Integer> list;
Handler hd;
public MyVpAdapter(MainActivity mainActivity, List<Integer> list, Handler hd) {
// TODO Auto-generated constructor stub
this.mainActivity = mainActivity;
this.list = list;
this.hd = hd;
}
@Override
public int getCount() {
// 得到最大值
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// 直接实例化imageview对象
ImageView iv = new ImageView(mainActivity);
// 设置图片资源需要用position取集合长度的余数
iv.setImageResource(list.get(position % list.size()));
// 设置轮播过程中图片的监听事件
iv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// 判断事件
switch (event.getAction()) {
// 取消时,发送handler空消息继续轮播
case MotionEvent.ACTION_CANCEL:
hd.sendEmptyMessageDelayed(1, 1500);
break;
// 摁下时,移除所有的回调和信息
case MotionEvent.ACTION_DOWN:
hd.removeCallbacksAndMessages(null);
break;
// 抬起时,发送handler空消息继续轮播
case MotionEvent.ACTION_UP:
hd.sendEmptyMessageDelayed(1, 1500);
break;
default:
break;
}
// 返回被消费
return true;
}
});
// 添加视图
container.addView(iv);
return iv;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
//网络请求设置轮播资源
public class VpAdapter extends PagerAdapter {
MainActivity mainActivity;
ArrayList<Details> ds;
Handler hd;
public VpAdapter(MainActivity mainActivity, ArrayList<Details> ds,
Handler hd) {
// TODO Auto-generated constructor stub
this.mainActivity = mainActivity;
this.ds = ds;
this.hd = hd;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
ImageView iv = new ImageView(mainActivity);
// 利用工具类获取图片资源
BitmapUtils bi = new BitmapUtils(mainActivity);
bi.display(iv, ds.get(position % ds.size()).firstImg);
iv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_CANCEL:
hd.sendEmptyMessageDelayed(1, 1500);
break;
case MotionEvent.ACTION_DOWN:
hd.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_UP:
hd.sendEmptyMessageDelayed(1, 1500);
break;
default:
break;
}
return true;
}
});
container.addView(iv);
return iv;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
// super.destroyItem(container, position, object);
container.removeView((View) object);
}
}
以上是关于本地和网络获得轮播图资源:的主要内容,如果未能解决你的问题,请参考以下文章
uniapp外包杯学习笔记day07 | 微信小程序轮播图分类导航楼层图的开发与实现