ImageLunBo_shape1
package com.baidu.viewpagershape;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import com.lidroid.xutils.BitmapUtils;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
public String[] image_url = new String[] {
"http://pic8.nipic.com/20100701/5290458_114840036316_2.jpg",
"http://img3.3lian.com/2013/s1/20/d/57.jpg",
"http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg",
"http://a0.att.hudong.com/15/08/300218769736132194086202411_950.jpg"
};
private List<ImageView> imgs;
int count = 0;
int olddotindex = 0;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
// 设置此次要显示viewpager
vp.setCurrentItem(count);
// 切换选中的圆点
dots.get(olddotindex).setBackgroundResource(R.drawable.dot_false);// 设置上次圆点为未选中
dots.get(count).setBackgroundResource(R.drawable.dot_true);// 设置当前选中的圆点为选中
olddotindex = count;
};
};
private ViewPager vp;
private List<View> dots;
private LinearLayout doctontint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
doctontint = (LinearLayout) findViewById(R.id.doctontint);
vp = (ViewPager) findViewById(R.id.vp);
// 获取网络图片,配置给VIEWPAGER
getImageViewList();
// 获得原点
getDotList();
// 设置第一个圆点为选中状态
dots.get(0).setBackgroundResource(R.drawable.dot_true);
vp.setAdapter(new MyVpAdapter());// 适配pager页
// 通过适配器,实现自动滑动效果
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
count++;// 下一页
if (count == 4) {
count = 0;
}
// 这是子线程,不能更新UI主页面,需用handler
handler.sendEmptyMessage(1);
}
}, 3000, 2000);
}
private void getDotList() {
dots = new ArrayList<View>();
// 循环图片数组,创建对应数量的dot
for (int i = 0; i < image_url.length; i++) {
View view = LayoutInflater.from(this).inflate(R.layout.dot_layout,
null);// 加载布局
// 得到布局中的dot点组件
View dot = view.findViewById(R.id.dotViw);
// 收集dot
dots.add(dot);
// 把布局添加到线性布局
doctontint.addView(view);
}
}
class MyVpAdapter extends PagerAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return imgs.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
// super.destroyItem(container, position, object);
ImageView img = imgs.get(position);
container.removeView(img);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(imgs.get(position));
return imgs.get(position);
}
}
/*
* 到网络获取图片,并赋值到viewpager
*/
private void getImageViewList() {
imgs = new ArrayList<ImageView>();
BitmapUtils bitmapUtils = new BitmapUtils(this);
// 加载图片
for (int i = 0; i < image_url.length; i++) {
// 用bitmaputil需要使用到载体
ImageView img = new ImageView(this);
img.setScaleType(ScaleType.FIT_XY);
bitmapUtils.display(img, image_url[i]);
imgs.add(img);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}