WEB开发网
开发学院手机开发Android 开发 Android 自定义 View 实例 AnalogClock 源码 阅读

Android 自定义 View 实例 AnalogClock 源码

 2010-02-22 16:09:00 来源:WEB开发网   
核心提示:}float scale = Math.min(hScale, vScale);setMeasuredDimension(resolveSize((int) (mDialWidth * scale), widthMeasureSpec),resolveSize((int) (mDialHeight * scale),

}

float scale = Math.min(hScale, vScale);

setMeasuredDimension(resolveSize((int) (mDialWidth * scale), widthMeasureSpec),

resolveSize((int) (mDialHeight * scale), heightMeasureSpec));

}

@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

mChanged = true;

}

主要的绘图重写View的onDraw方法,我们可以看到通过canvas实例直接屏幕

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

boolean changed = mChanged;

if (changed) {

mChanged = false;

}

int availableWidth = mRight - mLeft;

int availableHeight = mBottom - mTop;

int x = availableWidth / 2;

int y = availableHeight / 2;

final Drawable dial = mDial;

int w = dial.getIntrinsicWidth();

int h = dial.getIntrinsicHeight();

boolean scaled = false;

if (availableWidth < w || availableHeight < h) {

scaled = true;

float scale = Math.min((float) availableWidth / (float) w,

(float) availableHeight / (float) h);

canvas.save();

canvas.scale(scale, scale, x, y);

}

if (changed) {

dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));

}

dial.draw(canvas);

canvas.save();

canvas.rotate(mHour / 12.0f * 360.0f, x, y); //计算时针旋转的角度,android123提示就是那个时针图片的旋转角度,直接反应的就是表盘上那个针的时间

final Drawable hourHand = mHourHand;

if (changed) {

w = hourHand.getIntrinsicWidth();

h = hourHand.getIntrinsicHeight();

hourHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));

}

hourHand.draw(canvas);

上一页  1 2 3 4  下一页

Tags:Android 定义 View

编辑录入:coldstar [复制链接] [打 印]
赞助商链接