1. 程式人生 > >圖片輪播5種方法思路

圖片輪播5種方法思路

第一種方法 圖片自動依次輪播,第一輪輪播完,重新回到第一張輪播,但是介面不會後退去定位到第一張圖片

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);
		}
	}

}

另外3種方法

http://blog.csdn.net/u013210620/article/details/46537283

http://blog.csdn.net/u013210620/article/details/44105803

http://blog.csdn.net/u013210620/article/details/48729215