很多App都有这种效果,特别一些电商类的App,顶部每隔几秒钟会向右翻页显示下张图片,用来作推广或者内容展示用的。今天来简单地模仿一下,还自带一个自动跳动的小功能(底部有几个小点,图片移动的时候,点的状态也在变化),用定时器来实现不难。
import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; 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.v4.view.ViewPager.OnPageChangeListener; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.TextView; public class MainActivity extends Activity implements OnPageChangeListener { protected static final String TAG = "MainActivity"; private List<ImageView> imageViewList; private ViewPager mViewPager; private final int[] imageResIDs = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e }; private final String[] imageDescriptions = { "巩俐不低俗,我就不能低俗", "扑树又回来啦!再唱经典老歌引万人大合唱", "揭秘北京电影如何升级", "乐视网TV版大派送", "热血 丝的反杀" }; private TextView tvImageDescription; // 图片描述 private LinearLayout llPointGroup; // 点控件的组 private int previousPosition = 0; // viewpager选中的前一个position private boolean isStop = false; // 控制循环的子线程是否停止 private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); new Thread(new Runnable() { @Override public void run() { // 每两秒钟向主线程发送一条消息, 切换viewpager的界面 while(!isStop) { SystemClock.sleep(2000); handler.sendEmptyMessage(0); } Log.i(TAG, "循环线程停止了"); }}).start(); } @Override protected void onDestroy() { isStop = true; super.onDestroy(); } private void initView() { mViewPager = (ViewPager) findViewById(R.id.viewpager); llPointGroup = (LinearLayout) findViewById(R.id.ll_point_group); tvImageDescription = (TextView) findViewById(R.id.tv_image_description); imageViewList = new ArrayList<ImageView>(); ImageView iv; View pointView; LayoutParams params; for (int i = 0; i < imageResIDs.length; i++) { iv = new ImageView(this); iv.setBackgroundResource(imageResIDs[i]); imageViewList.add(iv); // 根据图片添加点 pointView = new View(this); params = new LayoutParams(5, 5); params.leftMargin = 5; pointView.setLayoutParams(params); pointView.setEnabled(false); pointView.setBackgroundResource(R.drawable.point_bg); llPointGroup.addView(pointView); } MyPagerAdapter mAdapter = new MyPagerAdapter(); mViewPager.setAdapter(mAdapter); mViewPager.setOnPageChangeListener(this); int item = (Integer.MAX_VALUE / 2) - ((Integer.MAX_VALUE / 2) % imageViewList.size()); mViewPager.setCurrentItem(item); // 设置当前选中的item的position tvImageDescription.setText(imageDescriptions[previousPosition]); llPointGroup.getChildAt(previousPosition).setEnabled(true); // 第一点被选中 } class MyPagerAdapter extends PagerAdapter { @Override public int getCount() { return Integer.MAX_VALUE; } /** * 移动的对象和进来的对象如果是同一个就返回true, 代表复用view对象 * false 使用object对象 */ @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } /** * 需要销毁的对象的position传进来 */ @Override public void destroyItem(ViewGroup container, int position, Object object) { // 移除掉指定position的对象 mViewPager.removeView(imageViewList.get(position % imageViewList.size())); } /** * 加载position位置的view对象 */ @Override public Object instantiateItem(ViewGroup container, int position) { // 添加指定position的对象 mViewPager.addView(imageViewList.get(position % imageViewList.size())); return imageViewList.get(position % imageViewList.size()); } } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } /** * 当viewpager页面切换时回调 * @param position 就是当前显示的page页 */ @Override public void onPageSelected(int position) { tvImageDescription.setText(imageDescriptions[position % imageViewList.size()]); llPointGroup.getChildAt(position % imageViewList.size()).setEnabled(true); llPointGroup.getChildAt(previousPosition).setEnabled(false); previousPosition = position % imageViewList.size(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。
新闻热点
疑难解答