Implementation: textview free copy Function

Source: Internet
Author: User

Source code has been uploaded. Link: http://download.csdn.net/detail/huangyabin001/7556825click to open the link


Package COM. example. copyfree; import android. app. actionbar; import android. app. activity; import android. app. alertdialog; import android. app. alertdialog. builder; import android. content. dialoginterface; import android. content. dialoginterface. onclicklistener; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. text. textutils; import android. text. method. hidereturnstransf Ormationmethod; import android. util. typedvalue; import android. view. gesturedetector; import android. view. menu; import android. view. menuitem; import android. view. motionevent; import android. view. scalegesturedetector; import android. view. view; import android. widget. edittext; import android. widget. textview; import android. widget. toast; public class copyfreeactivity extends activity {Private Static final string Tag = "selecttexttocopyactivity"; private float mscalefactor = 1; private comment mscaledetector; private gesturedetector mgesturedetector; private textview text; private edittext edit; Private Static final int zoom_in = 4; private Static final int zoom_out = 5; private final int max_zoom_in_size = 60; private final int max_zoom_out_size = 20; private final int the_size_of_per_zoom = 9; private Float mtextsize = 27; private int mzoommsg =-1; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_copy_free); initui () ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {return true;} public void button (view) {If (textutils. isempty (edit. gettext () {toast. maketext (this, "Enter the test content! ", 0 ). show ();} elsetext. settext (edit. gettext () ;}@ overridepublic Boolean onsearchrequested () {return false ;}@ overridepublic Boolean onoptionsitemselected (menuitem item) {Switch (item. getitemid () {Case android. r. id. home: alertdialog. builder dialog = new alertdialog. builder (this); dialog. settitle ("exit "). seticon (Android. r. drawable. btn_star ). setmessage ("quit the program now? "). Setpositivebutton ("OK", new onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {copyfreeactivity. this. finish ();}}). setnegativebutton ("cancel", new onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {dialog. cancel ();}}). show (); break;} return Super. onoptionsitemselected (item);} private void initui () {text = (textview) findviewbyid (R. id. text ); Edit = (edittext) findviewbyid (R. id. edit); text. settransformationmethod (hidereturnstransformationmethod. getinstance (); text. settextisselectable (true); mscaledetector = new scalegesturedetector (this, new myscalelistener (); mgesturedetector = new gesturedetector (this, new gesturedetector. simpleongesturelistener () {}); mgesturedetector. setondoubletaplistener (null); actionbar = getactionbar (); Ctionbar. setdisplayhomeasupenabled (true);} private handler muihandler = new handler () {@ overridepublic void handlemessage (Message MSG) {Switch (MSG. what) {Case zoom_in: zoomin (); text. invalidate (); break; Case zoom_out: zoomout (); text. invalidate (); // After modifying the textview, call this method to refresh the textview. Break; default: Break ;}}; private void zoomin () {mtextsize = mtextsize + the_size_of_per_zoom <= max_zoom_in_size? Mtextsize + the_size_of_per_zoom: max_zoom_in_size; If (mtextsize> = max_zoom_in_size) {mtextsize = max_zoom_in_size;} text. settextsize (typedvalue. complex_unit_px, mtextsize);} private void zoomout () {mtextsize = mtextsize-the_size_of_per_zoom <max_zoom_out_size? Max_zoom_out_size: mtextsize-the_size_of_per_zoom; If (mtextsize <= max_zoom_out_size) {mtextsize = max_zoom_out_size;} text. settextsize (typedvalue. complex_unit_px, mtextsize);} private class myscalelistener extendsscalegesturedetector. simpleonscalegesturelistener {@ overridepublic Boolean onscale (scalegesturedetector detector) {float scale = detector. getscalefactor (); If (scale <0.999999 | scale> 1.00001) {mscalefactor = scale;} return true ;}@ overridepublic Boolean onscalebegin (scalegesturedetector detector) {return true ;} @ overridepublic void onscaleend (scalegesturedetector detector) {float scale = detector. getscalefactor (); If (mscalefactor> 1.0) {mzoommsg = zoom_in;} else if (mscalefactor <1.0) {mzoommsg = zoom_out ;}} public Boolean onintercepttouchevent (motionevent eV) {mscaledetector. ontouchevent (EV); Final int action = eV. getaction (); Switch (Action) {Case motionevent. action_down: mgesturedetector. ontouchevent (EV); Return false; Case motionevent. action_move: mgesturedetector. ontouchevent (EV); Return false; Case motionevent. action_up: mgesturedetector. ontouchevent (EV); message MSG = message. obtain (); MSG. what = mzoommsg; muihandler. sendmessage (MSG); mzoommsg =-1; return false;} return true;} public Boolean ontouchevent (motionevent eV) {mscaledetector. ontouchevent (EV); Final int action = eV. getaction (); Switch (Action) {Case motionevent. action_down: mgesturedetector. ontouchevent (EV); Return true; Case motionevent. action_move: mgesturedetector. ontouchevent (EV); Return true; Case motionevent. action_up: mgesturedetector. ontouchevent (EV); message MSG = message. obtain (); MSG. what = mzoommsg; muihandler. sendmessage (MSG); mzoommsg =-1; return true; Case motionevent. action_cancel: mgesturedetector. ontouchevent (EV); Return true; default: If (mgesturedetector. ontouchevent (EV) {return true;} return true ;}@overrideprotected void ondestroy () {super. ondestroy (); muihandler. removecallbacksandmessages (null );}}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.