Android Open Source - volley Simple Displayer






From Project

Back to project page volley.

License

The source code is released under:

Apache License

If you think the Android project volley 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.android.volley.ext.display;
/* ww  w . j a v a 2 s . c o m*/
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.android.volley.ext.tools.BitmapTools.BitmapDisplayConfig;

public class SimpleDisplayer implements IDisplayer {
  
    @Override
  public void loadCompletedisplay(View imageView,Bitmap bitmap,BitmapDisplayConfig config){
      if (imageView == null) return;
      Drawable bitmapDrawable = null;
        if (config.roundConfig != null) {
            bitmapDrawable = new RoundedDrawable(bitmap, config.roundConfig.radius, config.roundConfig.margin);
            // BitmapUtil.getRoundedCornerBitmapDrawable((BitmapDrawable)bitmapDrawable);
        } else {
            bitmapDrawable = new BitmapDrawable(imageView.getResources(), bitmap);
        }
      if (config.isImmediate) {
          
          if(imageView instanceof ImageView){
              ((ImageView)imageView).setImageDrawable(bitmapDrawable);
          }else{
              imageView.setBackgroundDrawable(bitmapDrawable);
          }
      } else {
          switch (config.animationType) {
          case BitmapDisplayConfig.AnimationType.fadeIn:
              fadeInDisplay(imageView,bitmapDrawable);
              break;
          case BitmapDisplayConfig.AnimationType.userDefined:
              animationDisplay(imageView,bitmapDrawable,config.animation);
              break;
          default:
              break;
          }
      }
  }
    
    @Override
  public void loadFailDisplay(View view, BitmapDisplayConfig config){
      if (view == null) return;
        if(view instanceof ImageView){
            ImageView img = (ImageView) view;
            if (config.errorImageResId != 0) {
                img.setImageResource(config.errorImageResId);
            } else {
                img.setImageBitmap(null);
            }
        }else{
            if (config.errorImageResId != 0) {
                view.setBackgroundResource(config.errorImageResId);
            } else {
                view.setBackgroundDrawable(null);
            }
        }
    }
  
    @Override
  public void loadDefaultDisplay(View view, BitmapDisplayConfig config) {
      if (view == null) return;
      if (view instanceof ImageView) {
            ImageView img = (ImageView) view;
            if (config.defaultImageResId != 0) {
                img.setImageResource(config.defaultImageResId);
            } else {
                img.setImageBitmap(config.defaultBitmap);
            }
        } else {
            if (config.defaultImageResId != 0) {
                view.setBackgroundResource(config.defaultImageResId);
            } else if (config.defaultBitmap != null) {
                view.setBackgroundDrawable(new BitmapDrawable(view.getResources(), config.defaultBitmap));
            } else {
                view.setBackgroundDrawable(null);
            }
        }
  }

  private void fadeInDisplay(View imageView,Drawable bitmapDrawable){
      if (bitmapDrawable == null) {
          return;
      }
        final TransitionDrawable td =
                new TransitionDrawable(new Drawable[] {
                        new ColorDrawable(android.R.color.transparent),
                        bitmapDrawable
                });
        if(imageView instanceof ImageView){
            ((ImageView)imageView).setImageDrawable(td);
        }else{
            imageView.setBackgroundDrawable(td);
        }
        td.startTransition(200);
    }
    
    
    private void animationDisplay(View imageView,Drawable bitmapDrawable,Animation animation){
        if (bitmapDrawable == null) {
            return;
        }
        if (animation != null) {
            animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());        
        }
        if(imageView instanceof ImageView){
            ((ImageView)imageView).setImageDrawable(bitmapDrawable);
        }else{
            imageView.setBackgroundDrawable(bitmapDrawable);
        }
        if (animation != null) {
            imageView.startAnimation(animation);
        }
    }
    
    public static class RoundedDrawable extends Drawable {

        protected final float cornerRadius;
        protected final int margin;

        protected final RectF mRect = new RectF(),
                mBitmapRect;
        protected final BitmapShader bitmapShader;
        protected final Paint paint;

        public RoundedDrawable(Bitmap bitmap, int cornerRadius, int margin) {
            this.cornerRadius = cornerRadius;
            this.margin = margin;

            bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            mBitmapRect = new RectF (margin, margin, bitmap.getWidth() - margin, bitmap.getHeight() - margin);
            
            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setShader(bitmapShader);
        }

        @Override
        protected void onBoundsChange(Rect bounds) {
            super.onBoundsChange(bounds);
            mRect.set(margin, margin, bounds.width() - margin, bounds.height() - margin);
            
            // Resize the original bitmap to fit the new bound
            Matrix shaderMatrix = new Matrix();
            shaderMatrix.setRectToRect(mBitmapRect, mRect, Matrix.ScaleToFit.FILL);
            bitmapShader.setLocalMatrix(shaderMatrix);
            
        }

        @Override
        public void draw(Canvas canvas) {
            canvas.drawRoundRect(mRect, cornerRadius, cornerRadius, paint);
        }

        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }

        @Override
        public void setAlpha(int alpha) {
            paint.setAlpha(alpha);
        }

        @Override
        public void setColorFilter(ColorFilter cf) {
            paint.setColorFilter(cf);
        }
    }
}




