做网站建网站,景观设计方案网站,WordPress自定义js,wordpress tag 收录对于做Android开发的工程师对于这个效果的实现一定不陌生#xff0c;本篇我将带领大家先简单实现这个效果#xff0c;再为大家介绍一下其中的原理#xff0c;方便新手学习#xff0c;老手复习#xff0c;内容简单易懂#xff0c;没有基础一样学习#xff0c;不扯没用的了… 对于做Android开发的工程师对于这个效果的实现一定不陌生本篇我将带领大家先简单实现这个效果再为大家介绍一下其中的原理方便新手学习老手复习内容简单易懂没有基础一样学习不扯没用的了下面开始我们本篇内容的干货。 对于这个效果的实现第一次接触时倍感困难在之前的博客中为大家介绍了如何实现引导页效果虽然带领大家实现了上述功能但是对于具体的实现其实内心有疑惑的当初不是什么的清楚其中的原理经过这些天的不懈努力终于被我攻破了开始介绍一下实现的原理1、既然是广告效果一定需要图片切换2、图片切换要有标识方便用户查看3、图片切换要实现自动内容切换。这三点中最难的当属后两个了在之前的文章中我已经带领大家实现过第一个效果了有兴趣的小童鞋可以自行学习。 我们开始今天的工作首先我们需要准备6张图片两张圆点图片四张任意图片用于我们实现的需要。对于圆点图片大家有时间不容易找我为大家提供两种参考 白色 蓝色 仅供参考大家如果有更好的请绕道。 准备好素材后下面我们开始设计我们的代码 一、在res下新建一个drawable文件夹在其中新建一个round.xml用于我们上面两张图片切换显示控制具体代码如下 ?xml version1.0 encodingUTF-8?
selector xmlns:androidhttp://schemas.android.com/apk/res/androiditem android:state_selectedtrue android:drawabledrawable/blank/item android:state_selectedfalse android:drawabledrawable/white/
/selector 二、下面我们开始我们的布局文件书写 RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.MainActivity android.support.v4.view.ViewPagerandroid:idid/viewPagerandroid:layout_widthmatch_parentandroid:layout_heightmatch_parent/LinearLayout android:idid/llandroid:orientationhorizontalandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_alignParentBottomtrueandroid:layout_marginBottom20dpandroid:layout_centerHorizontaltrueImageView android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:backgrounddrawable/roundandroid:layout_marginRight5dpandroid:visibilitygoneandroid:clickabletrue/ImageView android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:backgrounddrawable/roundandroid:layout_marginRight5dpandroid:visibilitygoneandroid:clickabletrue/ImageView android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:backgrounddrawable/roundandroid:layout_marginRight5dpandroid:visibilitygoneandroid:clickabletrue/ImageView android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:backgrounddrawable/roundandroid:layout_marginRight5dpandroid:visibilitygoneandroid:clickabletrue/ImageView android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:backgrounddrawable/roundandroid:layout_marginRight5dpandroid:visibilitygoneandroid:clickabletrue//LinearLayout/RelativeLayout 注释蓝色标注处表示LinearLayout至于界面底部红色标注处表示应用我们配置好的图片信息现在我们的界面效果是看不出来的因为ImagerView我设置了销毁属性android:visibilitygone这个不影响在下面的代码中我们来控制显示。 三、我们实现图片切换时用到了PagerAdapter这里为了方便我们设计代码我设计了一个自定义的PagerAdapter对象MyselfPagerAdapter.java: public class MyselfPagerAdapter extends PagerAdapter {private ListView view;public MyselfPagerAdapter(ListView view){this.view view;}Overridepublic int getCount() {if(view!null){return view.size();}return 0;}Override//销毁position位置的界面public void destroyItem(View container, int position, Object object) {((ViewPager) container).removeView(view.get(position));}Override//初始化position位置的界面public Object instantiateItem(View container, int position) {((ViewPager) container).addView(view.get(position));return view.get(position);}Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0 arg1;}} 下面就是我们今天的重头戏了MainActivity.java先看代码下面做解释。 public class MainActivity extends Activity implements OnPageChangeListener, OnClickListener{private ViewPager vp;private MyselfPagerAdapter myselfPagerAdapter;private ListView listView;private ImageView[] round;private static final int [] imagerResource {R.drawable.imager1, R.drawable.imager2, R.drawable.imager3, R.drawable.imager4};public int currentIndex 0;private Handler handler new Handler();public MyRunnable myRunnable new MyRunnable();public boolean flag false;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);init();vp (ViewPager) findViewById(R.id.viewPager);myselfPagerAdapter new MyselfPagerAdapter(listView);vp.setAdapter(myselfPagerAdapter);vp.setOnPageChangeListener(this);//初始化底部小点 initRound();handler.postDelayed(myRunnable, 3000);}private void init() {listView new ArrayListView();for(int i 0; iimagerResource.length; i){ImageView imageView new ImageView(this);imageView.setImageResource(imagerResource[i]);listView.add(imageView);}}private void initRound() {LinearLayout ll (LinearLayout) findViewById(R.id.ll);round new ImageView[imagerResource.length];for(int i0; iimagerResource.length; i){round[i] (ImageView) ll.getChildAt(i);round[i].setVisibility(View.VISIBLE);round[i].setOnClickListener(this);round[i].setSelected(false);round[i].setTag(i);}round[currentIndex].setSelected(true);}private void setCurView(int position){if(position0||positionimagerResource.length){return;}vp.setCurrentItem(position);}private void setRoundView(int position){if(position0||positionimagerResource.length||currentIndexposition){return;}round[position].setSelected(true);round[currentIndex].setSelected(false);currentIndex position;}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}Override//当新的页面被选中时调用 public void onPageSelected(int arg0) {// TODO Auto-generated method stubsetRoundView(arg0);}Overridepublic void onClick(View v) {int position (Integer)v.getTag();setCurView(position);setRoundView(position);}class MyRunnable implements Runnable{Overridepublic void run() {int n currentIndex;if(n imagerResource.length-1){flag false;}else{if(n 0){flag true;}}if(flag){n (n 1)%listView.size();}else{n (n - 1)%listView.size();}setCurView(n);setRoundView(n);handler.postDelayed(myRunnable, 3000);}}} 这两段代码的作用为我们添加ImagerView的点击事件做铺垫 private void setCurView(int position){if(position0||positionimagerResource.length){return;}vp.setCurrentItem(position);}private void setRoundView(int position){if(position0||positionimagerResource.length||currentIndexposition){return;}round[position].setSelected(true);round[currentIndex].setSelected(false);currentIndex position;} 这段代码的作用实现图片的自动切换有别于平常的切换大家运行自行查看 class MyRunnable implements Runnable{Overridepublic void run() {int n currentIndex;if(n imagerResource.length-1){flag false;}else{if(n 0){flag true;}}if(flag){n (n 1)%listView.size();}else{n (n - 1)%listView.size();}setCurView(n);setRoundView(n);handler.postDelayed(myRunnable, 3000);}} 最后附一张效果图供大家参考 今天的介绍就到这里大家有什么疑问请留言。 转载于:https://www.cnblogs.com/AndroidJotting/p/4540637.html