Android平台提供了许多基础类和xml标签来帮助我们创建满足要求的view。接下来讨论如何自定义拥有核心功能的view。
public class PieChart extends View { public PieChart(Context context, AttributeSet attrs) { super(context, attrs); } }
添加一个内建的View作为用户交互界面,是使用XML元素对其进行声明,并且通过元素属性控制其外观样式和行为动作。写得好的自定义视图也应该是可以通过XML进行添加和样式控制。要让自定义view能拥有这些能力,我们必须:
<resources> <declare-styleable name="PieChart"> <attr name="showText" format="boolean" /> <attr name="labelPosition" format="enum"> <enum name="left" value="0"/> <enum name="right" value="1"/> </attr> </declare-styleable> </resources>
上面的代码声明了两个自定义属性,showText和labelPosition,他们都属于PieChart的属性。依据惯例,styleable实例的名字与自定义的View的类名一致。尽管不一定非要按照这样的习惯来写,但是许多编辑器是根据这样的命名习惯来提供代码提示的。
这要定义后就能够在xml layout文件中使用了。唯一不同的是自定义属性所从属的命名空间(namespace)不是http://schemas.android.com/apk/res/android,而是http://schemas.android.com/apk/res/你的包名。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/com.example.android.customviews" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.customviews.charting.PieChart android:layout_width="match_parent" android:layout_height="match_parent" custom:showText="true" custom:labelPosition="left" /> </LinearLayout>
为了不一直重复长长的命名空间,便使用xmlns为http://schemas.android.com/apk/com.example.android.customviews指明了一个简单的别名,别名可以随便取。
添加到layout xml中的自定义视图需要使用全路径名。如果是内部类,还要指明外部类。举例:PieChart有一个内部类PieView,要这样使用PieView:com.example.customviews.charting.PieChart$PieView。
自定义视图被定义在xml文件里后,我们所指定的属性会从resource bundle(资源包)中读入,并且以AttributeSet传递给该view的构造器。可以直接从AttributeSet中读取属性值,但是有许多不利因素:
public PieChart(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.getTheme().obtainStyledAttributes( attrs, R.styleable.PieChart, 0, 0); try { mShowText = a.getBoolean(R.styleable.PieChart_showText, false); mTextPos = a.getInteger(R.styleable.PieChart_labelPosition, 0); } finally { a.recycle(); } }
TypedArray
是共享资源,用了之后必须要回收。
属性是控制行为和外观的非常强大的一种方式,但是它们只能在view被初始化时才能读取。为了能够提供动态的控制,可以暴露属性的getter和setter方法。
public boolean isShowText() { return mShowText; } public void setShowText(boolean showText) { mShowText = showText; invalidate(); requestLayout(); }setShowText方法调用了invalidate()和requestLayout()方法。这是保证view行为正常的重要步骤。必须在改变了可能引起view外观改变的属性后使view无效(也就是调用invalidate()方法),以便于系统知道该view需要重绘了。同样地,在改变了可能引起view尺寸或者形状改变的时候,也需要请求一个新的布局(requestLayout()方法)。忘记了调用这些方法,可能会出现很难查找的bugs。
自定义view也需要提供事件监听来传达重要的事件。PieChart提供了一个叫做OnCurrentItemChanged的事件来通知用户将饼图旋转到了新的扇形区(这里的饼图是一格一格的扇形嘛)。
特别是在只有自己才用的自定义视图时,很容易就忘记了提供属性设置方法和事件监听。花一点点时间注意下自定义视图的接口暴露问题可以减少后期的维护成本。总是提供能够影响视图展示和行为的属性的接口是一个很好的原则。
Your custom view should support the widest range of users. This includes users with disabilities that prevent them from seeing or using a touchscreen. To support users with disabilities, you should:
最为重要的便是实现onDraw()方法。该方法有一个Canvas参数,用于自定义视图绘制自己。Canvas类也定义了诸如文字、线、图片等等基本图形的绘制。在绘制图形前,需要先创建Paint对象。
android.graphics将绘制分为了两个部分:
private void init() { mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mTextPaint.setColor(mTextColor); if (mTextHeight == 0) { mTextHeight = mTextPaint.getTextSize(); } else { mTextPaint.setTextSize(mTextHeight); } mPiePaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPiePaint.setStyle(Paint.Style.FILL); mPiePaint.setTextSize(mTextHeight); mShadowPaint = new Paint(0); mShadowPaint.setColor(0xff101010); mShadowPaint.setMaskFilter(new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL)); ...
要知道view的大小,以便于更好的绘制。复杂的自定义视图会根据自己在屏幕中的形状和大小执行多次布局计算。我们不能够假定视图在屏幕上的大小。即便只有一个应用使用我们的自定义试图,但是也要适配不同屏幕尺寸,多种屏幕密度,以及竖屏和横屏之间的切换。
不过,View有许多方法来进行测量,并且大多数都是不用重写的。若不需要特别的控制其大小,直接继承onSizeChanged()就可以了。当视图被赋予大小之后便会调用onSizeChanged()方法,当然,后来因为其他原因改变了大小,也会重新调用onSizeChanged()方法。计算出来的位置、尺寸以及与其大小相关的值都在onSizeChanged()方法里,便不用在绘制的时候每次都去重新计算了。在PieChart例子中,onSizeChanged()方法会在计算出饼图的边框、文本和其他可见元素的相对位置时回调。
为视图指定大小时,布局管理器会假设这个大小中包括了视图内部的边距(padding)的。我们必须自己处理padding。下面是PieChart中的onSizeChanged()片段:
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { // 统计padding float xpad = (float) (getPaddingLeft() + getPaddingRight()); float ypad = (float) (getPaddingTop() + getPaddingBottom()); // 加上文本的宽度 if (mShowText) xpad += mTextWidth; float ww = (float) w - xpad; float hh = (float) h - ypad; // 计算出饼图的大小 float diameter = Math.min(ww, hh); }
实现onMeasure()方法来更好的控制布局参数(layout parameters)。该方法的参数View.MeasureSpec中包含了父控件想要子视图绘制的大小,并且这个大小是要么是最大值,要么仅仅是一个建议。为了优化,这些值是以包装好了的形式存放,应当使用View.MeasureSpec来获取每个整形中的信息。
In this implementation, PieChart attempts to make its area big enough to make the pie as big as its label:
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // Try for a width based on our minimum int minw = getPaddingLeft() + getPaddingRight() + getSuggestedMinimumWidth(); int w = resolveSizeAndState(minw, widthMeasureSpec, 1); // Whatever the width ends up being, ask for a height that would let the pie // get as big as it can int minh = MeasureSpec.getSize(w) - (int)mTextWidth + getPaddingBottom() + getPaddingTop(); int h = resolveSizeAndState(MeasureSpec.getSize(w) - (int)mTextWidth, heightMeasureSpec, 0); setMeasuredDimension(w, h); }这有三个需要注意的地方:
protected void onDraw(Canvas canvas) { super.onDraw(canvas); // Draw the shadow canvas.drawOval( mShadowBounds, mShadowPaint ); // Draw the label text canvas.drawText(mData.get(mCurrentItem).mLabel, mTextX, mTextY, mTextPaint); // Draw the pie slices for (int i = 0; i < mData.size(); ++i) { Item it = mData.get(i); mPiePaint.setShader(it.mShader); canvas.drawArc(mBounds, 360 - it.mEndAngle, it.mEndAngle - it.mStartAngle, true, mPiePaint); } // Draw the pointer canvas.drawLine(mTextX, mPointerY, mPointerX, mPointerY, mTextPaint); canvas.drawCircle(mPointerX, mPointerY, mPointerSize, mTextPaint); }
@Override public boolean onTouchEvent(MotionEvent event) { return super.onTouchEvent(event); }
class mListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onDown(MotionEvent e) { return true; } } mDetector = new GestureDetector(PieChart.this.getContext(), new mListener());
@Override public boolean onTouchEvent(MotionEvent event) { boolean result = mDetector.onTouchEvent(event); if (!result) { if (event.getAction() == MotionEvent.ACTION_UP) { stopScrolling(); result = true; } } return result; }
@Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { mScroller.fling(currentX, currentY, velocityX / SCALE, velocityY / SCALE, minX, minY, maxX, maxY); postInvalidate(); }
if (!mScroller.isFinished()) { mScroller.computeScrollOffset(); setPieRotation(mScroller.getCurrY()); }The Scroller class computes scroll positions for you, but it does not automatically apply those positions to your view. It's your responsibility to make sure you get and apply new coordinates often enough to make the scrolling animation look smooth. There are two ways to do this:
ValueAnimator
in applications that target lower API levels. You just need to make sure to check the current API level at runtime, and omit the calls to the view animation system if the current level is less than 11.
mScroller = new Scroller(getContext(), null, true); mScrollAnimator = ValueAnimator.ofFloat(0,1); mScrollAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { if (!mScroller.isFinished()) { mScroller.computeScrollOffset(); setPieRotation(mScroller.getCurrY()); } else { mScrollAnimator.cancel(); onScrollFinished(); } } });
Users expect a modern UI to transition smoothly between states. UI elements fade in and out instead of appearing and disappearing. Motions begin and end smoothly instead of starting and stopping abruptly. The Android property animation framework, introduced in Android 3.0, makes smooth transitions easy.
To use the animation system, whenever a property changes that will affect your view's appearance, do not change the property directly. Instead, use ValueAnimator
to make the change. In the following example, modifying the currently selected pie slice in PieChart causes the entire chart to rotate so that the selection pointer is centered in the selected slice. ValueAnimator
changes the rotation over a period of several hundred milliseconds, rather than immediately setting the new rotation value.
mAutoCenterAnimator = ObjectAnimator.ofInt(PieChart.this, "PieRotation", 0); mAutoCenterAnimator.setIntValues(targetAngle); mAutoCenterAnimator.setDuration(AUTOCENTER_ANIM_DURATION); mAutoCenterAnimator.start();
If the value you want to change is one of the base View
properties, doing the animation is even easier, because Views have a built-in ViewPropertyAnimator
that is optimized for simultaneous animation of multiple properties. For example:
animate().rotation(targetAngle).setDuration(ANIM_DURATION).start();
Now that you have a well-designed view that responds to gestures and transitions between states, ensure that the view runs fast. To avoid a UI that feels sluggish or stutters during playback, ensure that animations consistently run at 60 frames per second.
To speed up your view, eliminate unnecessary code from routines that are called frequently. Start by working ononDraw()
, which will give you the biggest payback. In particular you should eliminate allocations in onDraw()
, because allocations may lead to a garbage collection that would cause a stutter. Allocate objects during initialization, or between animations. Never make an allocation while an animation is running.
In addition to making onDraw()
leaner, also make sure it's called as infrequently as possible. Most calls toonDraw()
are the result of a call to invalidate()
, so eliminate unnecessary calls to invalidate()
.
Another very expensive operation is traversing layouts. Any time a view calls requestLayout()
, the Android UI system needs to traverse the entire view hierarchy to find out how big each view needs to be. If it finds conflicting measurements, it may need to traverse the hierarchy multiple times. UI designers sometimes create deep hierarchies of nested ViewGroup
objects in order to get the UI to behave properly. These deep view hierarchies cause performance problems. Make your view hierarchies as shallow as possible.
If you have a complex UI, consider writing a custom ViewGroup
to perform its layout. Unlike the built-in views, your custom view can make application-specific assumptions about the size and shape of its children, and thus avoid traversing its children to calculate measurements. The PieChart example shows how to extendViewGroup
as part of a custom view. PieChart has child views, but it never measures them. Instead, it sets their sizes directly according to its own custom layout algorithm.
Creating Custom Views