在 Android 上的 ImageViewTouch 内将第二张图片叠加在图片之上

Overlay second image on top of image within ImageViewTouch on Android

我很难相信这是不可能的。我在 ImageViewTouch 视图中有一张地图,我正在尝试根据用户的位置在我移动的图像(在地图图像内)上添加一个 "You Are Here" 图像。

当用户在地图上四处平移时,我希望您在此处也可以通过在 ImageViewTouch 内进行平移。 (理想情况下,我希望它也可以通过缩放来缩放,但我会尽力而为!)

我考虑过使用 Canvas 每次用户位置改变时创建一个新的 Bitmap,但是考虑到地图很大,它的性能不是很好。

有什么办法吗?

我想通了!我没有使用速度较慢且会导致问题的 CanvasBitmap,而是能够使用 LayerDrawable。如果这对以后的任何人有帮助,这是我的代码。

public void updateYouAreHere(@Nullable MapLocation location, boolean initial){

    Resources resources = getResources();
    Drawable drawable;

    if (location == null){
        drawable = MiscUtil.getDrawable(resources, R.drawable.map_image);
    }else{

        Drawable[] layers = new Drawable[]{
                MiscUtil.getDrawable(resources, R.drawable.map_image),
                MiscUtil.getDrawable(resources, R.drawable.you_are_here)
        };
        mapDrawable = new LayerDrawable(layers);

        final int iconWidth = 50;
        final int iconHeight = 86;
        mapDrawable.setLayerInset(1, location.x, location.y,
                (int)(Globals.Map.MapWidth) - location.x - iconWidth, (int)(Globals.Map.MapHeight) - location.y - iconHeight);
        drawable = mapDrawable;
    }

    if (!initial) {
        Matrix currentPosition = mapScrollView.getDisplayMatrix();
        float minScale = mapScrollView.getMinScale();
        float maxScale = mapScrollView.getMaxScale();
        mapScrollView.setImageDrawable(drawable, currentPosition, minScale, maxScale);
    }else{
        mapScrollView.setImageDrawable(drawable);
    }
}

private class MapLocation {

    public int x;
    public int y;

    public MapLocation(int x, int y){
        this.x = x;
        this.y = y;
    }

}

此外,MiscUtil.getDrawable 只是一个包装器,可以根据 SDK 版本调用适当的 getResources().getDrawable() 函数。