ANDROID:自定义视图实现

发布于 2024-10-09 00:13:06 字数 2046 浏览 2 评论 0原文

我有一个自定义视图。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开柳相依 2024-10-16 00:13:06

您是否覆盖了 ViewGrouponMeasure() ?如果是这样,您需要记住在子视图上调用 measure()

Are you overriding the onMeasure() of the ViewGroups? If so you need to remember to call measure() on you child views.

柠檬 2024-10-16 00:13:06

每当视图必须测量其自身视图和子视图的尺寸时,总是会调用 onMeasure()
如果您要实现视图组,那么每当视图组必须在 onLayout() 之后向其所有子视图分派绘制事件时,就会调用其子视图的 onDraw() 方法。

这是一个图像链接,可以帮助您了解视图生命周期流程:

http://1.bp.blogspot.com/-9UCoqk4eOYs/UXOXi4Gqy5I/AAAAAAAAAG4/A8z6ociZjPY/s1600/android.png

有关简要说明,您可以阅读官方文档

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 after onLayout().

Here is a image link which may help you understanding view lifecycle flow:

http://1.bp.blogspot.com/-9UCoqk4eOYs/UXOXi4Gqy5I/AAAAAAAAAG4/A8z6ociZjPY/s1600/android.png

For brief description you may read the official doc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文