Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Fresco support #641

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ private void checkAndDisplayMatrix() {
* @param matrix - Matrix to map Drawable against
* @return RectF - Displayed Rectangle
*/
private RectF getDisplayRect(Matrix matrix) {
protected RectF getDisplayRect(Matrix matrix) {
Drawable d = mImageView.getDrawable();
if (d != null) {
mDisplayRect.set(0, 0, d.getIntrinsicWidth(),
Expand Down
2 changes: 2 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ dependencies {
implementation "com.android.support:recyclerview-v7:$supportLibVersion"
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation project(':photoview')

implementation 'com.facebook.fresco:fresco:1.3.0'
}
3 changes: 3 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<activity android:name=".ActivityTransitionToActivity" />

<activity android:name=".ImmersiveActivity" />

<activity android:name=".FrescoSampleActivity" />

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*******************************************************************************
* Copyright 2011, 2012 Chris Banes.
*
* 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.github.chrisbanes.photoview.sample;

import android.graphics.Matrix;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.drawable.ScalingUtils;
import com.github.chrisbanes.photoview.OnMatrixChangedListener;
import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.github.chrisbanes.photoview.OnSingleFlingListener;
import com.github.chrisbanes.photoview.sample.fresco.PhotoDraweeView;

import java.util.Random;


public class FrescoSampleActivity extends AppCompatActivity {

static final String PHOTO_TAP_TOAST_STRING = "Photo Tap! X: %.2f %% Y:%.2f %% ID: %d";
static final String SCALE_TOAST_STRING = "Scaled to: %.2ff";
static final String FLING_LOG_STRING = "Fling velocityX: %.2f, velocityY: %.2f";

private PhotoDraweeView mPhotoView;
private TextView mCurrMatrixTv;

private Toast mCurrentToast;

private Matrix mCurrentDisplayMatrix = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fresco_simple_sample);

Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Simple Sample");
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
toolbar.inflateMenu(R.menu.main_menu);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_zoom_toggle:
mPhotoView.setZoomable(!mPhotoView.isZoomEnabled());
item.setTitle(mPhotoView.isZoomEnabled() ? R.string.menu_zoom_disable : R.string.menu_zoom_enable);
return true;

case R.id.menu_scale_fit_center:
mPhotoView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_CENTER);
return true;

case R.id.menu_scale_fit_start:
mPhotoView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_START);
return true;

case R.id.menu_scale_fit_end:
mPhotoView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_END);
return true;

case R.id.menu_scale_fit_xy:
mPhotoView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY);
return true;

case R.id.menu_scale_scale_center:
mPhotoView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.CENTER);
return true;

case R.id.menu_scale_scale_center_crop:
mPhotoView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP);
return true;

case R.id.menu_scale_scale_center_inside:
mPhotoView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.CENTER_INSIDE);
return true;

case R.id.menu_scale_random_animate:
case R.id.menu_scale_random:
Random r = new Random();

float minScale = mPhotoView.getMinimumScale();
float maxScale = mPhotoView.getMaximumScale();
float randomScale = minScale + (r.nextFloat() * (maxScale - minScale));
mPhotoView.setScale(randomScale, item.getItemId() == R.id.menu_scale_random_animate);

showToast(String.format(SCALE_TOAST_STRING, randomScale));

return true;
case R.id.menu_matrix_restore:
if (mCurrentDisplayMatrix == null)
showToast("You need to capture display matrix first");
else
mPhotoView.setDisplayMatrix(mCurrentDisplayMatrix);
return true;
case R.id.menu_matrix_capture:
mCurrentDisplayMatrix = new Matrix();
mPhotoView.getDisplayMatrix(mCurrentDisplayMatrix);
return true;
}
return false;
}
});
mPhotoView = findViewById(R.id.iv_photo);
mCurrMatrixTv = findViewById(R.id.tv_current_matrix);

// Drawable bitmap = ContextCompat.getDrawable(this, R.drawable.wallpaper);
// mPhotoView.setImageDrawable(bitmap);
mPhotoView.setImageURI(Uri.parse("http://imgsrc.baidu.com/imgad/pic/item/7dd98d1001e93901cf713c2d71ec54e736d196b6.jpg"));
// Lets attach some listeners, not required though!
mPhotoView.setOnMatrixChangeListener(new MatrixChangeListener());
mPhotoView.setOnPhotoTapListener(new PhotoTapListener());
mPhotoView.setOnSingleFlingListener(new SingleFlingListener());
}

private class PhotoTapListener implements OnPhotoTapListener {

@Override
public void onPhotoTap(ImageView view, float x, float y) {
float xPercentage = x * 100f;
float yPercentage = y * 100f;

showToast(String.format(PHOTO_TAP_TOAST_STRING, xPercentage, yPercentage, view == null ? 0 : view.getId()));
}
}

private void showToast(CharSequence text) {
if (mCurrentToast != null) {
mCurrentToast.cancel();
}

mCurrentToast = Toast.makeText(FrescoSampleActivity.this, text, Toast.LENGTH_SHORT);
mCurrentToast.show();
}

private class MatrixChangeListener implements OnMatrixChangedListener {

@Override
public void onMatrixChanged(RectF rect) {
mCurrMatrixTv.setText(rect.toString());
}
}

private class SingleFlingListener implements OnSingleFlingListener {

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d("PhotoView", String.format(FLING_LOG_STRING, velocityX, velocityY));
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;

public class LauncherActivity extends AppCompatActivity {

public static final String[] options = {
Expand All @@ -35,13 +37,17 @@ public class LauncherActivity extends AppCompatActivity {
"Rotation Sample",
"Picasso Sample",
"Activity Transition Sample",
"Immersive Sample"
"Immersive Sample",
"Fresco Sample"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);

Fresco.initialize(getApplicationContext());

Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle(R.string.app_name);
RecyclerView recyclerView = findViewById(R.id.list);
Expand Down Expand Up @@ -78,6 +84,8 @@ public void onClick(View v) {
break;
case 5:
clazz = ImmersiveActivity.class;
case 6:
clazz = FrescoSampleActivity.class;
}

Context context = holder.itemView.getContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.chrisbanes.photoview.sample.fresco;

import android.graphics.Matrix;
import android.graphics.RectF;

import com.facebook.drawee.view.GenericDraweeView;
import com.github.chrisbanes.photoview.PhotoViewAttacher;

/*******************************************************************************
* Description: Extend PhotoViewAttacher to support Fresco.
*
* Author: Freeman
*
* Date: 2018/9/7
*******************************************************************************/
public class DraweeViewAttacher extends PhotoViewAttacher {

private GenericDraweeView imageView;

public DraweeViewAttacher(GenericDraweeView image) {
super(image);
this.imageView = image;
}

@Override
protected RectF getDisplayRect(Matrix matrix) {
RectF displayRect = new RectF();
imageView.getHierarchy().getActualImageBounds(displayRect);
matrix.mapRect(displayRect);
return displayRect;
}
}
Loading