private void setupPhotoAttacher() { mPhotoViewAttacher = new PhotoViewAttacher(mImageView); mPhotoViewAttacher.setOnViewTapListener( new PhotoViewAttacher.OnViewTapListener() { @Override public void onViewTap(View view, float v, float v1) { onBackPressed(); } }); mPhotoViewAttacher.update(); }
@Override public void setImageURI(Uri uri) { super.setImageURI(uri); if (null != mAttacher) { mAttacher.update(); } }
@Override public void setImageResource(int resId) { super.setImageResource(resId); if (null != mAttacher) { mAttacher.update(); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_svg_demo); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); mImageView = (ImageView) findViewById(R.id.iv_image); mButton = (Button) findViewById(R.id.bt_button); mSvg = Sharp.loadResource(getResources(), R.raw.cartman); // If you want to load typefaces from assets: // .withAssets(getAssets()); // If you want to load an SVG from assets: // mSvg = Sharp.loadAsset(getAssets(), "cartman.svg"); mButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { reloadSvg(true); } }); mAttacher = new PhotoViewAttacher(mImageView); mAttacher.setMaximumScale(10f); reloadSvg(false); }
@Override public void setScaleType(ScaleType scaleType) { if (null != mAttacher) { mAttacher.setScaleType(scaleType); } else { mPendingScaleType = scaleType; } }
@Override protected boolean setFrame(int l, int t, int r, int b) { boolean changed = super.setFrame(l, t, r, b); if (null != mAttacher) { mAttacher.update(); } return changed; }
@Override // setImageBitmap calls through to this method public void setImageDrawable(Drawable drawable) { super.setImageDrawable(drawable); if (null != mAttacher) { mAttacher.update(); } }
@Override public void onDestroy() { super.onDestroy(); // Need to call clean-up isAttacherDisposed = true; mAttacher.cleanup(); }
protected void init() { if (null == mAttacher || null == mAttacher.getImageView()) { mAttacher = new PhotoViewAttacher(this); } if (null != mPendingScaleType) { setScaleType(mPendingScaleType); mPendingScaleType = null; } }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { activity = (ActionBarActivity) ImageFragment.this.getActivity(); final FrameLayout root = (FrameLayout) inflater.inflate(R.layout.fragment_image_layout, container, false); final Bundle arguments = getArguments(); if (arguments != null) { final String image_url = arguments.getString("image_url"); mImageView = (ImageView) root.findViewById(R.id.iv_photo); mCurrMatrixTv = new TextView(getActivity()); // The MAGIC happens here! isAttacherDisposed = false; mAttacher = new PhotoViewAttacher(mImageView); mAttacher.setScaleType(ImageView.ScaleType.CENTER_INSIDE); Picasso.with(getActivity()) .load(image_url) .noFade() .into( mImageView, new Callback() { @Override public void onSuccess() { if (!isAttacherDisposed) { mAttacher.setScaleType(ImageView.ScaleType.FIT_CENTER); root.findViewById(R.id.progress).setVisibility(View.GONE); } } @Override public void onError() {} }); // Lets attach some listeners, not required though! mAttacher.setOnMatrixChangeListener(new MatrixChangeListener()); mAttacher.setOnPhotoTapListener(new PhotoTapListener()); } return root; }
@Override public void initialize() { loadBitmapFromTemporaryStorage(); mImageView.setImageBitmap(babeBitMap); // The MAGIC happens here! mAttacher = new PhotoViewAttacher(mImageView); mAttacher.update(); }
private void setUpPhotoAttacher() { mPhotoViewAttacher = new PhotoViewAttacher(mImageView); mPhotoViewAttacher.setOnViewTapListener((view, v, v1) -> hideOrShowToolbar()); mPhotoViewAttacher.setOnLongClickListener( v -> { new AlertDialog.Builder(PictureActivity.this) .setMessage(getString(R.string.ask_saving_picture)) .setNegativeButton( android.R.string.cancel, (dialog, which) -> { dialog.dismiss(); }) .setPositiveButton( android.R.string.ok, (dialog, which) -> { saveImageToGallery(); dialog.dismiss(); }) .show(); return true; }); }
@Override protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) { final View v = inflater.inflate(R.layout.image_detail_fragment, container, false); mImageView = (ImageView) v.findViewById(R.id.image); mAttacher = new PhotoViewAttacher(mImageView); mAttacher.setOnPhotoTapListener( new OnPhotoTapListener() { @Override public void onPhotoTap(View arg0, float arg1, float arg2) { getActivity().finish(); } }); progressBar = (ProgressBar) v.findViewById(R.id.loading); return v; }
@Override public Bitmap getVisibleRectangleBitmap() { return mAttacher.getVisibleRectangleBitmap(); }
@Override public void setOnScaleChangeListener( PhotoViewAttacher.OnScaleChangeListener onScaleChangeListener) { mAttacher.setOnScaleChangeListener(onScaleChangeListener); }
public void createScrollView() { // LayoutParams (Reuseable) LinearLayout.LayoutParams lparams_text = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); LinearLayout.LayoutParams lparams_img = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1000); RelativeLayout.LayoutParams rparams_text = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); RelativeLayout.LayoutParams rparams_img = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1000); lparams_text.gravity = Gravity.CENTER_HORIZONTAL; lparams_img.gravity = Gravity.CENTER_HORIZONTAL; if (questionDataLayout.getChildCount() != 0) { questionDataLayout.removeAllViewsInLayout(); } QData data; for (int position = 0; position < elements.size(); position++) { data = elements.get(position); RelativeLayout rl = new RelativeLayout(getApplicationContext()); if (data.isText()) { TextView tv = new TextView(this); tv.setLayoutParams(lparams_text); tv.setText(data.getText()); tv.setTextSize(20); tv.setTextColor(Color.DKGRAY); tv.setOnClickListener(questionItemClickListener); tv.setOnLongClickListener(questionItemLongClickListener); rl.addView(tv, rparams_text); questionDataLayout.addView(rl, lparams_text); } else if (!data.isText()) { // ADD IMAGEVIEW ImageKeepAspectRatio iv = new ImageKeepAspectRatio(this); iv.setLayoutParams(lparams_img); iv.setImageBitmap(data.getImageBitmap()); PhotoViewAttacher photoViewAttacher = new PhotoViewAttacher(iv); photoViewAttacher.setOnLongClickListener(questionItemLongClickListener); photoViewAttacher.setOnViewTapListener( new PhotoViewAttacher.OnViewTapListener() { @Override public void onViewTap(View view, float x, float y) { int index = questionDataLayout.indexOfChild(view); // scrollView.scrollTo(0, // questionDataLayout.getChildAt(index).getTop()); // reset colors/boxes for (int i = 0; i < elements.size(); i++) { circleIndex.get(i).setBorderColor(getResources().getColor(R.color.blue)); questionDataLayout.getChildAt(i).setBackground(null); } // circleIndex.get(index).setBorderColor(Color.WHITE); // TODO: WORKAROUND FOR API 16 CALL BELOW view.setBackground(rectShapeDrawable); selectedIndex = index; } }); photoViewAttacher.update(); rl.addView(iv, rparams_img); questionDataLayout.addView(rl, lparams_img); } } }
@Override public RectF getDisplayRect() { return mAttacher.getDisplayRect(); }
@Override public boolean canZoom() { return mAttacher.canZoom(); }
@Override public void setRotationBy(float rotationDegree) { mAttacher.setRotationBy(rotationDegree); }
@Override protected void onDetachedFromWindow() { mAttacher.cleanup(); mAttacher = null; super.onDetachedFromWindow(); }
@Override public void setOnSingleFlingListener( PhotoViewAttacher.OnSingleFlingListener onSingleFlingListener) { mAttacher.setOnSingleFlingListener(onSingleFlingListener); }
@Override public void setZoomTransitionDuration(int milliseconds) { mAttacher.setZoomTransitionDuration(milliseconds); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_r); toolbar.setTitleTextColor(getResources().getColor(R.color.white)); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setTitle(""); InstagramDatum instagramDatum = new Gson().fromJson(getIntent().getStringExtra(Constants.INSTA_DATA), InstagramDatum.class); mImageView = (ImageView) findViewById(R.id.image_view); final LinearLayout imageDataLinearLayout = (LinearLayout) findViewById(R.id.image_data_linear_layout); TextView imageUserTextView = (TextView) findViewById(R.id.image_user_text_view); TextView imageLikeTextView = (TextView) findViewById(R.id.image_like_text_view); TextView imageCommentTextView = (TextView) findViewById(R.id.image_comment_text_view); final ProgressBar imageProgressBar = (ProgressBar) findViewById(R.id.image_progress_bar); Picasso.with(getApplicationContext()) .load(instagramDatum.getImages().getStandardResolution().getUrl()) .into( mImageView, new Callback() { @Override public void onSuccess() { imageProgressBar.setVisibility(View.GONE); } @Override public void onError() { setContentView(R.layout.no_connection_layout); imageProgressBar.setVisibility(View.GONE); } }); imageCommentTextView.setText("" + instagramDatum.getComments().getCount()); imageLikeTextView.setText("" + instagramDatum.getLikes().getCount()); imageUserTextView.setText("@" + instagramDatum.getUser().getUsername()); mImageView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Animation fadeOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.animation_fade_out); Animation fadeInAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.animation_fade_in); if (isVisible) { imageDataLinearLayout.startAnimation(fadeOutAnimation); imageDataLinearLayout.setVisibility(View.GONE); getSupportActionBar().hide(); } else { imageDataLinearLayout.startAnimation(fadeInAnimation); imageDataLinearLayout.setVisibility(View.VISIBLE); getSupportActionBar().show(); } isVisible = !isVisible; } }); PhotoViewAttacher photoViewAttacher = new PhotoViewAttacher(mImageView); photoViewAttacher.update(); }
@Override public boolean setDisplayMatrix(Matrix finalRectangle) { return mAttacher.setDisplayMatrix(finalRectangle); }
@Override public Dialog onCreateDialog(Bundle savedInstanceState) { if (getActivity() != null) { final Dialog dialog = new Dialog(getActivity()); dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.activity_image_zoom); dialog.getWindow().setBackgroundDrawableResource(R.color.Transparent); WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(); layoutParams.copyFrom(dialog.getWindow().getAttributes()); layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT; dialog.getWindow().setAttributes(layoutParams); initToolbar(dialog); progressBar = dialog.findViewById(R.id.zoom_loader); zoomableImageView = (PhotoView) dialog.findViewById(R.id.image_zoom_photoview); if (bitmap != null) { if (rect != null) { animateIn(dialog); } } else if (fileUri != null) { showLoader(); Glide.with(getActivity()) .load(fileUri) .fitCenter() .listener( new RequestListener<Uri, GlideDrawable>() { @Override public boolean onException( Exception e, Uri model, Target<GlideDrawable> target, boolean isFirstResource) { return false; } @Override public boolean onResourceReady( GlideDrawable resource, Uri model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) { hideLoader(); return false; } }) .into(zoomableImageView); } else { dismiss(); } photoViewAttacher = new PhotoViewAttacher(zoomableImageView); photoViewAttacher.setOnViewTapListener( new PhotoViewAttacher.OnViewTapListener() { @Override public void onViewTap(View view, float x, float y) { dismiss(); } }); return dialog; } else { return null; } }
@Override public float getMediumScale() { return mAttacher.getMediumScale(); }
protected void onActivityResult(int requestCode, int resultCode, Intent data) { LinearLayout.LayoutParams lparams_img = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1000); lparams_img.gravity = Gravity.CENTER_HORIZONTAL; ImageKeepAspectRatio iv = new ImageKeepAspectRatio(this); iv.setLayoutParams(lparams_img); Log.d("QA_APP", "RETURN..."); if (requestCode == 1) { if (resultCode == RESULT_OK) { // A LOT OF RE-WRITTEN CODE THAT MUST BE REFACTORED Log.d("QA_APP", "RETURN SUCCESS"); questionDataLayout.removeViewAt(chooserIndex); Bitmap tempBitmap = PicUtil.loadFromCacheFile(); iv.setImageBitmap(PicUtil.loadFromCacheFile()); PhotoViewAttacher photoViewAttacher = new PhotoViewAttacher(iv); photoViewAttacher.setOnLongClickListener(questionItemLongClickListener); photoViewAttacher.setOnViewTapListener( new PhotoViewAttacher.OnViewTapListener() { @Override public void onViewTap(View view, float x, float y) { int index = questionDataLayout.indexOfChild(view); // scrollView.scrollTo(0, // questionDataLayout.getChildAt(index).getTop()); // reset colors/boxes for (int i = 0; i < elements.size(); i++) { circleIndex.get(i).setBorderColor(getResources().getColor(R.color.blue)); questionDataLayout.getChildAt(i).setBackground(null); } circleIndex.get(index).setBorderColor(Color.WHITE); // TODO: WORKAROUND FOR API 16 CALL BELOW questionDataLayout.getChildAt(index).setBackground(rectShapeDrawable); selectedIndex = index; } }); photoViewAttacher.update(); elements.add(new QData(tempBitmap)); questionDataLayout.addView(iv); addElementCircles(); createScrollView(); } if (resultCode == RESULT_CANCELED) { Log.d("QA_APP", "RETURN FAIL"); } } else if (requestCode == 2) { if (resultCode == RESULT_OK) { Uri selectedImage = data.getData(); InputStream imageStream = null; try { imageStream = getContentResolver().openInputStream(selectedImage); } catch (FileNotFoundException e) { e.printStackTrace(); } Bitmap selectedImg = BitmapFactory.decodeStream(imageStream); iv.setImageBitmap(selectedImg); PhotoViewAttacher photoViewAttacher = new PhotoViewAttacher(iv); photoViewAttacher.setOnLongClickListener(questionItemLongClickListener); photoViewAttacher.setOnViewTapListener( new PhotoViewAttacher.OnViewTapListener() { @Override public void onViewTap(View view, float x, float y) { int index = questionDataLayout.indexOfChild(view); // scrollView.scrollTo(0, // questionDataLayout.getChildAt(index).getTop()); // reset colors/boxes for (int i = 0; i < elements.size(); i++) { circleIndex.get(i).setBorderColor(getResources().getColor(R.color.blue)); questionDataLayout.getChildAt(i).setBackground(null); } circleIndex.get(index).setBorderColor(Color.WHITE); // TODO: WORKAROUND FOR API 16 CALL BELOW questionDataLayout.getChildAt(index).setBackground(rectShapeDrawable); selectedIndex = index; } }); photoViewAttacher.update(); elements.add(new QData(selectedImg)); addElementCircles(); createScrollView(); } } } // onActivityResult
@Override protected void onDestroy() { mAttacher.cleanup(); super.onDestroy(); }
@Override public void getDisplayMatrix(Matrix matrix) { mAttacher.getDisplayMatrix(matrix); }
@Override public void setOnDoubleTapListener(GestureDetector.OnDoubleTapListener newOnDoubleTapListener) { mAttacher.setOnDoubleTapListener(newOnDoubleTapListener); }