Skip to content

Commit

Permalink
Showcase: Remove SimpleDraweeView reference for PartialRequestFragmen…
Browse files Browse the repository at this point in the history
…t sample

Reviewed By: defHLT, kartavya-ramnani

Differential Revision: D63710287

fbshipit-source-id: 33037f79c5bc68aad38253b8b6da4de55b661613
  • Loading branch information
oprisnik authored and facebook-github-bot committed Jan 24, 2025
1 parent 81b7d8d commit 3868b25
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,30 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.fresco.samples.showcase.BaseShowcaseFragment;
import com.facebook.fresco.samples.showcase.R;
import com.facebook.fresco.samples.showcase.misc.ImageUriProvider;
import com.facebook.fresco.samples.showcase.vito.source.ImageRequestImageSource;
import com.facebook.fresco.vito.options.ImageOptions;
import com.facebook.fresco.vito.view.VitoView;
import com.facebook.imagepipeline.common.BytesRange;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

public class PartialRequestFragment extends BaseShowcaseFragment {

private static final String CALLER_CONTEXT = "PartialRequestFragment";

private static final ImageOptions IMAGE_OPTIONS =
ImageOptions.create()
.placeholderRes(R.mipmap.ic_launcher, ScalingUtils.ScaleType.CENTER)
.scale(ScalingUtils.ScaleType.CENTER_CROP)
.build();

public PartialRequestFragment() {
// Required empty public constructor
}
Expand All @@ -39,18 +50,17 @@ public View onCreateView(
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

final SimpleDraweeView partialDrawee =
(SimpleDraweeView) view.findViewById(R.id.drawee_partial_img);
final ImageView partialImage = (ImageView) view.findViewById(R.id.partial_img);

final SimpleDraweeView fullDrawee = (SimpleDraweeView) view.findViewById(R.id.drawee_full_img);
final ImageView fullImage = (ImageView) view.findViewById(R.id.full_img);

final Button clearCacheButton = (Button) view.findViewById(R.id.clear_cache);
clearCacheButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
partialDrawee.resetActualImage();
fullDrawee.resetActualImage();
VitoView.release(partialImage);
VitoView.release(fullImage);
Fresco.getImagePipeline().clearDiskCaches();
Fresco.getImagePipeline().clearMemoryCaches();
}
Expand All @@ -61,7 +71,7 @@ public void onClick(View v) {
new View.OnClickListener() {
@Override
public void onClick(View v) {
loadImageIntoDrawee(partialDrawee, BytesRange.toMax(30000));
loadImage(partialImage, BytesRange.toMax(30000));
}
});

Expand All @@ -70,24 +80,18 @@ public void onClick(View v) {
new View.OnClickListener() {
@Override
public void onClick(View v) {
loadImageIntoDrawee(fullDrawee, null);
loadImage(fullImage, null);
}
});
}

private void loadImageIntoDrawee(SimpleDraweeView draweeView, @Nullable BytesRange bytesRange) {
private void loadImage(ImageView imageView, @Nullable BytesRange bytesRange) {
final ImageRequest imageRequest =
ImageRequestBuilder.newBuilderWithSource(
sampleUris().createSampleUri(ImageUriProvider.ImageSize.L))
.setBytesRange(bytesRange)
.build();

final DraweeController draweeController =
Fresco.newDraweeControllerBuilder()
.setOldController(draweeView.getController())
.setImageRequest(imageRequest)
.build();

draweeView.setController(draweeController);
VitoView.show(
new ImageRequestImageSource(imageRequest), IMAGE_OPTIONS, CALLER_CONTEXT, imageView);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.fresco.samples.showcase.vito.source

import android.net.Uri
import com.facebook.fresco.vito.core.ImagePipelineUtils
import com.facebook.fresco.vito.core.impl.source.ImagePipelineImageSource
import com.facebook.fresco.vito.options.ImageOptions
import com.facebook.imagepipeline.request.ImageRequest
import com.facebook.imagepipeline.request.ImageRequest.RequestLevel

/** Custom ImageSource that takes an ImageRequest for the ImagePipeline */
data class ImageRequestImageSource(private val imageRequest: ImageRequest) :
ImagePipelineImageSource {
override fun maybeExtractFinalImageRequest(
imagePipelineUtils: ImagePipelineUtils,
imageOptions: ImageOptions
): ImageRequest? {
return imagePipelineUtils.wrapDecodedImageRequest(imageRequest, imageOptions)
}

override val imageUri: Uri = imageRequest.sourceUri

override val extras: Map<String, Any> = emptyMap()

override fun getRequestLevelForFetch(): RequestLevel = RequestLevel.FULL_FETCH
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@
/>
</LinearLayout>

<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/drawee_partial_img"
<ImageView
android:id="@+id/partial_img"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:placeholderImage="@mipmap/ic_launcher"
app:placeholderImageScaleType="center"
app:actualImageScaleType="centerCrop"
/>
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/drawee_full_img"
<ImageView
android:id="@+id/full_img"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
Expand Down

0 comments on commit 3868b25

Please sign in to comment.