示例#1
0
 private ImageRequest getImageRequest(LDraweeView view, Uri uri) {
   switch (mImageFormat) {
     case JPEG:
     case PNG:
       return ImageRequestBuilder.newBuilderWithSource(uri)
           .setImageType(view.getImageType())
           .setLowestPermittedRequestLevel(view.getLowestPermittedRequestLevel())
           .setPostprocessor(view.getPostProcessor()) // 修改图片
           .setResizeOptions(view.getResizeOptions())
           .setAutoRotateEnabled(true)
           .setLocalThumbnailPreviewsEnabled(true)
           .setProgressiveRenderingEnabled(false)
           .build();
     case GIF:
       if (uri.getScheme().toLowerCase().contains("file") && isCutGif()) {
         // 针对本地Gif预览时做特殊处理,裁剪出第一帧并显示
         File file = new File(uri.getPath());
         //                    File cutFile = Utils.getCopyFile(file);
         Uri newUri = new Uri.Builder().scheme("file").path(file.getAbsolutePath()).build();
         return ImageRequestBuilder.newBuilderWithSource(newUri)
             .setLowestPermittedRequestLevel(view.getLowestPermittedRequestLevel())
             .setPostprocessor(view.getPostProcessor())
             .setResizeOptions(view.getResizeOptions())
             .setAutoRotateEnabled(true)
             .build();
       } else {
         return ImageRequestBuilder.newBuilderWithSource(uri)
             .setLowestPermittedRequestLevel(view.getLowestPermittedRequestLevel())
             .setAutoRotateEnabled(true)
             .build();
       }
   }
   throw new RuntimeException("must have a ImageRequest");
 }
  public void maybeUpdateView() {
    if (!mIsDirty) {
      return;
    }

    boolean doResize = shouldResize(mUri);
    if (doResize && (getWidth() <= 0 || getHeight() <= 0)) {
      // If need a resize and the size is not yet set, wait until the layout pass provides one
      return;
    }

    GenericDraweeHierarchy hierarchy = getHierarchy();
    hierarchy.setActualImageScaleType(mScaleType);

    boolean usePostprocessorScaling =
        mScaleType != ScalingUtils.ScaleType.CENTER_CROP
            && mScaleType != ScalingUtils.ScaleType.FOCUS_CROP;
    float hierarchyRadius = usePostprocessorScaling ? 0 : mBorderRadius;

    RoundingParams roundingParams = hierarchy.getRoundingParams();
    roundingParams.setCornersRadius(hierarchyRadius);
    roundingParams.setBorder(mBorderColor, mBorderWidth);
    hierarchy.setRoundingParams(roundingParams);
    hierarchy.setFadeDuration(
        mFadeDurationMs >= 0 ? mFadeDurationMs : mIsLocalImage ? 0 : REMOTE_IMAGE_FADE_DURATION_MS);

    Postprocessor postprocessor = usePostprocessorScaling ? mRoundedCornerPostprocessor : null;

    ResizeOptions resizeOptions = doResize ? new ResizeOptions(getWidth(), getHeight()) : null;

    ImageRequest imageRequest =
        ImageRequestBuilder.newBuilderWithSource(mUri)
            .setPostprocessor(postprocessor)
            .setResizeOptions(resizeOptions)
            .setProgressiveRenderingEnabled(mProgressiveRenderingEnabled)
            .build();

    // This builder is reused
    mDraweeControllerBuilder.reset();

    mDraweeControllerBuilder
        .setAutoPlayAnimations(true)
        .setCallerContext(mCallerContext)
        .setOldController(getController())
        .setImageRequest(imageRequest);

    if (mControllerListener != null && mControllerForTesting != null) {
      ForwardingControllerListener combinedListener = new ForwardingControllerListener();
      combinedListener.addListener(mControllerListener);
      combinedListener.addListener(mControllerForTesting);
      mDraweeControllerBuilder.setControllerListener(combinedListener);
    } else if (mControllerForTesting != null) {
      mDraweeControllerBuilder.setControllerListener(mControllerForTesting);
    } else if (mControllerListener != null) {
      mDraweeControllerBuilder.setControllerListener(mControllerListener);
    }

    setController(mDraweeControllerBuilder.build());
    mIsDirty = false;
  }
  public static void getBitmapFromUri(
      Context context, Uri imageUri, final ImageUtilCallback callback) {
    ImagePipeline imagePipeline = Fresco.getImagePipeline();

    ImageRequest imageRequest =
        ImageRequestBuilder.newBuilderWithSource(imageUri)
            .setRequestPriority(Priority.HIGH)
            .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.FULL_FETCH)
            .build();

    DataSource<CloseableReference<CloseableImage>> dataSource =
        imagePipeline.fetchDecodedImage(imageRequest, context);

    try {
      dataSource.subscribe(
          new BaseBitmapDataSubscriber() {
            @Override
            public void onNewResultImpl(@Nullable Bitmap bitmap) {
              if (bitmap == null) {
                // Log.d(TAG, "Bitmap data source returned success, but bitmap null.");
                return;
              }
              // The bitmap provided to this method is only guaranteed to be around
              // for the lifespan of this method. The image pipeline frees the
              // bitmap's memory after this method has completed.
              //
              // This is fine when passing the bitmap to a system process as
              // Android automatically creates a copy.
              //
              // If you need to keep the bitmap around, look into using a
              // BaseDataSubscriber instead of a BaseBitmapDataSubscriber.

              callback.onBitmapLoaded(bitmap);
            }

            @Override
            public void onFailureImpl(DataSource dataSource) {
              // No cleanup required here
            }
          },
          CallerThreadExecutor.getInstance());
    } finally {
      if (dataSource != null) {
        dataSource.close();
      }
    }
  }
  public void bindRaw(String fileName) {
    if (bindedFile != null) {
      bindedFile.detach();
      bindedFile = null;
    }
    currentId = 0;

    ImageRequest request =
        ImageRequestBuilder.newBuilderWithSource(Uri.fromFile(new File(fileName)))
            .setResizeOptions(new ResizeOptions(size, size))
            .setAutoRotateEnabled(true)
            .build();
    PipelineDraweeController controller =
        (PipelineDraweeController)
            Fresco.newDraweeControllerBuilder()
                .setOldController(getController())
                .setImageRequest(request)
                .build();
    setController(controller);
  }
