Learning and using webp in android and androidwebp Learning

Source: Internet
Author: User
Tags webp

Learning and using webp in android and androidwebp Learning

Recently, I want to create a beautiful and atmospheric guide page for the app. I am looking for a UI to communicate with each other. I am very passionate about the UI. I just sent it soon. Four pictures are really beautiful, but I can check the size, 4 m scared, and communicated quickly, not only to be beautiful, atmospheric, but also to be small, the smaller the better, and finally after many communications, sent four pictures, a total of more than 400 K, it cannot be smaller. If it is smaller, the display effect will be affected.

The product said, why is the app so big? The smaller the app, the better the app, the more likely it is to be pushed, the lower the cost.

There is no way to find a solution for development. I searched the internet and found that there is a webp model for images. Google recommends that many apps in China use this solution. Download The Conversion Tool iSparta, the conversion of the four guide images is much smaller, from the original 400 k to less than 100 K. The key is that the effect is almost unaffected, at least not obviously distorted to the naked eye, therefore, we converted all charts larger than K into webp display.

1. First, you need to convert the target image to a webp image, and use iSparta for conversion. After the conversion, you can delete the source image and suffix it. copy the webp image to the source image of the project.

2. Load the so file. The libwebp. jia package officially provided

         public static Bitmap webpToBitmap(byte[] encoded) {int[] width = new int[] { 0 };int[] height = new int[] { 0 };byte[] decoded = libwebp.WebPDecodeARGB(encoded, encoded.length, width,height);int[] pixels = new int[decoded.length / 4];ByteBuffer.wrap(decoded).asIntBuffer().get(pixels);return Bitmap.createBitmap(pixels, width[0], height[0],Bitmap.Config.ARGB_8888);}public static byte[] streamToBytes(InputStream in) {ByteArrayOutputStream out = new ByteArrayOutputStream(1024);byte[] buffer = new byte[1024];int len = -1;try {while ((len = in.read(buffer)) >= 0) {out.write(buffer, 0, len);out.flush();}} catch (java.io.IOException e) {e.printStackTrace();} finally {try {in.close();} catch (Exception e) {e.printStackTrace();}}return out.toByteArray();}

3. android4.0 and later versions support webp images, that is, more than 4.0 of images can be used directly like images in other formats. Special processing is required for less than 4.0 of images, and encapsulation is used:


Public static Drawable getDrawable (int id, Context context) {return new BitmapDrawable (getBitmap (id, context);} public static Bitmap getBitmap (int resId, Context context) {InputStream rawImageStream = context. getResources (). openRawResource (resId); byte [] data = com. dingji. webpdemo. webpUtils. streamToBytes (rawImageStream); return com. dingji. webpdemo. webpUtils. webpToBitmap (data);}/*** assigned webp * @ param id * @ param imageview * @ param context */public static void setWebPValue (int resId, ImageView iv, Context context) {if (Build. VERSION. SDK_INT <Build. VERSION_CODES.ICE_CREAM_SANDWICH) {// less than 4.0iv.setImageBitmap (getBitmap (resId, context);} else {iv. setImageResource (resId);}/*** assigned webp * @ param id * @ param view * @ param context */public static void setWebPValue (int resId, View view, context context) {if (Build. VERSION. SDK_INT <Build. VERSION_CODES.ICE_CREAM_SANDWICH) {// less than 4.0view.setBackgroundDrawable (getDrawable (resId, context);} else {view. setBackgroundResource (resId );}}

4. Direct calls in the project where needed

      ImageView iv=(ImageView) view.findViewById(R.id.guideImg);      WebpUtils.setWebPValue(R.drawable.guide_img_one, iv, context);

Advantages of using webp: easy to use. Of course, the most important thing is to significantly reduce the image size without losing its own image size.

Disadvantage: there must be a cost for getting the image smaller, that is, the loading time is increased, but the test time is increased by dozens of milliseconds, basically within the acceptable range.


Download demo:

Http://download.csdn.net/detail/socketsyuhai/8870491


Download the conversion. webp image Conversion Tool:

Http://download.csdn.net/detail/socketsyuhai/8870549


For more details, refer to url:

Essence of concentration! Get to know the latest image format WEBP from scratch

Http://www.uisdc.com/image-format-webp-introduction

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.