实现画布android

发布于 2024-11-25 21:53:59 字数 156 浏览 1 评论 0原文

我正在尝试在 android 的 ios 中实现画布功能。 ios 画布有一个名为drawChildren 的属性,它基本上是一个数组,用于管理可由画布控制器添加/删除的绘图对象。

有没有人可以帮助我实现这一壮举?我知道应该使用 Canvas 类,但是,我不确定如何将其排列为数组。

I am trying to implement the canvas feature in ios for android. The ios canvas has a property known as drawChildren that is basically an array that manages drawing objects that can be added/removed by the canvas controller.

Is there a way anyone can help me achieve this feat? I am aware that the Canvas class should be used however, I am not sure how to arrange it in as array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

强辩 2024-12-02 21:53:59

我不知道这是否适合你,或者你是否解决了这个问题,但我采用了这种方法:

Activity.class

//Create an activity to hold everything 
public class GameActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        Rel_main_game = (RelativeLayout) findViewById(R.id.rlt_main);
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        final int heightS = dm.heightPixels;
        final int widthS = dm.widthPixels;
//this is the SurfaceView that will do the magic
        game_panel = new GamePanel(getApplicationContext(),this,widthS, heightS);
        Rel_main_game.addView(game_panel);

}

SurfaceView

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {

    public MainThread thread;    
    public int ScreenWidth;
    public int Screenheigt;
    public GameActivity game;



    public GamePanel(Context context, GameActivity game,  int width , int height) {
        super(context);
        getHolder().addCallback(this);
        this.game = game;
        this.ScreenWidth=width;
        this.Screenheigt=height;
        thread = new MainThread(getHolder(),this);                  
        ship = new Ship(BitmapFactory.decodeResource(getResources(), R.drawable.player), 100, 0, ScreenWidth, Screenheigt);

    }


void Draw(Canvas canvas){
        if (canvas!=null)
        {
         ship.draw(canvas);   
        }
    }

    void Update(float dt){
        ship.udpdate(dt);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {      

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        thread.setRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        boolean retry = true;
        while (retry) 
        {
            try
            {
                thread.join();
                retry=false;
            } 
            catch (InterruptedException e)
            {
            }
        }
    }
}

并且你添加了具有draw和更新方式:

public class Ship {

    public Bitmap bitmap;
    public int x;
    private int y;          
    private int Speed;
    private int inc;
    private int ScreenWidth;
    private int ScreenHeight;
    public int direction=0;
    boolean death;  
    float animTime=0;
    float totalAnimTime = 1;
    float numFrames;

    public Ship(Bitmap decodeResource, int x, int y, int screenWidth, int screenheight) {
        this.bitmap = decodeResource;
        this.x =screenWidth/2 - bitmap.getWidth()/2;
        this.y = screenheight - bitmap.getHeight();
        Speed=1;
        inc=0;
        death=false;
        ScreenWidth =screenWidth;
        ScreenHeight =screenheight;
    }

    public void draw(Canvas canvas){
        if (!death)
        {
            canvas.drawBitmap(bitmap, x, y - bitmap.getHeight() / 2, null);
        }                       
    }

    public void update(float dt){
        if (death)
        {
            animTime += dt;
        }
        else
        {
            float movement =ScreenWidth/2*dt;
            if (x - (bitmap.getWidth()/ 2)<ScreenWidth || x > - bitmap.getWidth()/ 2)
            switch(direction)
            {
                case(1):
                {
                    x+=8;//movement*dt;
                    break;
                }
                case(2):
                {
                    x-=8;//movement*dt;
                    break;
                }
            }
            }
    }

等等。

I don't know if this will work for you, or if you solved this but I made this approach:

Activity.class

//Create an activity to hold everything 
public class GameActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        Rel_main_game = (RelativeLayout) findViewById(R.id.rlt_main);
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        final int heightS = dm.heightPixels;
        final int widthS = dm.widthPixels;
//this is the SurfaceView that will do the magic
        game_panel = new GamePanel(getApplicationContext(),this,widthS, heightS);
        Rel_main_game.addView(game_panel);

}

SurfaceView

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {

    public MainThread thread;    
    public int ScreenWidth;
    public int Screenheigt;
    public GameActivity game;



    public GamePanel(Context context, GameActivity game,  int width , int height) {
        super(context);
        getHolder().addCallback(this);
        this.game = game;
        this.ScreenWidth=width;
        this.Screenheigt=height;
        thread = new MainThread(getHolder(),this);                  
        ship = new Ship(BitmapFactory.decodeResource(getResources(), R.drawable.player), 100, 0, ScreenWidth, Screenheigt);

    }


void Draw(Canvas canvas){
        if (canvas!=null)
        {
         ship.draw(canvas);   
        }
    }

    void Update(float dt){
        ship.udpdate(dt);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {      

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        thread.setRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        boolean retry = true;
        while (retry) 
        {
            try
            {
                thread.join();
                retry=false;
            } 
            catch (InterruptedException e)
            {
            }
        }
    }
}

and you add children classes that have draw and update methods:

public class Ship {

    public Bitmap bitmap;
    public int x;
    private int y;          
    private int Speed;
    private int inc;
    private int ScreenWidth;
    private int ScreenHeight;
    public int direction=0;
    boolean death;  
    float animTime=0;
    float totalAnimTime = 1;
    float numFrames;

    public Ship(Bitmap decodeResource, int x, int y, int screenWidth, int screenheight) {
        this.bitmap = decodeResource;
        this.x =screenWidth/2 - bitmap.getWidth()/2;
        this.y = screenheight - bitmap.getHeight();
        Speed=1;
        inc=0;
        death=false;
        ScreenWidth =screenWidth;
        ScreenHeight =screenheight;
    }

    public void draw(Canvas canvas){
        if (!death)
        {
            canvas.drawBitmap(bitmap, x, y - bitmap.getHeight() / 2, null);
        }                       
    }

    public void update(float dt){
        if (death)
        {
            animTime += dt;
        }
        else
        {
            float movement =ScreenWidth/2*dt;
            if (x - (bitmap.getWidth()/ 2)<ScreenWidth || x > - bitmap.getWidth()/ 2)
            switch(direction)
            {
                case(1):
                {
                    x+=8;//movement*dt;
                    break;
                }
                case(2):
                {
                    x-=8;//movement*dt;
                    break;
                }
            }
            }
    }

and so on.

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