Android Open Source - lightbox-android-webservices Remote Thumb Image View






From Project

Back to project page lightbox-android-webservices.

License

The source code is released under:

Apache License

If you think the Android project lightbox-android-webservices listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright (c) 2012 Lightbox// w  ww  . j av  a 2  s.  c o  m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lightbox.android.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;

import com.lightbox.android.bitmap.BitmapLoader;
import com.lightbox.android.bitmap.BitmapLoader.Volatility;
import com.lightbox.android.bitmap.BitmapLoaderListener;
import com.lightbox.android.bitmap.BitmapSource;
import com.lightbox.android.bitmap.BitmapSource.Type;
import com.lightbox.android.cache.BitmapCache;
import com.lightbox.android.utils.debug.DebugLog;

/**
 * RemoteThumbImageView
 * 
 * @author Nilesh Patel
 */
public class RemoteThumbImageView extends ImageView implements BitmapLoaderListener {
  /** Used to tag logs */
  private static final String TAG = "RemoteThumbImageView";

  private BitmapSource mCurrentImageSource = null;
  private BitmapLoader mBitmapLoader;

  /**
   * Constructor.
   * 
   * @param context
   * @param attrs
   * @param defStyle
   */
  public RemoteThumbImageView(Context context, AttributeSet attrs,
      int defStyle) {
    super(context, attrs, defStyle);
    init(context);
  }

  /**
   * Constructor.
   * 
   * @param context
   * @param attrs
   */
  public RemoteThumbImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
  }

  /**
   * Constructor.
   * 
   * @param context
   */
  public RemoteThumbImageView(Context context) {
    super(context);
    init(context);
  }

  /**
   * @param context
   */
  private void init(Context context) {
    mBitmapLoader = new BitmapLoader();

    setScaleType(ScaleType.CENTER_CROP);
    ViewGroup.LayoutParams fgParams = new ViewGroup.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    setLayoutParams(fgParams);
//    setBackgroundColor(getResources().getColor(R.color.thumbBackground));
  }

  public void startLoading(BitmapSource bitmapSource) {
    if (mCurrentImageSource == null || !mCurrentImageSource.getAbsoluteFileName(Type.THM).equals(bitmapSource.getAbsoluteFileName(Type.THM))) {
      // Different image than the current one, need to load
      mCurrentImageSource = bitmapSource;
      mBitmapLoader.cancel();
      reset();
      mBitmapLoader.loadAsync(bitmapSource, Type.THM, Config.RGB_565, Volatility.DEFAULT, this);
    }
  }

  private BitmapDrawable mBitmapDrawable;
  @Override
  public void onLoaded(Bitmap bitmap, Type type, boolean isFromMemCache) {
    if (mFadeRunnable.isFading()) {
      return;
    }
    if (mBitmapDrawable == null) {
      if (isFromMemCache) {
        mBitmapDrawable = new BitmapDrawable(bitmap);
        setImageDrawable(mBitmapDrawable);
        invalidate();
      } else {
        mBitmapDrawable = new BitmapDrawable(bitmap);
        mBitmapDrawable.setAlpha(0);
        setImageDrawable(mBitmapDrawable);
        invalidate();
        mFadeRunnable.start();
      }
    }
  }

  public void loadOnlyIfInMemCache(BitmapSource bitmapSource, int position) {    
    mCurrentImageSource = bitmapSource;
    Bitmap bitmap = BitmapCache.getInstance().getFromMemory(bitmapSource.getAbsoluteFileName(Type.THM));
    if (bitmap != null && (mBitmapDrawable == null || mBitmapDrawable.getBitmap() != bitmap)) {
      mBitmapDrawable = new BitmapDrawable(bitmap);
      setImageDrawable(mBitmapDrawable);
      invalidate();
    }
  }
  
  public void cancelLoading() {
    mBitmapLoader.cancel();
    if (mFadeRunnable.isFading()) {
      mFadeRunnable.stop();
    }
  }
  
  public void resumeLoading() {
    if (mFadeRunnable.isFading()) {
      return;
    }
    if (mCurrentImageSource != null) {
      if (mBitmapDrawable != null) {
        mBitmapDrawable.setAlpha(255);
        invalidate();
      } else {
        mBitmapLoader.loadAsync(mCurrentImageSource, Type.THM, Config.RGB_565, Volatility.DEFAULT, this);
      }
    }
  }
  
  @Override
  public void onFailure(Exception e) {
    DebugLog.d(TAG, "onFailure: %s", e);

    setImageResource(android.R.color.white);
  }

  public void reset() {
    mFadeRunnable.stop();
    mBitmapDrawable = null;
    setImageDrawable(null);
    invalidate();
  }
  
  public void resetIfWrongSource(BitmapSource bitmapSource) {
    if (mCurrentImageSource == null || !bitmapSource.getAbsoluteFileName(Type.THM).equals(mCurrentImageSource.getAbsoluteFileName(Type.THM))) {
      reset();
    }
  }
  
  private FadeRunnable mFadeRunnable = new FadeRunnable();
  private static final int FADE_DURATION = 400;
  private class FadeRunnable implements Runnable {
    private boolean mIsRunning;
    private long mStartTime;
    
    public void start() {
      if (mBitmapDrawable.getPaint().getAlpha() == 255) {
        return;
      }
      
      if (mIsRunning) {
        return;
      }
      
      mIsRunning = true;
      mStartTime = System.currentTimeMillis();
      
      RemoteThumbImageView.this.post(this);
    }
    
    public void stop() {
      mIsRunning = false;
      RemoteThumbImageView.this.removeCallbacks(this);
    }
    
    public boolean isFading() {
      return mIsRunning;
    }
    
    @Override
    public void run() {
      long elapsedTime = System.currentTimeMillis() - mStartTime;
      
      int alpha = Math.min((int)(255 * (float)(elapsedTime)/FADE_DURATION), 255);
      if (mBitmapDrawable != null) {
        mBitmapDrawable.setAlpha(alpha);
        invalidate();
      }
    
      if (mIsRunning && alpha < 255) {
        RemoteThumbImageView.this.post(this);
      } else {
        mIsRunning = false;
      }
    }    
  }
}




