WEB开发网
开发学院手机开发Android 开发 Android 自定义 UI 组件 阅读

Android 自定义 UI 组件

 2010-03-04 04:00:00 来源:WEB开发网   
核心提示:int widthSize = MeasureSpec.getSize(widthMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasure

int widthSize = MeasureSpec.getSize(widthMeasureSpec);

int heightMode = MeasureSpec.getMode(heightMeasureSpec);

int heightSize = MeasureSpec.getSize(heightMeasureSpec);

float hScale = 1.0f;

float vScale = 1.0f;

if (widthMode != MeasureSpec.UNSPECIFIED && widthSize < mDialWidth) {

hScale = (float) widthSize / (float) mDialWidth;

}

if (heightMode != MeasureSpec.UNSPECIFIED && heightSize < mDialHeight) {

vScale = (float )heightSize / (float) mDialHeight;

}

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) {

上一页  1 2 3 4  下一页

Tags:Android 定义 UI

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