If I look farther than others, it is because I stand on the shoulders of giants.
GitHub Address: Https://github.com/chrisbanes/PhotoView
Introduced
In the general application, always encounter the function of viewing pictures, click on the picture, detailed view, Photoview this open source control to help us to complete this function very well.
It inherits from ImageView, supports zooming, and listens to various gestures.
Characteristics
- Zoom in by multi-touch and double-tap.
- Smooth scrolling with flick strokes.
- Work well with a parent with scrolling functionality (e.g. Viewpager)
- Allows the app to be notified when the displayed matrix has changed, which is useful when you need to update your UI based on the current zoom/swipe position.
- Allows the app to be notified when you click on a picture
Use
1, through the code attached ImageView use
ImageView Mimageview; Photoviewattacher Mattacher; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //Any implementation of ImageView can is used!Mimageview =(ImageView) Findviewbyid (R.id.iv_photo); //Set the drawable displayeddrawable bitmap =getresources (). getdrawable (R.drawable.wallpaper); Mimageview.setimagedrawable (bitmap); //Attach a photoviewattacher, which takes care of the zooming functionality.Mattacher =NewPhotoviewattacher (Mimageview);}//If You later call mimageview.setimagedrawable/setimagebitmap/setimageresource/etc then you just needAttacher.update ();
2, through layout use.
< Uk.co.senab.photoview.PhotoView xmlns:android = "Http://schemas.android.com/apk/res/android" android:id= "@+id/iv_photo" android:layout_width= "Fill_parent" android:layout_height= "fill_parent"/>
PS: If you need to rotate the picture can be implemented through Setrotationto,setrotationby , other relevant operations between the sample code:
/******************************************************************************* * Copyright, Chris Banes . * * Licensed under the Apache License, Version 2.0 (the "License"); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable or agreed to writing, software * Distributed under the License is distribute D on ' 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. *******************************************************************************/ Packageuk.co.senab.photoview.sample;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.Matrix;ImportAndroid.graphics.RectF;Importandroid.graphics.drawable.Drawable;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;Importandroid.os.Environment;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.ImageView;ImportAndroid.widget.ImageView.ScaleType;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.util.Random;ImportUk.co.senab.photoview.PhotoViewAttacher;ImportUk.co.senab.photoview.PhotoViewAttacher.OnMatrixChangedListener;ImportUk.co.senab.photoview.PhotoViewAttacher.OnPhotoTapListener; Public classSimplesampleactivityextendsActivity {Static FinalString photo_tap_toast_string = "PHOTO tap! X:%.2f percent y:%.2f percent ID:%d "; Static FinalString scale_toast_string = "scaled to:%.2ff"; PrivateTextView Mcurrmatrixtv; PrivatePhotoviewattacher Mattacher; PrivateToast Mcurrenttoast; PrivateMatrix Mcurrentdisplaymatrix =NULL; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ImageView Mimageview=(ImageView) Findviewbyid (R.id.iv_photo); MCURRMATRIXTV=(TextView) Findviewbyid (R.id.tv_current_matrix); drawable Bitmap=getresources (). getdrawable (R.drawable.wallpaper); Mimageview.setimagedrawable (bitmap); //The MAGIC happens here!Mattacher =NewPhotoviewattacher (Mimageview); //Lets Attach some listeners, not required though!Mattacher.setonmatrixchangelistener (NewMatrixchangelistener ()); Mattacher.setonphototaplistener (NewPhototaplistener ()); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main_menu, menu); return Super. Oncreateoptionsmenu (menu); } @Override Public voidOnDestroy () {Super. OnDestroy (); //need to call clean-upMattacher.cleanup (); } @Override Public BooleanOnprepareoptionsmenu (Menu menu) {MenuItem zoomtoggle=Menu.finditem (R.id.menu_zoom_toggle); assert NULL!=Zoomtoggle; Zoomtoggle.settitle (Mattacher.canzoom ()?r.string.menu_zoom_disable:r.string.menu_zoom_enable); return Super. Onprepareoptionsmenu (menu); } @Override Public Booleanonoptionsitemselected (MenuItem item) {Switch(Item.getitemid ()) { Caser.id.menu_zoom_toggle:mattacher.setzoomable (!mattacher.canzoom ()); return true; CaseR.id.menu_scale_fit_center:mattacher.setscaletype (Scaletype.fit_center); return true; CaseR.id.menu_scale_fit_start:mattacher.setscaletype (Scaletype.fit_start); return true; CaseR.id.menu_scale_fit_end:mattacher.setscaletype (scaletype.fit_end); return true; CaseR.id.menu_scale_fit_xy:mattacher.setscaletype (SCALETYPE.FIT_XY); return true; CaseR.id.menu_scale_scale_center:mattacher.setscaletype (Scaletype.center); return true; CaseR.id.menu_scale_scale_center_crop:mattacher.setscaletype (Scaletype.center_crop); return true; CaseR.id.menu_scale_scale_center_inside:mattacher.setscaletype (scaletype.center_inside); return true; Caser.id.menu_scale_random_animate: Caser.id.menu_scale_random:random R=NewRandom (); floatMinscale =Mattacher.getminimumscale (); floatMaxscale =Mattacher.getmaximumscale (); floatRandomscale = Minscale + (r.nextfloat () * (Maxscale-Minscale)); Mattacher.setscale (Randomscale, Item.getitemid ()==r.id.menu_scale_random_animate); Showtoast (String.Format (scale_toast_string, Randomscale)); return true; CaseR.id.menu_matrix_restore:if(Mcurrentdisplaymatrix = =NULL) Showtoast ("Need to capture display matrix first"); ElseMattacher.setdisplaymatrix (Mcurrentdisplaymatrix); return true; CaseR.id.menu_matrix_capture:mcurrentdisplaymatrix=Mattacher.getdisplaymatrix (); return true; CaseR.id.extract_visible_bitmap:Try{Bitmap bmp=Mattacher.getvisiblerectanglebitmap (); File tmpfile= File.createtempfile ("Photoview", ". png", Environment.getexternalstoragepublicdirectory (environment.directory_downloads)); FileOutputStream out=NewFileOutputStream (tmpfile); Bmp.compress (Bitmap.CompressFormat.PNG,90, out); Out.close (); Intent Share=NewIntent (intent.action_send); Share.settype ("Image/png"); Share.putextra (Intent.extra_stream, Uri.fromfile (tmpfile)); StartActivity (share); Toast.maketext ( This, String.Format ("extracted into:%s", Tmpfile.getabsolutepath ()), Toast.length_short). Show (); } Catch(Throwable t) {t.printstacktrace (); Toast.maketext ( This, "Error occured while extracting bitmap", Toast.length_short). Show (); } return true; } return Super. onoptionsitemselected (item); } Private classPhototaplistenerImplementsOnphototaplistener {@Override Public voidOnphototap (View view,floatXfloaty) {floatXpercentage = x *100f; floatYpercentage = y *100f; Showtoast (String.Format (photo_tap_toast_string, Xpercentage, ypercentage, view==NULL? 0: View.getid ())); } } Private voidshowtoast (charsequence text) {if(NULL!=mcurrenttoast) {Mcurrenttoast.cancel (); } mcurrenttoast= Toast.maketext (simplesampleactivity. This, text, toast.length_short); Mcurrenttoast.show (); } Private classMatrixchangelistenerImplementsOnmatrixchangedlistener {@Override Public voidonmatrixchanged (RECTF rect) {Mcurrmatrixtv.settext (rect.tostring ()); } }}
Android Open Source Library--photoview picture View