Android Mosaic (Mosaics) Effect _android

Source: Internet
Author: User

A few days ago saw the effect of the open source project is good, look at the code, the implementation is basically in the original interface over 10% Custom view, get to the click of the View content (Bitmap), and then in the cover of the custom view of the specific location to draw out, After that is to do some column split on this bitmap, change the process of redrawing. Here according to his bitmap, the feeling used to achieve bitmap effect is also good, try to do.

Here are two ways to achieve mosaic effect. Look at the effect before you start


It feels good!

1. Direct drawing

public static Bitmap Getmosaicsbitmap (Bitmap bmp, double precent) {Long start = System.currenttimemillis (); int BMPW = b
Mp.getwidth ();
int bmph = Bmp.getheight ();
Bitmap resultbmp = Bitmap.createbitmap (BMPW, bmph, Bitmap.Config.ARGB_8888);
Canvas Canvas = new Canvas (resultbmp);
Paint Paint = new Paint ();
Double Unit;
if (precent = = 0) {unit = BMPW} or else {unit = 1/precent;} double resultbmpw = Bmpw/unit;
Double resultbmph = bmph/unit; for (int i = 0; i < resultbmph. i++) {for (int j = 0; J < Resultbmpw; J +) {int pickpointx = (int) (unit * (j +
 0.5));
 int pickpointy = (int) (unit * (i + 0.5));
 int color;
 if (pickpointx >= BMPW | | pickpointy >= bmph) {color = Bmp.getpixel (BMPW/2, BMPH/2);
 else {color = Bmp.getpixel (Pickpointx, Pickpointy);
 } paint.setcolor (color);
 Canvas.drawrect ((int) (unit * j), (int) (unit * i), (int) (unit * (j + 1)), (int) (unit * (i + 1)), paint);
} canvas.setbitmap (NULL);
Long end = System.currenttimemillis (); LOG.V (TAG, "Drawtime:" + (End-start));
return resultbmp;
 }

2, modifying pixel

 public static Bitmap Getmosaicsbitmaps (Bitmap bmp, double precent) {Long start = System.currenttimemillis ();
 int BMPW = Bmp.getwidth ();
 int bmph = Bmp.getheight ();
 int[] pixels = new int[bmph * BMPW];
 Bmp.getpixels (pixels, 0, BMPW, 0, 0, BMPW, bmph);
 int raw = (int) (BMPW * precent);
 int unit;
 if (raw = = 0) {unit = BMPW; else {unit = Bmpw/raw;//original unit*unit pixel to synthesize one, using the value in the upper-left corner} if (Unit >= BMPW | | \ >= bmph) {return Getmosai
 Csbitmap (BMP, Precent); for (int i = 0; i < bmph;) {for (int j = 0; J < BMPW;)
  {int lefttoppoint = i * BMPW + j;
   for (int k = 0; k <, k++) {for (int m = 0; m < unit; m++) {int point = (i + k) * BMPW + (j + m);
   if (Point < Pixels.length) {Pixels[point] = Pixels[lefttoppoint];
 }} j + +;
 i + = unit;
 Long end = System.currenttimemillis ();
 LOG.V (TAG, "Drawtime:" + (End-start));
Return Bitmap.createbitmap (pixels, BMPW, bmph, Bitmap.Config.ARGB_8888); }

From the efficiency point of view, the second way efficiency will be 10 times times higher, as long as the first way to draw too many times, and drawing is more time-consuming. Here is a special tip, do not use LOG.V (...) within a large number of looping statements, this is a time-consuming operation.

It's not very interesting, we can try it by ourselves.

Related Article

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.