如何在缩放或拖动(左,右,上,下)图像时精确设置另一个视图的位置?

时间:2016-02-25 15:27:08

标签: android android-layout imageview position image-zoom

在这种情况下我遇到了很大麻烦。 我在图像背景上添加了一个imageview(绿色图标)。绿色图标的位置是动态设置的(图1)

enter image description here

此图像可以放大,缩小,向左,向右,向上,向下拖动。我想在缩放或拖动图像背景时精确移动imageview的位置(绿色图标)。

当我缩放,拖动图像背景时,如何将图像视图的位置(绿色图标)与图像背景进行映射?

图2:图像背景被缩放但绿色图标视图没有与图像背景对应,它位置错误。

enter image description here

你能给我一些解决方案,方向或脚本吗?谢谢大家!

1 个答案:

答案 0 :(得分:1)

使用以下功能将图像位图与标记图像位图重叠并返回可用于显示的新位图

private Bitmap overlay(Bitmap bmp1, Bitmap bmp2, float left, float top) {
        Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
        Canvas canvas = new Canvas(bmOverlay);
        canvas.drawBitmap(bmp1, new Matrix(), null);
        canvas.drawBitmap(bmp2, left, top, null); 
        return bmOverlay;
    }

例如: -

        Bitmap border = null;
        Bitmap scaledBorder = null;
        border = BitmapFactory.decodeResource(getResources(), R.drawable.palette);
        scaledBorder = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        ImageView im = (ImageView) findViewById(R.id.imageView);
        im.setImageBitmap(overlay(border, scaledBorder, 0, 0));// replace both 0,0 point with your mark point 
相关问题