package baway.com.viewpagerbanner; import android.graphics.Bitmap; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import java.util.ArrayList; import java.util.List; import baway.com.viewpagerbanner.Utils.AyscTu; public class MainActivity extends AppCompatActivity { private ViewPager vp; private List<String> urllist; private int connt = 0; private int oldposition = 0; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); vp.setCurrentItem(connt); } }; private LinearLayout ll; private ArrayList<View> dolist; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); initdata(); autoPlay(); } private void autoPlay() { new Thread() { @Override public void run() { super.run(); while (true) { SystemClock.sleep(5000); connt++; handler.sendEmptyMessage(0); } } }.start(); } private void initview() { urllist = new ArrayList<>(); dolist = new ArrayList<>(); ll = (LinearLayout) findViewById(R.id.ll); urllist.add("http://169.254.140.58:8080/n.jpg"); urllist.add("http://169.254.140.58:8080/i.jpg"); urllist.add("http://169.254.140.58:8080/o.jpg"); urllist.add("http://169.254.140.58:8080/p.jpg"); for (int i = 0; i < urllist.size(); i++) { View view = new View(MainActivity.this); view.setLayoutParams(new LinearLayout.LayoutParams(50, 50)); dolist.add(view); if (i == 0) { dolist.get(i).setBackgroundResource(R.mipmap.btn_radio_on_holo_dark); } else { dolist.get(i).setBackgroundResource(R.mipmap.btn_radio_on_disabled_holo_dark); } ll.addView(dolist.get(i)); } } private void initdata() { vp = (ViewPager) findViewById(R.id.lv); vp.setAdapter(new Mydapter()); vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int po) { dolist.get(po % dolist.size()).setBackgroundResource(R.mipmap.btn_radio_on_holo_dark); dolist.get(oldposition % dolist.size()).setBackgroundResource(R.mipmap.btn_radio_on_disabled_holo_dark); oldposition = po; } @Override public void onPageScrollStateChanged(int state) { } }); } class Mydapter extends PagerAdapter { @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, int position) { final ImageView imageView = new ImageView(MainActivity.this); new AyscTu(new AyscTu.CallAyTu() { @Override public void callTu(Bitmap bitmap) { imageView.setImageBitmap(bitmap); } }).execute(urllist.get(position % urllist.size())); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } }
viewpagerbanner
最新推荐文章于 2020-06-08 10:23:33 发布