viewpage图片轮播

时间:2021-10-04 05:21:58

package com.exmple.viewpage2;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.examle.netUtil.NetUtill;
import com.examle.vo.Super;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.BitmapUtils;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

private ViewPager viewpage;
private List<Super> li;
private List<View> dots;
int count = 0;
int odl = 0;
Handler ha = new Handler() {
public void handleMessage(android.os.Message msg) {

viewpage.setCurrentItem(count);
dots.get(count).setBackgroundResource(R.drawable.founce);
dots.get(odl).setBackgroundResource(R.drawable.normal);
odl=count;

};
};
private LinearLayout line;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpage = (ViewPager) findViewById(R.id.viewpage);
line = (LinearLayout) findViewById(R.id.line);
new Asy().execute(NetUtill.JSON_URL1);

}

public class Asy extends AsyncTask<String, Integer, String> {

@Override
protected String doInBackground(String... params) {
String url = params[0];
String str = NetUtill.JsonData(url);

return str;
}

@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
BitmapUtils bit = new BitmapUtils(MainActivity.this);
Gson gs = new Gson();
List<ImageView> lis = new ArrayList<ImageView>();

li = gs.fromJson(result, new TypeToken<List<Super>>() {
}.getType());

for (int i = 0; i < li.size(); i++) {
ImageView image = new ImageView(MainActivity.this);
image.setScaleType(ScaleType.FIT_XY);
bit.display(image, "http://172.17.29.120/localuser/ljy/jc/"
+ li.get(i).getSrc());
lis.add(image);
}
Apater ap = new Apater(lis, MainActivity.this);

viewpage.setAdapter(ap);

getDont();
dots.get(0).setBackgroundResource(R.drawable.founce);
Timer time = new Timer();
time.schedule(new TimerTask() {

@Override
public void run() {
count++;
if (count == li.size()) {
count = 0;

}
ha.sendEmptyMessage(1);

}
}, 2000, 2000);

}

public void getDont() {
dots = new ArrayList<View>();
for (int i = 0; i < li.size(); i++) {
View view = View
.inflate(MainActivity.this, R.layout.view, null);
View dot = view.findViewById(R.id.view1);

dots.add(dot);
line.addView(view);
}
}

}

}

//////////////////////////////Apater

package com.exmple.viewpage2;

import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class Apater extends PagerAdapter {
List<ImageView> lis;
Context co;

public Apater(List<ImageView> lis, Context co) {
super();
this.lis = lis;
this.co = co;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return lis.size();
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(lis.get(position));
return lis.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
// super.destroyItem(container, position, object);
}

}