Best practices for using alpha

Source: Internet
Author: User

Alpha is a common special effect in graphic interface development. We usually use the following code to implement the alpha special effect:

view.setAlpha(0.5f);View.ALPHA.set(view, 0.5f);ObjectAnimator.ofFloat(view, "alpha", 0.5f).start();view.animate().alpha(0.5f).start();view.setAnimation(new AlphaAnimation(1.0f, 0.5f));

The results are equivalent:

canvas.saveLayer(l, r, t, b, 127, Canvas.CLIP_TO_LAYER_SAVE_FLAG);

Therefore, the common Alpha effect is to draw an image to offscreen buffer and display it. Such an operation consumes a lot of resources and may even cause performance problems, in the development process, we can avoid creating offsreen buffer in other ways.

Textview

For textview, we usually need the text transparency effect instead of the view transparency. Therefore, it is more efficient to directly set the textcolor with the Alpha value.

// Not thistextview. setalpha (alpha); // do not create offscreen bufferint newtextcolor = (INT) (0xff * alpha) <24 | basetextcolor & 0 xffffff; textview. settextcolor (newtextcolor );
Imageview

Similarly, it is more reasonable to directly call the setimagealpha () method for imageview with only SRC image.

// Not this, the setalpha method is inherited by the view, imageview of poor performance. setalpha (0.5f); // when the following method is used, imageview will specify alpha for the image when creating the image. // you can avoid creating offscreenbufferimageview. setimagealpha (INT) Alpha * 255 );
Customview

Similarly, when using a custom control, you should directly set the Alpha of the paint.

// Not thiscustomView.setAlpha(alpha);// But thispaint.setAlpha((int) alpha * 255);canvas.draw*(..., paint);

At the same time, Android provides the hasoverlappingrendering () interface. by rewriting this interface, you can tell the system whether the current view contains overlapping content and help the system optimize the rendering process. The principle is as follows: for views with overlapping content, the system simply and rudely uses offscreen buffer to assist in processing. When the system is notified that the view has no overlapping content, the system will use the appropriate Alpha value to draw each layer.

/** * Returns whether this View has content which overlaps. This function, intended to be * overridden by specific View types, is an optimization when alpha is set on a view. If * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer * and then composited it into place, which can be expensive. If the view has no overlapping * rendering, the view can draw each primitive with the appropriate alpha value directly. * An example of overlapping rendering is a TextView with a background image, such as a * Button. An example of non-overlapping rendering is a TextView with no background, or * an ImageView with only the foreground image. The default implementation returns true; * subclasses should override if they have cases which can be optimized. * * @return true if the content in this view might overlap, false otherwise. */public boolean hasOverlappingRendering() {    return true;}

Finally, we will reference the Chet Haase statement as a summary.

"You know what your view is doing, so do the right thing for your situation ."

Via Android tips: Best practices for using alpha

Best practices for using alpha

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.