[android]代码库
//daojishi
private String[] imageView=new String[]{"http://p0.so.qhmsg.com/bdr/326__/t01620f66967a9f8c70.jpg",
"http://p3.so.qhmsg.com/bdr/326__/t01ba898b85ee68c8dd.jpg","http://p4.so.qhmsg.com/bdr/326__/t01c8ccc5629cc31f2e.jpg",
"http://p1.so.qhmsg.com/bdr/326__/t0168bc4191fc244d51.jpg"};
private ImageView[] image=new ImageView[imageView.length];
private int time=2*3600-1;
private int hour=0;
private int minute=0;
private int second=0;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int a= vp.getCurrentItem();
vp.setCurrentItem(++a);
if(time>0) {
hour = time / 3600;
minute = (time - hour * 3600) / 60;
second = time - hour * 3600 - minute * 60;
date.setText(hour + "小时" + minute + "分钟" + second + "秒");
time--;
}
handler.sendEmptyMessageDelayed(0,1000);
}
};
//main方法里
@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);
date = (TextView) findViewById(R.id.date);
options = new DisplayImageOptions.Builder().build();
setPorivter();
setViewPager();
handler.sendEmptyMessageDelayed(0,1000);
}
//定义的方法
private void setViewPager() {
vp.setAdapter(new MyView());
vp.setCurrentItem(0);
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
setPoint(position%imageView.length);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
public void setPoint(int index){
for(int i=0;i<imageView.length;i++){
if(i==index){
image[i].setImageResource(R.drawable.selecter);
}else{
image[i].setImageResource(R.drawable.normal);
}
}
}
private void setPorivter() {
for(int i=0;i<imageView.length;i++){
ImageView img=new ImageView(this);
img.setLayoutParams(new ViewGroup.LayoutParams(20,20));
img.setScaleType(ImageView.ScaleType.FIT_XY);
if(i==0){
//上来就显示小圆点第一个就是红色
img.setImageResource(R.drawable.selecter);
}else{
img.setImageResource(R.drawable.normal);
}
image[i]=img;
//添加视图
ll.addView(img);
}
}
class MyView extends PagerAdapter{
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView img=new ImageView(MainActivity.this);
img.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
img.setScaleType(ImageView.ScaleType.FIT_XY);
int index=position%imageView.length;
ImageLoader.getInstance().displayImage(imageView[index],img,options);
container.addView(img);
return img;
}
}
//布局
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="300dp"
android:id="@+id/vp"></android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:id="@+id/ll"
android:layout_alignBottom="@+id/vp"></LinearLayout>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#f00"/>
<stroke android:width="1dp" android:color="#0f0"/>
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#fff"/>
<stroke android:width="1dp" android:color="#0f0"/>
</shape>
by: 发表于:2017-09-27 10:01:20 顶(0) | 踩(0) 回复
??
回复评论