Android Learning Tutorials Pictures Hair glass effect (4) _android

Source: Internet
Author: User
Tags abs gettext vmin

This tutorial for you to share the android hair glass effect of the specific code for your reference, the specific content as follows

Blurimageactivity.java Code:

Package Com.siso.crazyworld;
Import android.content.res.Resources;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.text.TextUtils;
Import Android.view.View;
Import Android.widget.EditText;

Import Android.widget.ImageView;
Import Com.siso.crazyworld.utils.APP;

Import Com.siso.crazyworld.utils.FastBlurUtil;
  public class Blurimageactivity extends appcompatactivity {imageview image;

  EditText edit;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_blurimage);
    Image = (ImageView) Findviewbyid (r.id.image);


    Edit = (edittext) Findviewbyid (R.id.edit); Findviewbyid (R.id.button2). Setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View
        V) {String pattern = Edit.gettext (). toString ();
        int scaleratio = 0; if (TExtutils.isempty (pattern)) {scaleratio = 0;
        else if (Scaleratio < 0) {scaleratio = 10;
        else {scaleratio = Integer.parseint (pattern);
        //Get the original artwork that needs to be blurred bitmap resources res = getresources ();

        Bitmap Scaledbitmap = Bitmapfactory.decoderesource (res, r.drawable.filter);
        Scaledbitmap is the target image, 10 is the magnification of the zoom (the greater the blur effect is higher) Bitmap Blurbitmap = Fastblurutil.toblur (Scaledbitmap, scaleratio);
        Image.setscaletype (ImageView.ScaleType.CENTER_CROP);
      Image.setimagebitmap (BLURBITMAP);

    }
    }); Findviewbyid (R.id.button). Setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View V

        {//url is the url,10 of the network picture is a multiple of the magnification (the greater the Blur effect) Final String pattern = Edit.gettext (). toString ();
            Final String URL =//"http://imgs.duwu.me/duwu/doc/cover/201601/18/173040803962.jpg"; "Http://b.hiphotos.baidu.com/album/pic/item/caef76094b36acafe72d0e667cd98d1000e99c5f.jpg?psign=e72d0e667cd98d1001e93901213fb80e7aec54e737d1b867 ";
            New Thread (New Runnable () {@Override public void run () {int scaleratio = 0;
            if (Textutils.isempty (pattern)) {scaleratio = 0;
            else if (Scaleratio < 0) {scaleratio = 10;
            else {scaleratio = Integer.parseint (pattern); The following method must perform the final Bitmap blurBitmap2 = Fastblurutil.geturlbitmap (URL, Scalera

            TIO); Refreshing the UI must execute App.runonuithread in the main thread (new Runnable () {@Override public vo
                ID run () {image.setscaletype (ImageView.ScaleType.CENTER_CROP);
              Image.setimagebitmap (BLURBITMAP2);
          }
            });


      }). Start ();
  }
    });

 }
}

Activity_blurimage.xml content:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "Android:layout_hei ght= "match_parent" android:orientation= "vertical" > <imageview android:id= "@+id/image2" Android: Layout_width= "Match_parent" android:layout_height= "220DP" android:scaletype= "Centercrop" Android:backgro und= "@drawable/filter"/> <linearlayout android:layout_width= "match_parent" android:layout_height= "W Rap_content "android:orientation=" Horizontal > <edittext android:id= "@+id/edit" Android
        : layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_margintop= "15DP" android:hint= "Input ambiguity"/> <button android:id= "@+id/button2" android:layout_width= Content "Android:layout_height= "Wrap_content" android:text= "transformed wool glass"/> <button android:id= "@+id/button" Androi
        D:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "4DP" android:text= "Transformation Network picture wool glass"/> </LinearLayout> <imageview android:id= "@+id/image" Android:la Yout_width= "Match_parent" android:layout_height= "240DP" android:layout_below= "@+id/image2"/>

 ;/linearlayout>

App.java code under Utils folder:

Package com.siso.crazyworld.utils;

Import android.app.Application;
Import Android.os.Handler;

public class App extends application {
  private static app sinstance;
  public static app. getinstance () {return
    sinstance;
  }

  /**
   * Method of refreshing UI in main thread
   * *
   @param r
   /public
  static void Runonuithread (Runnable r) {
    App.getmainhandler (). Post (R);
  }
  /**
   * App's entry function * *
  @Override public
  void OnCreate () {
    super.oncreate ();
    Initialize context
    sinstance = this;
    Initialize Handler
    mhandler = new Handler ();
  }

  QCL is used to refresh the UI
  private static Handler Mhandler in the main thread;

  public static Handler Getmainhandler () {return
    mhandler;
  }
}

FastBlurUtil.cs Code:

Package com.siso.crazyworld.utils;
Import Android.graphics.Bitmap;

Import Android.graphics.BitmapFactory;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;


Import Java.net.URL; The public class Fastblurutil {/** * obtains bitmap//** from ImagePath to obtain the absolute path of the local or network bitmap URL-network or local picture, for example: *
   A. Network path: url= "Http://blog.foreverlove.us/girl2.png";
   * B. Local path: Url= "File://mnt/sdcard/photo/image.png";

  * C. Supported picture formats, PNG, jpg,bmp,gif, etc. * @param URL * @return/public static int io_buffer_size = 2 * 1024;
    public static Bitmap geturlbitmap (String url, int scaleratio) {int Blurradius = 8;//is usually set to 8 on the line.
    if (scaleratio <= 0) {scaleratio = 10;
    } Bitmap originbitmap = null;
    InputStream in = null;
    Bufferedoutputstream out = null; try {in = new Bufferedinputstream (URL). OpenstreaM (), io_buffer_size);
      Final Bytearrayoutputstream DataStream = new Bytearrayoutputstream ();
      out = new Bufferedoutputstream (DataStream, io_buffer_size);
      Copy (in, out);
      Out.flush ();
      byte[] data = Datastream.tobytearray ();

      Originbitmap = Bitmapfactory.decodebytearray (data, 0, data.length); Bitmap Scaledbitmap = Bitmap.createscaledbitmap (Originbitmap, Originbitmap.getwidth ()/Scaleratio, or
      Iginbitmap.getheight ()/Scaleratio, false);
      Bitmap Blurbitmap = Doblur (Scaledbitmap, Blurradius, true);
    return blurbitmap;
      catch (IOException e) {e.printstacktrace ();
    return null; } private static void copy (InputStream in, outputstream out) throws IOException {byte[] b = new Byte[io_
    Buffer_size];
    int read;
    while (read = In.read (b))!=-1) {out.write (b, 0, read); }///Local picture MAO-Glass public static Bitmap Toblur (Bitmap originbitmap, int scaleratio) {//int scaleratio = 10;
    Increases the scaleratio scaling ratio, uses the same smaller bitmap to be possible to blur the effect to be better, and is advantageous to occupy the memory the reduction; int Blurradius = 8;//is usually set to 8. Increase the Blurradius, you can get a higher degree of virtualization, but will cause the CPU more intensive/* The first three parameters are obvious, among which we can select the width of the original size of 1/10; The fourth filter is the effect of scaling, and the filter is true
    Get a smooth edge of the bitmap, on the contrary, you will get edge sawtooth, pixelrelated bitmap. Here we want to zoom on the image to virtual, so regardless of the edge effect, Filter=false.
    */if (scaleratio <= 0) {scaleratio = 10;
        } Bitmap Scaledbitmap = Bitmap.createscaledbitmap (Originbitmap, Originbitmap.getwidth ()/Scaleratio,
    Originbitmap.getheight ()/Scaleratio, false);
    Bitmap Blurbitmap = Doblur (Scaledbitmap, Blurradius, true);
  return blurbitmap;
    public static Bitmap Doblur (Bitmap sentbitmap, int radius, Boolean canreuseinbitmap) {Bitmap Bitmap;
    if (canreuseinbitmap) {bitmap = Sentbitmap;
    else {bitmap = Sentbitmap.copy (Sentbitmap.getconfig (), true);
    } if (Radius < 1) {return (NULL); int w = BITMAP.GETWIdth ();

    int h = bitmap.getheight ();
    int[] pix = new int[w * h];

    Bitmap.getpixels (pix, 0, W, 0, 0, W, h);
    int wm = W-1;
    int HM = H-1;
    int WH = w * h;

    int div = radius + radius + 1;
    int r[] = new INT[WH];
    int g[] = new INT[WH];
    int b[] = new INT[WH];
    int rsum, gsum, Bsum, x, Y, I, p, yp, Yi, yw;

    int vmin[] = new Int[math.max (W, h)];
    int divsum = (div + 1) >> 1;
    Divsum *= divsum;
    int dv[] = new int[256 * Divsum];
    for (i = 0; i < 256 * DIVSUM; i++) {Dv[i] = (i/divsum);

    } yw = Yi = 0;
    int[][] stack = new INT[DIV][3];
    int stackpointer;
    int Stackstart;
    Int[] Sir;
    int RBS;
    int r1 = radius + 1;
    int routsum, goutsum, boutsum;

    int rinsum, ginsum, binsum;
      for (y = 0; y < h; y++) {rinsum = Ginsum = Binsum = Routsum = Goutsum = Boutsum = Rsum = gsum = bsum = 0; for (i =-radius i <= radius; i++) {p = pix[yi + math.min (WM, Math.max (i, 0))];
        Sir = stack[i + radius];
        Sir[0] = (P & 0xff0000) >> 16;
        SIR[1] = (P & 0x00ff00) >> 8;
        SIR[2] = (P & 0x0000ff);
        RBS = R1-math.abs (i);
        Rsum + = sir[0] * RBS;
        Gsum + = sir[1] * RBS;
        Bsum + = sir[2] * RBS;
          if (i > 0) {rinsum + = sir[0];
          Ginsum + = sir[1];
        Binsum + = sir[2];
          else {routsum + = sir[0];
          Goutsum + = sir[1];
        Boutsum + = sir[2];

      } stackpointer = radius;
        for (x = 0; x < W x + +) {R[yi] = Dv[rsum];
        G[yi] = Dv[gsum];

        B[yi] = Dv[bsum];
        Rsum-= routsum;
        Gsum-= goutsum;

        Bsum-= boutsum;
        Stackstart = Stackpointer-radius + div;

        sir = Stack[stackstart% div];
        Routsum-= sir[0];
        Goutsum-= sir[1];

        Boutsum-= sir[2];
        if (y = = 0) {Vmin[x] = math.min (x + radius + 1, WM);
  }      p = pix[yw + vmin[x]];
        Sir[0] = (P & 0xff0000) >> 16;
        SIR[1] = (P & 0x00ff00) >> 8;

        SIR[2] = (P & 0x0000ff);
        Rinsum + = sir[0];
        Ginsum + = sir[1];

        Binsum + = sir[2];
        Rsum + = Rinsum;
        Gsum + = Ginsum;

        Bsum + = Binsum;
        Stackpointer = (stackpointer + 1)% Div;

        sir = stack[(stackpointer)% div];
        Routsum + = sir[0];
        Goutsum + = sir[1];

        Boutsum + = sir[2];
        Rinsum-= sir[0];
        Ginsum-= sir[1];

        Binsum-= sir[2];
      yi++;
    } yw + = W;
      for (x = 0; x < W x + +) {rinsum = Ginsum = Binsum = Routsum = Goutsum = Boutsum = Rsum = gsum = bsum = 0;
      YP =-radius * W;

        for (i =-radius i <= radius; i++) {Yi = Math.max (0, YP) + x;

        Sir = stack[i + radius];
        Sir[0] = R[yi];
        SIR[1] = G[yi];

        SIR[2] = B[yi];

        RBS = R1-math.abs (i); Rsum + + R[yi] * RBS;
        Gsum + = g[yi] * RBS;

        Bsum + = b[yi] * RBS;
          if (i > 0) {rinsum + = sir[0];
          Ginsum + = sir[1];
        Binsum + = sir[2];
          else {routsum + = sir[0];
          Goutsum + = sir[1];
        Boutsum + = sir[2];
        } if (I < HM) {YP + = W;
      } yi = x;
      Stackpointer = radius;  for (y = 0; y < h; y++) {//Preserve alpha channel: (0xff000000 & Pix[yi]) pix[yi] = (0xff000000 & Pix[yi]) | (Dv[rsum] << 16) | (Dv[gsum] << 8) |

        Dv[bsum];
        Rsum-= routsum;
        Gsum-= goutsum;

        Bsum-= boutsum;
        Stackstart = Stackpointer-radius + div;

        sir = Stack[stackstart% div];
        Routsum-= sir[0];
        Goutsum-= sir[1];

        Boutsum-= sir[2];
        if (x = = 0) {Vmin[y] = math.min (y + r1, HM) * W;

        } p = x + vmin[y];
        Sir[0] = r[p];
SIR[1] = g[p];        SIR[2] = b[p];
        Rinsum + = sir[0];
        Ginsum + = sir[1];

        Binsum + = sir[2];
        Rsum + = Rinsum;
        Gsum + = Ginsum;

        Bsum + = Binsum;
        Stackpointer = (stackpointer + 1)% Div;

        sir = Stack[stackpointer];
        Routsum + = sir[0];
        Goutsum + = sir[1];

        Boutsum + = sir[2];
        Rinsum-= sir[0];
        Ginsum-= sir[1];

        Binsum-= sir[2];
      Yi + W;

    } bitmap.setpixels (pix, 0, W, 0, 0, W, h);
  return (bitmap);

 }

}

Run the results as shown in figure:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.