Android Open Source - android-screenshot-gallery Picture View






From Project

Back to project page android-screenshot-gallery.

License

The source code is released under:

Apache License

If you think the Android project android-screenshot-gallery 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

package com.demondevelopers.screenshotgallery;
/*from w ww .  j ava 2s  .com*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.widget.ImageView;


public class PictureView extends ImageView
{
  private PictureRequest mRequest;
  private OnAsyncCompleted mOnAsyncCompleted;
  
  
  public PictureView(Context context)
  {
    super(context);
    initView(context, null);
  }
  
  public PictureView(Context context, AttributeSet attrs)
  {
    super(context, attrs);
    initView(context, attrs);
  }
  
  public PictureView(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs, defStyle);
    initView(context, attrs);
  }
  
  protected void initView(Context context, AttributeSet attrs)
  {
    setAdjustViewBounds(true);
  }
  
  public void setOnAsyncCompleted(OnAsyncCompleted onAsyncCompleted)
  {
    mOnAsyncCompleted = onAsyncCompleted;
  }
  
  private void dispatchOnAsyncCompleted(boolean successful, String filePath)
  {
    if(mOnAsyncCompleted != null){
      mOnAsyncCompleted.onAsyncCompleted(this, successful, filePath);
    }
  }
  
  private String createCacheKey(String filePath)
  {
    return "image-" + filePath;
  }
  
  public void setImageAsync(String filePath)
  {
    Bitmap cached = GalleryApp.from(getContext())
      .getImageFromCache(createCacheKey(filePath));
    if(cached == null){
      cancelImageRequest();
      setImageResource(R.drawable.ic_picture);
      mRequest = new PictureRequest(filePath);
      mRequest.execute();
    }
    else{
      setImageBitmap(cached);
    }
  }
  
  private void cancelImageRequest()
  {
    if(mRequest != null && !mRequest.isCancelled()){
      mRequest.cancel();
    }
  }
  
  @Override
  protected void onDetachedFromWindow()
  {
    super.onDetachedFromWindow();
    cancelImageRequest();
  }
  
  
  private class PictureRequest extends AsyncTask<Void, Void, Bitmap>
  {
    private String mFilePath;
    private BitmapFactory.Options mOptions;
    
    
    public PictureRequest(String filePath)
    {
      mFilePath = filePath;
    }
    
    @Override
    protected Bitmap doInBackground(Void... params)
    {
      mOptions = new BitmapFactory.Options();
      return ImageUtils.getDisplaySizedBitmap(getContext(), mFilePath, mOptions);
    }
    
    @Override
    protected void onPostExecute(Bitmap result)
    {
      if(result != null){
        GalleryApp.from(getContext())
          .putImageIntoCache(createCacheKey(mFilePath), result);
        setImageBitmap(result);
        dispatchOnAsyncCompleted(true, mFilePath);
      }
      else{
        dispatchOnAsyncCompleted(false, mFilePath);
      }
    }

    public void cancel()
    {
      super.cancel(false);
      if(mOptions != null){
        mOptions.requestCancelDecode();
      }
    }
  }
  
  public interface OnAsyncCompleted
  {
    public void onAsyncCompleted(PictureView view, boolean successful, String filePath);
  }
}




Java Source Code List

com.demondevelopers.screenshotgallery.AboutFragment.java
com.demondevelopers.screenshotgallery.BaseActivity.java
com.demondevelopers.screenshotgallery.BaseFragment.java
com.demondevelopers.screenshotgallery.FitTopDecorOnlyLayout.java
com.demondevelopers.screenshotgallery.GalleryApp.java
com.demondevelopers.screenshotgallery.ImageUtils.java
com.demondevelopers.screenshotgallery.MainActivity.java
com.demondevelopers.screenshotgallery.OverlayDecorLayout.java
com.demondevelopers.screenshotgallery.PictureActivity.java
com.demondevelopers.screenshotgallery.PictureFragment.java
com.demondevelopers.screenshotgallery.PictureGridFragment.java
com.demondevelopers.screenshotgallery.PictureView.java
com.demondevelopers.screenshotgallery.ThumbView.java