Java Source Code List

com.lightbox.android.bitmap.BitmapFileCleanerTask.java
com.lightbox.android.bitmap.BitmapLoaderListener.java
com.lightbox.android.bitmap.BitmapLoaderTask.java
com.lightbox.android.bitmap.BitmapLoader.java
com.lightbox.android.bitmap.BitmapSize.java
com.lightbox.android.bitmap.BitmapSource.java
com.lightbox.android.bitmap.BitmapUtils.java
com.lightbox.android.cache.AbstractCache.java
com.lightbox.android.cache.ApiCache.java
com.lightbox.android.cache.BitmapCache.java
com.lightbox.android.cache.Cache.java
com.lightbox.android.data.ClearAndSaveBatchTask.java
com.lightbox.android.data.Data.java
com.lightbox.android.data.DatabaseCleanerTask.java
com.lightbox.android.data.DatabaseHelper.java
com.lightbox.android.data.DeleteBatchTask.java
com.lightbox.android.data.SaveBatchTask.java
com.lightbox.android.io.RandomAccessFileOutputStream.java
com.lightbox.android.lifecycle.LifeCycleListener.java
com.lightbox.android.lifecycle.ManagedLifeCycleActivity.java
com.lightbox.android.location.LocationHelper.java
com.lightbox.android.location.LocationListener.java
com.lightbox.android.network.HttpHelper.java
com.lightbox.android.network.NetworkUtils.java
com.lightbox.android.operations.AbstractOperation.java
com.lightbox.android.operations.CachedOperation.java
com.lightbox.android.operations.DeleteOperation.java
com.lightbox.android.operations.FailureOperation.java
com.lightbox.android.operations.ModificationNetworkOnlyOperation.java
com.lightbox.android.operations.NetworkOnlyOperation.java
com.lightbox.android.operations.OperationListener.java
com.lightbox.android.operations.OperationTask.java
com.lightbox.android.operations.Operation.java
com.lightbox.android.operations.Retrievable.java
com.lightbox.android.operations.RetrieveOperation.java
com.lightbox.android.operations.SaveOperation.java
com.lightbox.android.operations.Updatable.java
com.lightbox.android.tasks.BackgroundTaskWeak.java
com.lightbox.android.tasks.BackgroundTask.java
com.lightbox.android.tasks.DefaultExecutor.java
com.lightbox.android.utils.AndroidUtils.java
com.lightbox.android.utils.Base64.java
com.lightbox.android.utils.IntentUtils.java
com.lightbox.android.utils.MediaUtils.java
com.lightbox.android.utils.ResUtils.java
com.lightbox.android.utils.debug.DebugLifeCycleListener.java
com.lightbox.android.utils.debug.DebugLog.java
com.lightbox.android.views.RemoteImageView.java
com.lightbox.android.views.RemoteThumbImageView.java
com.lightbox.android.webservices.processors.GenerationException.java
com.lightbox.android.webservices.processors.JacksonProcessor.java
com.lightbox.android.webservices.processors.ParsingException.java
com.lightbox.android.webservices.processors.Processor.java
com.lightbox.android.webservices.requests.ApiRequestFactory.java
com.lightbox.android.webservices.requests.ApiRequestListener.java
com.lightbox.android.webservices.requests.ApiRequestTask.java
com.lightbox.android.webservices.requests.ApiRequest.java
com.lightbox.android.webservices.requests.ApiRequests.java
com.lightbox.android.webservices.requests.JacksonApiRequestFactory.java
com.lightbox.android.webservices.responses.ApiException.java
com.lightbox.android.webservices.responses.ApiResponse.java
com.lightbox.tweetsnearby.TweetsNearbyApplication.java
com.lightbox.tweetsnearby.activities.MainActivity.java
com.lightbox.tweetsnearby.activities.PickPlaceActivity.java
com.lightbox.tweetsnearby.model.Tweet.java
com.lightbox.tweetsnearby.model.Venue.java
com.lightbox.tweetsnearby.responses.foursquare.FoursquareApiResponse.java
com.lightbox.tweetsnearby.responses.foursquare.VenueListResponse.java
com.lightbox.tweetsnearby.responses.twitter.TweetListResponse.java
com.lightbox.tweetsnearby.responses.twitter.TwitterApiResponse.java