第一种方法 图片自动依次轮播,第一轮轮播完,重新回到第一张轮播,但是界面不会后退去定位到第一张图片
main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rl" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="120dp" android:background="@drawable/icon" /> <ImageView android:id="@+id/imageView2" android:layout_width="fill_parent" android:layout_height="120dp" android:background="@drawable/expriment" /> </RelativeLayout>
IamgeTranslatActivity
package com.test; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.AnimationUtils; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.TextView; public class IamgeTranslatActivity extends Activity { public ImageView imageView; public ImageView imageView2; public Animation animation1; public Animation animation2; public boolean juage = true; // 需要播放的图片集合 public int images[] = new int[] { R.drawable.icon, R.drawable.expriment, R.drawable.changer, R.drawable.dataline, R.drawable.preffitication }; // 播放次数 public int count = 0; public Handler handler = new Handler(); public Runnable runnable = new Runnable() { @Override public void run() { AnimationSet animationSet1 = new AnimationSet(true); AnimationSet animationSet2 = new AnimationSet(true); imageView2.setVisibility(View.VISIBLE); TranslateAnimation ta = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f); ta.setDuration(1000); animationSet1.addAnimation(ta); animationSet1.setFillAfter(true); ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f); ta.setDuration(1000); animationSet2.addAnimation(ta); animationSet2.setFillAfter(true); imageView.setBackgroundResource(images[count % 5]); count++; imageView2.setBackgroundResource(images[count % 5]); // iamgeView 出去 imageView2 进来 imageView.startAnimation(animationSet1); imageView2.startAnimation(animationSet2); if (juage) handler.postDelayed(runnable, 1000); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView = (ImageView) findViewById(R.id.imageView); imageView2 = (ImageView) findViewById(R.id.imageView2); imageView2.setVisibility(View.GONE); // 延时delayMillis毫秒 将Runnable插入消息列队 handler.postDelayed(runnable, 1000); } /** * 程序正常启动:onCreate()->onStart()->onResume(); * 正常退出:onPause()->onStop()->onDestory() 一个Activity启动另一个Activity: * onPause()->onStop(), 再返回:onRestart()->onStart()->onResume() 程序按back 退出: * onPause()->onStop()->onDestory(),再进入:onCreate()->onStart()->onResume(); * 程序按home 退出: onPause()->onStop(),再进入:onRestart()->onStart()->onResume(); */ public void onPause() { juage = false; super.onPause(); } }
第二种方法 手势可以滑动页面,也可以自动轮播,但是bug是手势滑动的同时,也会自动轮播
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.lunbodemo.MainActivity" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="wrap_content" > </android.support.v4.view.ViewPager> </RelativeLayout>
MainActivity
package com.example.lunbodemo; import java.util.ArrayList; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; public class MainActivity extends Activity { private int imageIds[]; private ArrayList<ImageView> images; private ViewPager mViewPager; private ViewPagerAdapter adapter; private ScheduledExecutorService scheduledExecutorService; private int currentItem; // 当前页面 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageIds = new int[] { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e }; images = new ArrayList<ImageView>(); for (int i = 0; i < imageIds.length; i++) { ImageView imageView = new ImageView(this); imageView.setBackgroundResource(imageIds[i]); images.add(imageView); } mViewPager = (ViewPager) findViewById(R.id.viewpager); adapter = new ViewPagerAdapter(); mViewPager.setAdapter(adapter); mViewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { currentItem = position; } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); } @Override protected void onStart() { super.onStart(); scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); // 每隔2秒钟切换一张图片--相对固定的延迟后,执行某项计划 scheduledExecutorService.scheduleWithFixedDelay(new ViewPagerTask(), 2, 2, TimeUnit.SECONDS); } private class ViewPagerTask implements Runnable { @Override public void run() { currentItem = (currentItem + 1) % imageIds.length; // message 从handler 类获取,从而可以直接向该handler 对象发送消息 /** * 另外写法 * Message msg=new Message(); msg.arg1=i; handler.sendMessage(msg); */ handler.obtainMessage().sendToTarget(); } } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // 设置当前页面 mViewPager.setCurrentItem(currentItem); } }; private class ViewPagerAdapter extends PagerAdapter { @Override public int getCount() { return images.size(); } // 是否是同一张图片 @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void destroyItem(ViewGroup view, int position, Object object) { view.removeView(images.get(position)); } @Override public Object instantiateItem(ViewGroup view, int position) { view.addView(images.get(position)); return images.get(position); } } }
另外2种方法
http://blog.csdn.net/u013210620/article/details/46537283
http://blog.csdn.net/u013210620/article/details/44105803
时间: 2024-10-16 09:16:53