LeveyTabBar click Tabbar to refresh the page

Source: Internet
Author: User

1. preface in Android, views are often converted to Bitmap. For example, screenshots are taken and images are generated for the entire screen View; coverflow needs to convert a page-by-page view to Bitmap to achieve complex graphic effects (shadows, reflection effects, etc ); for example, some dynamic real-time views need to generate a static Bitmap temporarily to facilitate observation and record of data. 2. implementation Method 1) The following is a frequently used conversion method: public static Bitmap convertViewToBitmap (View view, int bitmapWidth, int bitmapHeight) {Bitmap bitmap = Bitmap. createBitmap (bitmapWidth, bitmapHeight, Bitmap. config. ARGB_8888); view. draw (new Canvas (bitmap); return bitmap;} or use the following method: public static Bitmap convertViewToBitmap (View view) {view. buildDrawingCache (); Bitmap bitmap = view. getDrawingCache (); return bitmap;} This Methods can work normally. However, sometimes Bitmap generation may fail (Bitmap is black ). The main reason is that the value of drawingCache is greater than the value specified by the system. Let's take a look at a piece of code in the buildDrawingCache () method: if (width <= 0 | height <= 0 | (width * height * (opaque &&! TranslucentWindow? 2: 4)> ViewConfiguration. get (mContext ). getScaledMaximumDrawingCacheSize () {destroyDrawingCache (); return;} in the code above, width and height are the width and height of the view to be cached, so (width * height * (opaque &&! TranslucentWindow? 2: 4) The current cache size is calculated. ViewConfiguration. get (mContext). getScaledMaximumDrawingCacheSize () obtains the maximum DrawingCache value provided by the system. When the maximum drawingCache value provided by the required DrawingCache> system is used, the Bitmap is generated and the obtained Bitmap is null. Therefore, you only need to modify the required cache value to solve the problem. So we introduced the second method: 2) Perfect solution public static Bitmap convertViewToBitmap (View view) {view. measure (MeasureSpec. makeMeasureSpec (0, MeasureSpec. UNSPECIFIED), MeasureSpec. makeMeasureSpec (0, MeasureSpec. UNSPECIFIED); view. layout (0, 0, view. getMeasuredWidth (), view. getMeasuredHeight (); view. buildDrawingCache (); www.2cto.com Bitmap bitmap = view. getDrawingCache (); return bitmap;} view use "getMeasuredWidth ()" and The "getMeasuredHeight ()" method calculates the length and width. In this case, Bitmap can be obtained correctly.

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.