示例#5
0
 /**
  * Submits a request for execution and returns a DataSource representing the pending encoded
  * image(s).
  *
  * <p>The ResizeOptions in the imageRequest will be ignored for this fetch
  *
  * <p>The returned DataSource must be closed once the client has finished with it.
  *
  * @param imageRequest the request to submit
  * @return a DataSource representing the pending encoded image(s)
  */
 public DataSource<CloseableReference<PooledByteBuffer>> fetchEncodedImage(
     ImageRequest imageRequest, Object callerContext) {
   Preconditions.checkNotNull(imageRequest.getSourceUri());
   try {
     Producer<CloseableReference<PooledByteBuffer>> producerSequence =
         mProducerSequenceFactory.getEncodedImageProducerSequence(imageRequest);
     // The resize options are used to determine whether images are going to be downsampled during
     // decode or not. For the case where the image has to be downsampled and it's a local image it
     // will be kept as a FileInputStream until decoding instead of reading it in memory. Since
     // this method returns an encoded image, it should always be read into memory. Therefore, the
     // resize options are ignored to avoid treating the image as if it was to be downsampled
     // during decode.
     if (imageRequest.getResizeOptions() != null) {
       imageRequest = ImageRequestBuilder.fromRequest(imageRequest).setResizeOptions(null).build();
     }
     return submitFetchRequest(
         producerSequence, imageRequest, ImageRequest.RequestLevel.FULL_FETCH, callerContext);
   } catch (Exception exception) {
     return DataSources.immediateFailedDataSource(exception);
   }
 }
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    mContext = getActivity();

    final View view = inflater.inflate(R.layout.fragment_detail_info, container, false);

    Intent intent = getActivity().getIntent();

    int hero_id = -2;
    if (intent != null) {
      hero_id = intent.getIntExtra(MainActivityFragment.TAG_HERO_ID, -1);
    }

    HeroesDBAdapter adapter = new HeroesDBAdapter(getActivity());
    adapter.open();

    try {
      mCharacter = adapter.getItem(hero_id);
    } catch (EmptyCursorException e) {
      Log.e(LOG_TAG, " EmptyCursorException");
      Crashlytics.log(Log.ERROR, LOG_TAG, " EmptyCursorException");
      e.printStackTrace();
    }

    ImagePipeline imagePipeline = Fresco.getImagePipeline();

    final ImageItem imageItem = mCharacter.getThumbnail();
    Uri uri = Uri.parse(imageItem.toString());

    ImageRequest imageRequest =
        ImageRequestBuilder.newBuilderWithSource(uri)
            .setRequestPriority(Priority.HIGH)
            .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.FULL_FETCH)
            .build();

    DataSource<CloseableReference<CloseableImage>> dataSource =
        imagePipeline.fetchDecodedImage(imageRequest, getActivity());

    try {
      dataSource.subscribe(
          new BaseBitmapDataSubscriber() {
            @Override
            public void onNewResultImpl(@Nullable Bitmap bitmap) {
              if (bitmap == null) {
                Crashlytics.log(
                    Log.ERROR, LOG_TAG, "Bitmap data source returned success, but bitmap null.");

                return;
              } else {
                mBitmap = bitmap;
                ImageView imageView = (ImageView) view.findViewById(R.id.imageView_detail);
                imageView.setImageBitmap(mBitmap);
              }
            }

            @Override
            public void onFailureImpl(DataSource dataSource) {
              Crashlytics.log(Log.ERROR, LOG_TAG, "Bitmap data source returned error");
            }
          },
          CallerThreadExecutor.getInstance());
    } finally {
      if (dataSource != null) {
        dataSource.close();
      }
    }

    TextView textView = (TextView) view.findViewById(R.id.heroe_name);
    textView.setText(mCharacter.getName());

    textView = (TextView) view.findViewById(R.id.heroe_description);
    textView.setText(mCharacter.getDescription());

    adapter.close();

    return view;
  }