如何在平铺视图中获取像素颜色



我已经导入了一个外部库来从这个库实现一个TileView:TileView 2.2.7

我已经阅读了所有文档,但没有找到获取接触点像素颜色的方法。

这是我代码的一部分,但我认为这对解决问题没有帮助。

public class GetDamages extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final TileView tileView = new TileView(getApplicationContext());
        tileView.setSize(1855, 2880);
        tileView.setScaleLimits(0, 2);
        tileView.setShouldRenderWhilePanning(true);
        tileView.addDetailLevel(0.125f, "tiles/auto/125/%d_%d.jpg");
        tileView.addDetailLevel(0.250f, "tiles/auto/250/%d_%d.jpg");
        tileView.addDetailLevel(0.500f, "tiles/auto/500/%d_%d.jpg");
        tileView.addDetailLevel(1.000f, "tiles/auto/1000/%d_%d.jpg");
        //Setup OnTouchListener
        tileView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int eventAction = motionEvent.getAction();
                switch (eventAction) {
                    case MotionEvent.ACTION_DOWN:
                        double x = tileView.getCoordinateTranslater().translateAndScaleAbsoluteToRelativeX(tileView.getScrollX() + motionEvent.getX(), tileView.getScale());
                        double y = tileView.getCoordinateTranslater().translateAndScaleAbsoluteToRelativeY(tileView.getScrollY() + motionEvent.getY(), tileView.getScale());
              //HERE I NEED TO GET THE PIXEL COLOR

                        //addPin(tileView, x, y);
                        break;
                }
                return false;
            }
        });

        tileView.defineBounds(0, 0, 1, 1);
        tileView.setMarkerAnchorPoints(-0.5f, -1.0f);
        tileView.setScale(0.5f);
        setContentView(tileView);
    }
    //Add marker in the map
    private void addPin(TileView tileView, double x, double y) {
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(R.drawable.push_pin);
        tileView.addMarker(imageView, x, y, -0.5f, -1.0f);
    }
}

我已经尝试了不同的解决方案,其中,但是

final Bitmap bitmap = ((BitmapDrawable)tileView.getDrawable()).getBitmap(); 

因为 TileView 没有 getDrawable((...

tileView.getTileCanvasViewGroup().setDrawingCacheEnabled(true);
Bitmap hotspots = Bitmap.createBitmap(tileView.getTileCanvasViewGroup().getDrawingCache());
int touchColor;
if (hotspots == null) {
touchColor = 0;
} else {                            tileView.getTileCanvasViewGroup().setDrawingCacheEnabled(false);
touchColor = hotspots.getPixel((int)x, (int)y);
}
Log.wtf("Debug", "Pixel Color " + touchColor);

此解决方案始终返回相同的负整数

试试 tileview.tilemap 这只是一个想法,但看起来你试图从视图中引用地图的一部分,而不是地图本身。

顺便说一句,如果你正在制作一个游戏,我会建议libgdx。它内置了一个瓦片地图加载器和一个完整的游戏框架。它也是多平台的。

https://libgdx.badlogicgames.com/

是的,你正在用这个制作地图?因为您可能只想使用切片地图加载程序。

但无论如何,看起来平铺地图画布与位图渲染有关,你看过吗?

http://moagrius.github.io/TileView/index.html?com/qozix/tileview/TileView.html

最新更新