[英]How to create a layout?
借助Android Canvas
及其方法drawLines()
和drawCircle()
使用自定義視圖
這是創建自定義視圖的方法 ,這是一個非常不錯的簡短教程 ,介紹如何使用這些方法繪制任何形狀。
樣品
// somewhere in the constructor, call this
private void init() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
// mPaint.setStrokeWidth(SOME_VALUE);
...
}
// override onSizeChanged() to make your measurements.
// if you need 'finer' control, override onMeasure(), but be careful with that one
// (read its javadocs before you override it).
// measurements include shape and text locations and sizes, etc.
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mTriangleVertices[0] = 0.2 * w; // x0
mTriangleVertices[1] = 0.15 * h; // y0
mTriangleVertices[2] = 0.5 * w; // x1
mTriangleVertices[3] = 0.85 * h; // y1
mTriangleVertices[4] = 0.8 * w; // x2
mTriangleVertices[5] = 0.15 * h; // y2
// other calculations...
}
// after you save your measurements to some fields, override onDraw().
// use all the tools you created and the info you gathers above here.
// avoid creating objects at all cost. read the docs for more info.
protected void onDraw(Canvas canvas) {
super.onDraw(canvas); // include this first, last or you can even omit it sometimes
canvas.drawLines(mTriangleVertices, 0, mTriangleVertices.length, mPaint);
// canvas.drawCircle(); canvas.drawOval(); etc..
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.