My blog Http://blog.csdn.net/dawn_moon
The needs of our customers are all very strange. I sometimes want to scold clients for being SB when doing projects for clients. But please believe me, waiting for you to have the demand, when you become a customer, you do the project brother will certainly scold you are SB.
Is this, the customer needs to do a picture upload function, this picture needs to be cropped. In general, this thing is done with the cropping of the system itself. But the customer does not, he wants to be able to cut freely, is the aspect ratio is not fixed, wants to cut into what proportion to cut into what proportion, I hear, the eggs are all broken.
There is no way, customer SB to SB, demand or have to do, otherwise do not give money to Hexibeifeng.
Picture clipping function system comes with, and my needs only a little different, that is, it is fixed aspect ratio, set the aspect ratio can not be changed, and I need free aspect ratio. Well, since it's just not the same, change it for him.
In fact, I this person is very lazy, I have long wanted to Google to see if anyone has written well, take to change the direct use. But a circle was found, and only one brother wrote his own definition of cutting function. Run a bit, the basic functions can be achieved, but too ugly, and not too stable, some adaptation is not done. Think about it or change the source code yourself, well, that's it.
Download the source Code of Gallery, extract the code of the cropping function,
This basic is good, then cropimage.java inside has fork, oneself change down to be good. This class uses some of the variables in the view, which are tagged with the hide tag, so SDK application development is not visible, only the system compiles the ability to pass. There are so many things we don't need, for example.
/*if (Extras! = null) {if (Extras.getstring ("Circlecrop")! = null) {Mcirclecrop = True ; Maspectx = 1; Maspecty = 1; } Msaveuri = (Uri) extras.getparcelable (mediastore.extra_output); if (Msaveuri! = null) {String outputformatstring = extras.getstring ("OutputFormat"); if (outputformatstring! = null) {Moutputformat = Bitmap.CompressFormat.valueOf ( outputformatstring); }} else {msetwallpaper = Extras.getboolean ("SetWallpaper"); } Mbitmap = (Bitmap) extras.getparcelable ("Data"); Maspectx = Extras.getint ("Aspectx"); Maspecty = Extras.getint ("Aspecty"); MOUTPUTX = Extras.getint ("Outputx"); Moutputy = Extras.getint ("Outputy"); Mscale = Extras.getboolean ("scale", true); Mscaleup = Extras.getbOolean ("scaleupifneeded", true); Mdofacedetection = Extras.containskey ("Nofacedetection")?!extras.getboolean ("Nofacedetection") : true; } if (Mbitmap = = null) {Uri target = Intent.getdata ();//mallimages = Imagemanager.makeimage List (Mcontentresolver, target,//imagemanager.sort_ascending); Mimage = Mallimages.getimageforuri (target); if (mimage! = null) {//Don ' t read in really large bitmaps. Use the (big) thumbnail//instead. TODO when saving the resulting bitmap with the//Decode/crop/encode API so we don ' t lose any resolution. Mbitmap = Mimage.thumbbitmap (iimage.rotate_as_needed); } }*/Look at the code that is staring out here, that is, you call the system to cut the time to pass some of the parameters, I just stare here, only to pass a URI to cut the image can be.
Then how to let him free cutting, look at the use of the MASPECTX variable, the source code in the Maspectx and maspecty to do the aspect ratio calculation, we will he calculate the aspect ratio of the place to stare away. Set Maspectx and Maspecty to 1, then change several places:
Hv.setup (Mimagematrix, Imagerect, Facerect, Mcirclecrop,false /*maspectx! = 0 && Maspecty! = 0*/);
Here we stare at the last condition and pass false directly, which will remove the aspect ratio limit.
if (maspectx! = 0 && Maspecty! = 0) { if (Maspectx > Maspecty) { //free scale cropheight = cropwidth * m aspecty;//maspectx; } else { Cropwidth = cropheight * MASPECTX;//maspecty; } }
This removes the aspect ratio calculation.
And then basically it's over.
Free cropping of Android picture clipping