标签:markdown override sla com 计算 ber java .text 运动
以下来实现一个loading效果。详细效果例如以下:
首先对这个效果进行拆分,它由以下部分组成:
拆分完效果后。思考下如何实现。以下是我的思考过程。
仅仅要有两个变量scanTop/scanBottom记录绘制的上下界限就可以。然后控制scanTop/scanBottom进行变化就可以。如何控制变化非常显然能够通过post/postDelayed实现。
另外一个难点是如何绘制部分”闪电”?思索一番,能够通过canvas.clipRect的方式控制绘制区域。这样间接实现了我们须要的效果;
大致思考完之后。能够写代码了。
首先是measure过程:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//须要计算自己实际须要的宽高
//须要把padding考虑进来
//须要考虑父容器的測量规则
int width,height;
width = (int)mViewMinWidth+getPaddingLeft()+getPaddingRight();
height = (int)mViewMinHeight+getPaddingTop()+getPaddingBottom();
setMeasuredDimension(getMeasuredSize(widthMeasureSpec, width), getMeasuredSize(heightMeasureSpec, height));
}
通过getMeasuredSize计算考虑父容器限制后的实际大小:
private int getMeasuredSize(int measureSpec,int desiredSize){
int result;
int mode = MeasureSpec.getMode(measureSpec);
int size = MeasureSpec.getSize(measureSpec);
switch (mode){
case MeasureSpec.EXACTLY:
result = size;
break;
default:
result = desiredSize;
if(mode == MeasureSpec.AT_MOST)
result = Math.min(result,size);
break;
}
return result;
}
然后是draw的过程:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(mViewBackground);
//假设xml中设置layout_width/layout_height大于默认宽高。那么居中(不同意小于默认宽高)
if(getWidth()-getPaddingLeft()-getPaddingRight() > (int)mViewMinWidth || getHeight()-getPaddingTop()-getPaddingBottom() > (int)mViewMinHeight){
canvas.translate((getWidth()-mViewMinWidth)/2.0f,(getHeight()-mViewMinHeight)/2.0f);
}
//画圆角矩形
canvas.drawRoundRect(mBounds, dp2px(5), dp2px(5), mPaint);
//平移到圆角矩形中心点,画闪电
canvas.translate((mViewMinWidth - mDefaultWidth) / 2.0f, (mViewMinHeight - mDefaultHeight) / 2.0f);
mPaint.setColor(mBackgroundColor);
canvas.drawPath(mThunderPath, mPaint);
mPaint.setColor(mCoverColor);
//通过clicpRect的方式控制可绘制区域(在外界看来好像有闪动的动画效果)
canvas.clipRect(getPaddingLeft(), mScanTop + getPaddingTop(), mDefaultWidth + getPaddingLeft(), mScanBottom + getPaddingTop());
canvas.drawPath(mThunderPath, mPaint);
}
mScanTop/mScanBottom变量能够通过post()进行改变:
class AnimRunnable implements Runnable{
@Override
public void run() {
if (!flag) {
mScanBottom += mGap;
if (mScanBottom >= mDefaultHeight) {
mScanBottom = (int) mDefaultHeight;
flag = true;
}
postInvalidate();
post(this);
} else {
mScanTop += mGap;
if (mScanTop >= mDefaultHeight) {
mScanTop = mScanBottom = 0;
flag = false;
postInvalidate();
postDelayed(this, 700);
} else {
postInvalidate();
post(this);
}
}
}
}
private void startAnim() {
mRunnable = new AnimRunnable();
post(mRunnable);
}
核心代码就这么多。
标签:markdown override sla com 计算 ber java .text 运动
原文地址:http://www.cnblogs.com/blfbuaa/p/7239057.html