diff --git a/AndroidImageIndicator/.classpath b/AndroidImageIndicator/.classpath index a4f1e40..26bdfa6 100644 --- a/AndroidImageIndicator/.classpath +++ b/AndroidImageIndicator/.classpath @@ -3,6 +3,7 @@ - + + diff --git a/AndroidImageIndicator/AndroidManifest.xml b/AndroidImageIndicator/AndroidManifest.xml index ea05351..722e859 100644 --- a/AndroidImageIndicator/AndroidManifest.xml +++ b/AndroidImageIndicator/AndroidManifest.xml @@ -12,7 +12,8 @@ android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" - android:theme="@style/AppTheme" > + android:theme="@style/AppTheme" + android:name="com.allthelucky.common.view.sample.SampleApp" > diff --git a/AndroidImageIndicator/res/layout/activity_indicator_auto.xml b/AndroidImageIndicator/res/layout/activity_indicator_auto.xml index 2ccaf93..6493e92 100644 --- a/AndroidImageIndicator/res/layout/activity_indicator_auto.xml +++ b/AndroidImageIndicator/res/layout/activity_indicator_auto.xml @@ -3,7 +3,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" > - diff --git a/AndroidImageIndicator/res/values/strings.xml b/AndroidImageIndicator/res/values/strings.xml index eeb0256..0d69e25 100644 --- a/AndroidImageIndicator/res/values/strings.xml +++ b/AndroidImageIndicator/res/values/strings.xml @@ -1,4 +1,4 @@ - + AndroidImageIndicator diff --git a/AndroidImageIndicator/res/values/styles.xml b/AndroidImageIndicator/res/values/styles.xml index 79a39af..afe1d34 100644 --- a/AndroidImageIndicator/res/values/styles.xml +++ b/AndroidImageIndicator/res/values/styles.xml @@ -1,13 +1,13 @@ diff --git a/AndroidImageIndicator/src/com/allthelucky/common/view/AutoBrocastManager.java b/AndroidImageIndicator/src/com/allthelucky/common/view/AutoBrocastManager.java new file mode 100644 index 0000000..7a4a1bd --- /dev/null +++ b/AndroidImageIndicator/src/com/allthelucky/common/view/AutoBrocastManager.java @@ -0,0 +1,182 @@ +package com.allthelucky.common.view; + +import android.os.Handler; +import android.os.Message; + +import com.allthelucky.common.view.ImageIndicatorView; + +/** + * Auto BrocastManager for ImageIndicatorView + * + * @author steven-pan + * + */ +public class AutoBrocastManager { + + /** + * 自动播放标志位,默认播放 + */ + private boolean broadcastEnable = false; + /** + * 自动播放启动默认时间 + */ + private static final long DEFAULT_STARTMILS = 2 * 1000; + /** + * 自动播放间隔默认时间 + */ + private static final long DEFAULT_INTEVALMILS = 3 * 1000; + /** + * 启动时间ms + */ + private long startMils = DEFAULT_STARTMILS; + /** + * 间隔ms + */ + private long intevalMils = DEFAULT_INTEVALMILS; + /** + * 向右 + */ + private final static int RIGHT = 0; + /** + * 向左 + */ + private final static int LEFT = 1; + + /** + * 当前方向 + */ + private int direction = RIGHT; + + /** + * 自动播放默认次数(无限) + */ + private static final int DEFAULT_TIMES = -1; + + /** + * 自动播放次数 + */ + private int broadcastTimes = DEFAULT_TIMES; + + /** + * 自动播放次数记数 + */ + private int timesCount = 0; + + /** + * 循环播放 + */ + private Handler broadcastHandler = null; + + /** + * target ImageIndicatorView + */ + private ImageIndicatorView mImageIndicatorView = null; + + public AutoBrocastManager(ImageIndicatorView imageIndicatorView) { + this.mImageIndicatorView = imageIndicatorView; + this.broadcastHandler = new BroadcastHandler(AutoBrocastManager.this); + } + + /** + * 设置自动播放启动时间和间隔 + * + * @param startMils + * 启动时间ms(默认为8s) + * @param intevelMils + * 间隔ms(默认为3s) + */ + public void setBroadcastTimeIntevel(long startMils, long intevelMils) { + this.startMils = startMils; + this.intevalMils = intevelMils; + } + + /** + * 设置自动播放开关 + * + * @param flag + * 打开或关闭 + */ + public void setBroadcastEnable(boolean flag) { + this.broadcastEnable = flag; + } + + /** + * 设置循环播放次数 + * + * @param times + * 循环播放次数 + */ + public void setBroadCastTimes(int times) { + this.broadcastTimes = times; + } + + /** + * 启动循环播放 + */ + public void loop() { + if (broadcastEnable) { + broadcastHandler.sendEmptyMessageDelayed(0, this.startMils); + } + } + + protected void handleMessage(android.os.Message msg) { + if (broadcastEnable) { + if (System.currentTimeMillis() + - mImageIndicatorView.getRefreshTime() < 5 * 1000) {// 最近一次划动间隔小于5s + return; + } + if ((broadcastTimes != DEFAULT_TIMES) + && (timesCount > broadcastTimes)) {// 循环次数用完 + return; + } + + if (direction == RIGHT) {// roll right + if (mImageIndicatorView.getCurrentIndex() < mImageIndicatorView + .getTotalCount()) { + if (mImageIndicatorView.getCurrentIndex() == mImageIndicatorView + .getTotalCount() - 1) { + timesCount++;// 循环次数次数加1 + direction = LEFT; + } else { + mImageIndicatorView + .getViewPager() + .setCurrentItem( + mImageIndicatorView.getCurrentIndex() + 1, + true); + } + } + } else {// roll left + if (mImageIndicatorView.getCurrentIndex() >= 0) { + if (mImageIndicatorView.getCurrentIndex() == 0) { + direction = RIGHT; + } else { + mImageIndicatorView + .getViewPager() + .setCurrentItem( + mImageIndicatorView.getCurrentIndex() - 1, + true); + } + } + } + + broadcastHandler.sendEmptyMessageDelayed(1, this.intevalMils); + } + } + + static class BroadcastHandler extends Handler { + private AutoBrocastManager autoBrocastManager; + + public BroadcastHandler(AutoBrocastManager autoBrocastManager) { + this.autoBrocastManager = autoBrocastManager; + } + + @Override + public void handleMessage(Message msg) { + super.handleMessage(msg); + if (this.autoBrocastManager != null) { + autoBrocastManager.handleMessage(msg); + } + } + } + +} diff --git a/AndroidImageIndicator/src/com/allthelucky/common/view/AutoImageIndicatorView.java b/AndroidImageIndicator/src/com/allthelucky/common/view/AutoImageIndicatorView.java deleted file mode 100644 index c0d10e2..0000000 --- a/AndroidImageIndicator/src/com/allthelucky/common/view/AutoImageIndicatorView.java +++ /dev/null @@ -1,181 +0,0 @@ -package com.allthelucky.common.view; - -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -import android.content.Context; -import android.os.Handler; -import android.os.Message; -import android.util.AttributeSet; - -/** - * 自动播放的宣传画控件(间隔播放,自动切换方向,循环) - * - * @author savant-pan - * - */ -public class AutoImageIndicatorView extends ImageIndicatorView { - /** - * 定时服务 - */ - private ScheduledExecutorService scheduler; - /** - * 自动播放标志位,默认播放 - */ - private boolean broadcastEnagle = false; - /** - * 自动播放启动默认时间 - */ - private static final long DEFAULT_STARTMILS = 2 * 1000; - /** - * 自动播放间隔默认时间 - */ - private static final long DEFAULT_INTEVALMILS = 3 * 1000; - /** - * 启动时间ms - */ - private long startMils = DEFAULT_STARTMILS; - /** - * 间隔ms - */ - private long intevalMils = DEFAULT_INTEVALMILS; - /** - * 向右 - */ - private final static int RIGHT = 0; - /** - * 向左 - */ - private final static int LEFT = 1; - - /** - * 当前方向 - */ - private int direction = RIGHT; - - /** - * 自动播放默认次数(无限) - */ - private static final int DEFAULT_TIMES = -1; - - /** - * 自动播放次数 - */ - private int broadcastTimes = DEFAULT_TIMES; - - /** - * 自动播放次数记数 - */ - private int timesCount = 0; - - /** - * 循环播放 - */ - private Handler broadcastHandler = null; - - public AutoImageIndicatorView(Context context, AttributeSet attrs) { - super(context, attrs); - this.init(); - } - - public AutoImageIndicatorView(Context context) { - super(context); - this.init(); - } - - private void init() { - this.broadcastHandler = new BroadcastHandler(AutoImageIndicatorView.this); - this.scheduler = Executors.newScheduledThreadPool(1); - } - - /** - * 设置自动播放启动时间和间隔 - * - * @param startMils - * 启动时间ms(默认为8s) - * @param intevelMils - * 间隔ms(默认为3s) - */ - public void setBroadcastTimeIntevel(long startMils, long intevelMils) { - this.startMils = startMils; - this.intevalMils = intevelMils; - } - - /** - * 设置自动播放开关 - * - * @param flag - * 打开或关闭 - */ - public void setBroadcastEnable(boolean flag) { - this.broadcastEnagle = flag; - } - - /** - * 设置循环播放次数 - * - * @param times - * 循环播放次数 - */ - public void setBroadCastTimes(int times) { - this.broadcastTimes = times; - } - - @Override - public void show() { - super.show(); - // 定时播放服务 - this.scheduler.scheduleAtFixedRate(new Runnable() { - @Override - public void run() { - if (broadcastEnagle) { - if (System.currentTimeMillis() - getRefreshTime() < 5 * 1000) {// 最近一次划动间隔小于5s - return; - } - if ((broadcastTimes != DEFAULT_TIMES) && (timesCount > broadcastTimes)) {// 循环次数用完 - return; - } - broadcastHandler.sendEmptyMessage(0); - } - } - }, this.startMils, this.intevalMils, TimeUnit.MILLISECONDS); - } - - protected void handleMessage(android.os.Message msg) { - if (direction == RIGHT) {// roll right - if (getCurrentIndex() < getTotalCount()) { - if (getCurrentIndex() == getTotalCount() - 1) { - timesCount++;// 循环次数次数加1 - direction = LEFT; - } else { - getViewPager().setCurrentItem(getCurrentIndex() + 1, true); - } - } - } else {// roll left - if (getCurrentIndex() >= 0) { - if (getCurrentIndex() == 0) { - direction = RIGHT; - } else { - getViewPager().setCurrentItem(getCurrentIndex() - 1, true); - } - } - } - } -} - -class BroadcastHandler extends Handler { - private AutoImageIndicatorView autoImageIndicatorView; - - public BroadcastHandler(AutoImageIndicatorView autoImageIndicatorView) { - this.autoImageIndicatorView = autoImageIndicatorView; - } - - @Override - public void handleMessage(Message msg) { - super.handleMessage(msg); - if (this.autoImageIndicatorView != null) { - autoImageIndicatorView.handleMessage(msg); - } - } -} diff --git a/AndroidImageIndicator/src/com/allthelucky/common/view/ImageIndicatorView.java b/AndroidImageIndicator/src/com/allthelucky/common/view/ImageIndicatorView.java index 36c4074..10ab8a2 100644 --- a/AndroidImageIndicator/src/com/allthelucky/common/view/ImageIndicatorView.java +++ b/AndroidImageIndicator/src/com/allthelucky/common/view/ImageIndicatorView.java @@ -5,9 +5,6 @@ import java.util.Arrays; import java.util.List; import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.BitmapFactory.Options; import android.os.Handler; import android.os.Message; import android.os.Parcelable; @@ -19,12 +16,9 @@ import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.ImageView; -import android.widget.ImageView.ScaleType; import android.widget.LinearLayout; import android.widget.RelativeLayout; -import com.allthelucky.http.RequestListener; -import com.allthelucky.http.RequestManager; import com.app.library.common.view.R; /** @@ -149,28 +143,28 @@ public class ImageIndicatorView extends RelativeLayout { * * @return */ - protected ViewPager getViewPager() { + public ViewPager getViewPager() { return viewPager; } /** * 取当前位置Index值 */ - protected int getCurrentIndex() { + public int getCurrentIndex() { return this.currentIndex; } /** * 取总VIEW数目 */ - protected int getTotalCount() { + public int getTotalCount() { return this.totelCount; } /** * 取最近一次刷新时间 */ - protected long getRefreshTime() { + public long getRefreshTime() { return this.refreshTime; } @@ -236,75 +230,6 @@ public class ImageIndicatorView extends RelativeLayout { } } - /** - * 设置显示图片URL列表 - * - * @param urlList - * URL列表 - */ - public void setupLayoutByImageUrl(final List urlList) { - if (urlList == null) - throw new NullPointerException(); - - final int len = urlList.size(); - if (len > 0) { - for (int index = 0; index < len; index++) { - final ImageView pageItem = new ImageView(getContext()); - pageItem.setScaleType(ScaleType.FIT_XY); - loadImage(pageItem, urlList.get(index), R.drawable.ic_launcher); - addViewItem(pageItem); - } - } - } - - private void loadImage(final ImageView pageItem, final String imageUrl,final int imageResId ) { - /** - * load callback for RequestManager - */ - final RequestListener requestListener = new RequestListener() { - - @Override - public void onStart() { - - } - - @Override - public void onCompleted(int statusCode, byte[] data, long lastModified, String description, int actionId) { - if (RequestListener.ERR == statusCode) { - pageItem.setImageResource(imageResId); - } else { - if (null != data) { - BitmapFactory.Options o = new BitmapFactory.Options();// decode image size - o.inJustDecodeBounds = true; - BitmapFactory.decodeByteArray(data, 0, data.length, o); - - final int REQUIRED_SIZE = 100; // Find the correct scale value. - int width_tmp = o.outWidth, height_tmp = o.outHeight; - int scale = 1; - while (true) { - if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) - break; - width_tmp /= 2; - height_tmp /= 2; - scale *= 2; - } - - BitmapFactory.Options options = new Options();// decode with scale - options.inSampleSize = scale; - Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options); - if (bitmap != null) { - pageItem.setImageBitmap(bitmap); - } else { - pageItem.setImageResource(imageResId); - } - } - } - } - }; - - RequestManager.getInstance().get(getContext(), imageUrl, null, requestListener, true, 0); - } - /** * 设置当前显示项 * diff --git a/AndroidImageIndicator/src/com/allthelucky/common/view/network/NetworkApp.java b/AndroidImageIndicator/src/com/allthelucky/common/view/network/NetworkApp.java new file mode 100644 index 0000000..2ab5fc6 --- /dev/null +++ b/AndroidImageIndicator/src/com/allthelucky/common/view/network/NetworkApp.java @@ -0,0 +1,33 @@ +package com.allthelucky.common.view.network; + +import com.android.http.RequestManager; +import com.android.volley.toolbox.ImageLoader; + +import android.app.Application; + +/** + * @description NetworkApp + * + * @author steven-pan + * + */ +public class NetworkApp extends Application { + + private static ImageLoader sImageLoader = null; + + private final NetworkImageCache imageCacheMap = new NetworkImageCache(); + + public static ImageLoader getImageLoader() { + return sImageLoader; + } + + @Override + public void onCreate() { + super.onCreate(); + + RequestManager.getInstance().init(NetworkApp.this); + sImageLoader = new ImageLoader(RequestManager.getInstance() + .getRequestQueue(), imageCacheMap); + } + +} diff --git a/AndroidImageIndicator/src/com/allthelucky/common/view/network/NetworkImageCache.java b/AndroidImageIndicator/src/com/allthelucky/common/view/network/NetworkImageCache.java new file mode 100644 index 0000000..db29bf9 --- /dev/null +++ b/AndroidImageIndicator/src/com/allthelucky/common/view/network/NetworkImageCache.java @@ -0,0 +1,44 @@ +package com.allthelucky.common.view.network; + +import com.android.volley.toolbox.ImageLoader.ImageCache; + +import android.graphics.Bitmap; +import android.support.v4.util.LruCache; + +/** + * @description NetworkImageCache + * + * @auther steven-pan + */ +public class NetworkImageCache extends LruCache implements + ImageCache { + + public NetworkImageCache() { + this(getDefaultLruCacheSize()); + } + + public NetworkImageCache(int sizeInKiloBytes) { + super(sizeInKiloBytes); + } + + @Override + protected int sizeOf(String key, Bitmap value) { + return value.getRowBytes() * value.getHeight() / 1024; + } + + @Override + public Bitmap getBitmap(String url) { + return get(url); + } + + @Override + public void putBitmap(String url, Bitmap bitmap) { + put(url, bitmap); + } + + public static int getDefaultLruCacheSize() { + final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); + final int cacheSize = maxMemory / 8; + return cacheSize; + } +} diff --git a/AndroidImageIndicator/src/com/allthelucky/common/view/network/NetworkImageIndicatorView.java b/AndroidImageIndicator/src/com/allthelucky/common/view/network/NetworkImageIndicatorView.java new file mode 100644 index 0000000..0b791ca --- /dev/null +++ b/AndroidImageIndicator/src/com/allthelucky/common/view/network/NetworkImageIndicatorView.java @@ -0,0 +1,52 @@ +package com.allthelucky.common.view.network; + +import java.util.List; + +import android.content.Context; +import android.util.AttributeSet; +import android.widget.ImageView.ScaleType; + +import com.allthelucky.common.view.ImageIndicatorView; +import com.android.http.WebImageView; +import com.app.library.common.view.R; + +/** + * Network ImageIndicatorView, by urls + * + * @author steven-pan + * + */ +public class NetworkImageIndicatorView extends ImageIndicatorView { + + public NetworkImageIndicatorView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public NetworkImageIndicatorView(Context context) { + super(context); + } + + /** + * 设置显示图片URL列表 + * + * @param urlList + * URL列表 + */ + public void setupLayoutByImageUrl(final List urlList) { + if (urlList == null) + throw new NullPointerException(); + + final int len = urlList.size(); + if (len > 0) { + for (int index = 0; index < len; index++) { + final WebImageView pageItem = new WebImageView(getContext()); + pageItem.setScaleType(ScaleType.FIT_XY); + pageItem.setDefaultImageResId(R.drawable.ic_launcher); + pageItem.setImageUrl(urlList.get(index), + NetworkApp.getImageLoader()); + addViewItem(pageItem); + } + } + } + +} diff --git a/AndroidImageIndicator/src/com/allthelucky/common/view/sample/AutoImageIndicatorActivity.java b/AndroidImageIndicator/src/com/allthelucky/common/view/sample/AutoImageIndicatorActivity.java index 1086a12..4df5890 100644 --- a/AndroidImageIndicator/src/com/allthelucky/common/view/sample/AutoImageIndicatorActivity.java +++ b/AndroidImageIndicator/src/com/allthelucky/common/view/sample/AutoImageIndicatorActivity.java @@ -3,19 +3,19 @@ package com.allthelucky.common.view.sample; import android.app.Activity; import android.os.Bundle; -import com.allthelucky.common.view.AutoImageIndicatorView; +import com.allthelucky.common.view.AutoBrocastManager; import com.allthelucky.common.view.ImageIndicatorView; import com.app.library.common.view.R; public class AutoImageIndicatorActivity extends Activity { - private AutoImageIndicatorView autoImageIndicatorView; + private ImageIndicatorView autoImageIndicatorView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_indicator_auto); - this.autoImageIndicatorView = (AutoImageIndicatorView) findViewById(R.id.auto_indicate_view); + this.autoImageIndicatorView = (ImageIndicatorView) findViewById(R.id.auto_indicate_view); autoImageIndicatorView.setOnItemChangeListener(new ImageIndicatorView.OnItemChangeListener() { @Override public void onPosition(int position, int totalCount) { @@ -34,11 +34,16 @@ public class AutoImageIndicatorActivity extends Activity { private void initView() { final Integer[] resArray = new Integer[] { R.drawable.poster1, R.drawable.poster2, R.drawable.poster3 }; - this.autoImageIndicatorView.setBroadcastEnable(true); - this.autoImageIndicatorView.setBroadCastTimes(5);//循环播放5次 - this.autoImageIndicatorView.setBroadcastTimeIntevel(2 * 1000, 3 * 1000);//播放启动时间及间隔 + this.autoImageIndicatorView.setupLayoutByDrawable(resArray);//图片 this.autoImageIndicatorView.show(); + + AutoBrocastManager autoBrocastManager = new AutoBrocastManager(this.autoImageIndicatorView); + autoBrocastManager.setBroadcastEnable(true); + autoBrocastManager.setBroadCastTimes(5);//循环播放5次 + autoBrocastManager.setBroadcastTimeIntevel(2 * 1000, 3 * 1000);//播放启动时间及间隔 + autoBrocastManager.loop(); + } } diff --git a/AndroidImageIndicator/src/com/allthelucky/common/view/sample/SampleApp.java b/AndroidImageIndicator/src/com/allthelucky/common/view/sample/SampleApp.java new file mode 100644 index 0000000..e45a13c --- /dev/null +++ b/AndroidImageIndicator/src/com/allthelucky/common/view/sample/SampleApp.java @@ -0,0 +1,17 @@ +package com.allthelucky.common.view.sample; + +import com.allthelucky.common.view.network.NetworkApp; + +/** + * SampleApp + * + * @author steven-pan + * + */ +public class SampleApp extends NetworkApp { + @Override + public void onCreate() { + super.onCreate(); + } + +}