有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何处理图像的事件触摸?

  • 我有以下代码,但当我触摸图像时,它会使应用程序崩溃
  • 我想,触摸它之后,会在某处给我画一个字

共 (1) 个答案

  1. # 1 楼答案

        SpriteBatch batch;
        int x;
        Sprite img;
        float w,h,tw,th=0;
        Camera camera;
        BitmapFont bf;
    
        @Override
        public void create()
        {
            bf=new BitmapFont();
            w = Gdx.graphics.getWidth();
            h = Gdx.graphics.getHeight();
            texture = new Texture(Gdx.files.internal("android.jpg"));
            batch = new SpriteBatch();
            camera = new OrthographicCamera(w, h);
            camera.position.set(w/2, h/2, 0);
            camera.update();
            img = new Sprite(texture);
            tw = img.getWidth();
            th = img.getHeight();
            img.setBounds( camera.position.x - (tw/2), camera.position.y - (th/2),tw,th);
            Gdx.input.setInputProcessor(new InputAdapter(){
    
                    @Override
                    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    
                        if(img.getBoundingRectangle().contains(screenX, screenY))
                            System.out.println("Image Clicked");
                        bf.draw(batch,"click",200,200);
    
                        return true;
                    }
    
                });
        }
    
        @Override
        public void render()
        {        
            Gdx.gl.glClearColor(1, 1, 1, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            batch.begin();
            batch.draw(texture,  x, 0);
    
            x++;
            if(x>=Gdx.graphics.getWidth())x=0;
            batch.end();
        }