Original article,
Reprinted please indicate the source: http://blog.csdn.net/qinjuning
Translated 3:
Optimization View
For details about how to design custom views and response touch time, see Android developer:
Address: http://developer.android.com/training/custom-views/index.html
Translation address: optimizing the view
Through the previous study, the well-designed view can now respond to gestures and switch between States. In addition, you must ensure that the view
It runs smoothly and quickly. To avoid a slow UI effect or pause, make sure that your animation is running at 60 frames per second.
The fewer the better
To speed up the view, remove unnecessary code from those activities that call frequently. Start Painting in the ondraw () method, which will give you the largest
Benefits. Especially low, you should also reduce the memory allocation in the ondraw () method, because any memory allocation may lead to memory recovery, which will
Cause inconsistency. Allocate objects between initialization or animation. Never allocate memory during animation operation.
On the other hand, you need to reduce the overhead in the ondraw () method and call the ondraw () method only when necessary. Generally, the invalidate () method calls
The ondraw () method reduces unnecessary calls to invalidate. If possible, the overloaded version with the parameter "invalidate ()" is called ()
Instead of the invalidate () method without parameters. The invalidate () method with parameters can make the draw process more effective, and reduce the number
You do not need to re-paint the view outside the region (the region specified by the parameter.
Note: The three overloaded versions of invalidate () are:
1. Public
Void invalidate (rect dirty)
2. Public void invalidate (int
L, int T, int R, int B)
3. Public void invalidate ()
Another high-cost operation is the layout process (layout ). Call the requestlayout () method for the view at any time. The Android UI framework
You must traverse the entire view tree to determine the size of each view. If there is any conflict in the measure process, the traversal may be performed multiple times.
View tree. Uidesigners sometimes create a deeper viewgroup to achieve some results. However, these deep view trees will lead to efficiency
Problem. Make sure that the hierarchy of your view tree is as light as possible.
If your uidesign is complex, you should consider designing a custom viewgroup to implement the layout process. Unlike the built-in view control,
A custom view can assume the size and shape of each sub-view and avoid performing the measure process for each sub-view. Piechart
Demonstrate how to inherit the viewgroup class. Piechart has child views, but it never measure them. Instead, it uses its own Layout Algorithm
Directly set the size of each sub-view.
The following code is used:
/** * Custom view that shows a pie chart and, optionally, a label. */public class PieChart extends ViewGroup { ... // // Measurement functions. This example uses a simple heuristic: it assumes that // the pie chart should be at least as wide as its label. // @Override protected int getSuggestedMinimumWidth() { return (int) mTextWidth * 2; } @Override protected int getSuggestedMinimumHeight() { return (int) mTextWidth; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // Try for a width based on our minimum int minw = getPaddingLeft() + getPaddingRight() + getSuggestedMinimumWidth(); int w = Math.max(minw, MeasureSpec.getSize(widthMeasureSpec)); // Whatever the width ends up being, ask for a height that would let the pie // get as big as it can int minh = (w - (int) mTextWidth) + getPaddingBottom() + getPaddingTop(); int h = Math.min(MeasureSpec.getSize(heightMeasureSpec), minh); setMeasuredDimension(w, h); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // Do nothing. Do not call the superclass method--that would start a layout pass // on this view's children. PieChart lays out its children in onSizeChanged(). } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); // // Set dimensions for text, pie chart, etc // // Account for padding ... // Lay out the child view that actually draws the pie. mPieView.layout((int) mPieBounds.left, (int) mPieBounds.top, (int) mPieBounds.right, (int) mPieBounds.bottom); mPieView.setPivot(mPieBounds.width() / 2, mPieBounds.height() / 2); mPointerView.layout(0, 0, w, h); onDataChanged(); }}
Use hardware acceleration
After Android 3.0, the android 2D graphics library can use GPU (graphics processing unit) Acceleration on most Android devices. GPU hardware
Acceleration can greatly optimize most applications, but it is not the best choice for each application. The android framework shows you whether the app
Control over hardware acceleration.
<How to use hardware acceleration> This article describes how to use hardware acceleration at the application, activity, and window levels. It is worth noting that
You must manually set the application API level to 11 or higher in the configuration file, that is, configure androidmanifest. XML as follows:
<Uses-SDK Android: targetsdkversion = "11"/>
Once you enable hardware acceleration, you may not be able to see the improvement in efficiency. Mobile GPUs is good at handling specific tasks, such as scaling, rotating,
Translate an image. It also has some tasks that are not good at processing, such as creating a straight line or curve. As the saying goes, make the best use of your resources, foster strengths and circumvent weaknesses, and try to make GPU
Process the tasks that it is good at, and reduce the tasks that make it handle weaknesses.
In the piechart example, for example, it is resource-consuming to draw a circle. Re-painting caused by each rotation causes the UI to be slow.
The solution is to make the view display the circle and set the layer type attribute of the view to layer_type_hardware. Therefore, the GPU
Static images can be cached. In the example, this view exists as an internal class of the piechart class, reducing the code overhead to implement this method.
private class PieView extends View { public PieView(Context context) { super(context); if (!isInEditMode()) { setLayerType(View.LAYER_TYPE_HARDWARE, null); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for (Item it : mData) { mPiePaint.setShader(it.mShader); canvas.drawArc(mBounds, 360 - it.mEndAngle, it.mEndAngle - it.mStartAngle, true, mPiePaint); } } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { mBounds = new RectF(0, 0, w, h); } RectF mBounds;}
After the changes, the piechart. pieview. ondraw () method is called only when the view is displayed for the first time. Other
Time, the drawn image will be used as the image cache, GPU will rotate any image during re-painting.
However, this is only a compromise. Video memory overhead is caused by cached images as the hardware layer. However, video memory is restricted.
Resources. For this reason, in the final version of piechart. pieview, the layer type attribute is set
Layer_type_hardware. At other times, set its layer type attribute to layer_type_hardware.
Allows the GPU to stop caching images.
Finally, do not forget to analyze your code. Optimization Techniques Made on one view may have bad effects on other views.