Java Source Code List

com.android.volley.AuthFailureError.java
com.android.volley.CacheDispatcher.java
com.android.volley.Cache.java
com.android.volley.DefaultRetryPolicy.java
com.android.volley.ExecutorDelivery.java
com.android.volley.NetworkDispatcher.java
com.android.volley.NetworkError.java
com.android.volley.NetworkResponse.java
com.android.volley.Network.java
com.android.volley.NoConnectionError.java
com.android.volley.ParseError.java
com.android.volley.RequestQueue.java
com.android.volley.Request.java
com.android.volley.ResponseDelivery.java
com.android.volley.Response.java
com.android.volley.RetryPolicy.java
com.android.volley.ServerError.java
com.android.volley.TimeoutError.java
com.android.volley.Utils.java
com.android.volley.VolleyError.java
com.android.volley.VolleyLog.java
com.android.volley.ext.ContentLengthInputStream.java
com.android.volley.ext.HttpCallback.java
com.android.volley.ext.PauseOnScrollListener.java
com.android.volley.ext.RequestInfo.java
com.android.volley.ext.display.IDisplayer.java
com.android.volley.ext.display.SimpleDisplayer.java
com.android.volley.ext.tools.BitmapTools.java
com.android.volley.ext.tools.HttpTools.java
com.android.volley.toolbox.AndroidAuthenticator.java
com.android.volley.toolbox.Authenticator.java
com.android.volley.toolbox.BasicNetwork.java
com.android.volley.toolbox.BitmapCache.java
com.android.volley.toolbox.BitmapDecoder.java
com.android.volley.toolbox.ByteArrayPool.java
com.android.volley.toolbox.ClearCacheRequest.java
com.android.volley.toolbox.ContentLengthInputStream.java
com.android.volley.toolbox.DiskBasedCache.java
com.android.volley.toolbox.DiskLruBasedCache.java
com.android.volley.toolbox.DownloadRequest.java
com.android.volley.toolbox.HttpClientStack.java
com.android.volley.toolbox.HttpHeaderParser.java
com.android.volley.toolbox.HttpStack.java
com.android.volley.toolbox.HurlStack.java
com.android.volley.toolbox.ImageLoader.java
com.android.volley.toolbox.ImageRequest.java
com.android.volley.toolbox.InflatingEntity.java
com.android.volley.toolbox.JsonArrayRequest.java
com.android.volley.toolbox.JsonObjectRequest.java
com.android.volley.toolbox.JsonRequest.java
com.android.volley.toolbox.MultiPartRequest.java
com.android.volley.toolbox.NetworkImageView.java
com.android.volley.toolbox.NoCache.java
com.android.volley.toolbox.PoolingByteArrayOutputStream.java
com.android.volley.toolbox.RequestFuture.java
com.android.volley.toolbox.StringRequest.java
com.android.volley.toolbox.UploadMultipartEntity.java
com.android.volley.toolbox.Volley.java
com.android.volley.toolbox.disklrucache.DiskLruCache.java
com.android.volley.toolbox.disklrucache.StrictLineReader.java
com.android.volley.toolbox.disklrucache.Util.java
com.android.volley.toolbox.multipart.BasePart.java
com.android.volley.toolbox.multipart.Boundary.java
com.android.volley.toolbox.multipart.FilePart.java
com.android.volley.toolbox.multipart.MultipartEntity.java
com.android.volley.toolbox.multipart.Part.java
com.android.volley.toolbox.multipart.StringPart.java
com.android.volley.toolbox.multipart.UrlEncodingHelper.java