ANDROID:自定义视图实现
我有一个自定义视图。 android什么时候会调用draw()呢?在调用onMeasure和onLayout之后?
'package global.domination.pack;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.Log;
import android.view.View;
public class TileView extends View {
// rules about what can be and is on this tile
boolean bLand;
boolean bSea;
boolean bAir;
boolean bOccupied;
Character _character;
// this tile's coordinates
int nX;
int nY;
// bitmaps used for drawing this tile
Bitmap _originalBitmap;
Bitmap _finalBitmap;
// this constructor initializes all variables (creates the tile)
public TileView(Context context, boolean isLand, boolean isSea, boolean isAir, boolean isOccupied, int x_Coordinate, int y_Coordinate, Bitmap bitmap) {
super(context);
//Initialize variables and set tile graphic.
bLand = isLand;
bSea = isSea;
bAir = isAir;
bOccupied = isOccupied;
nX = x_Coordinate;
nY = y_Coordinate;
_originalBitmap = bitmap;
_character = null;
_finalBitmap = bitmap;
}
/* This method is called by parent view when it
* wants to know this view's drawing preferences
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// set this tiles dimensions
this.setMeasuredDimension(50, 50);
}
/* This method is called by parent view when it wants
* this view to draw itself.
*/
@Override
protected void onDraw(Canvas canvas) {
// attempt to draw the _finalBitmap (tile graphic with character graphic)
try{
canvas.drawBitmap(_finalBitmap, 0, 0, null);
}
catch(RuntimeException rtException){
Log.e("EXCEPTION", "ERROR DRAWING FINAL BITMAP" + rtException.getMessage() , rtException);
}
}
}
'
I have a custom view. When will android call draw() on it? After it calls onMeasure and onLayout?
'package global.domination.pack;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.Log;
import android.view.View;
public class TileView extends View {
// rules about what can be and is on this tile
boolean bLand;
boolean bSea;
boolean bAir;
boolean bOccupied;
Character _character;
// this tile's coordinates
int nX;
int nY;
// bitmaps used for drawing this tile
Bitmap _originalBitmap;
Bitmap _finalBitmap;
// this constructor initializes all variables (creates the tile)
public TileView(Context context, boolean isLand, boolean isSea, boolean isAir, boolean isOccupied, int x_Coordinate, int y_Coordinate, Bitmap bitmap) {
super(context);
//Initialize variables and set tile graphic.
bLand = isLand;
bSea = isSea;
bAir = isAir;
bOccupied = isOccupied;
nX = x_Coordinate;
nY = y_Coordinate;
_originalBitmap = bitmap;
_character = null;
_finalBitmap = bitmap;
}
/* This method is called by parent view when it
* wants to know this view's drawing preferences
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// set this tiles dimensions
this.setMeasuredDimension(50, 50);
}
/* This method is called by parent view when it wants
* this view to draw itself.
*/
@Override
protected void onDraw(Canvas canvas) {
// attempt to draw the _finalBitmap (tile graphic with character graphic)
try{
canvas.drawBitmap(_finalBitmap, 0, 0, null);
}
catch(RuntimeException rtException){
Log.e("EXCEPTION", "ERROR DRAWING FINAL BITMAP" + rtException.getMessage() , rtException);
}
}
}
'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否覆盖了
ViewGroup
的onMeasure()
?如果是这样,您需要记住在子视图上调用measure()
。Are you overriding the
onMeasure()
of theViewGroup
s? If so you need to remember to callmeasure()
on you child views.每当视图必须测量其自身视图和子视图的尺寸时,总是会调用
onMeasure()
。如果您要实现视图组,那么每当视图组必须在
onLayout()
之后向其所有子视图分派绘制事件时,就会调用其子视图的onDraw()
方法。这是一个图像链接,可以帮助您了解视图生命周期流程:
有关简要说明,您可以阅读官方文档。
onMeasure()
is always called whenever a view have to measure dimensions of its own and child views.If you are going to implement the viewgroup then its child view's
onDraw()
method will be called whenever a viewgroup has to dispatch draw event to all its childs afteronLayout()
.Here is a image link which may help you understanding view lifecycle flow:
For brief description you may read the official doc.