【问题标题】:Get coordinates of a Bitmap image from an ImageView twice as small从两倍小的 ImageView 获取位图图像的坐标
【发布时间】:2011-10-12 19:36:18
【问题描述】:

我有一个包含Bitmap 图像的ImageView。图像是其容器的两倍大。我已声明 onScroll() 能够在 Bitmap 图像周围移动。如何获取ImageViewBitmap 图像上的坐标?

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
_iv.setImageBitmap(bm);
_iv.setAdjustViewBounds(true);
_iv.setMaxHeight(bm.getHeight());
_iv.setMaxWidth(bm.getWidth());
_iv.setScaleType(ImageView.ScaleType.CENTER);

Bitmap newBm = Bitmap.createScaledBitmap(bm, bm.getWidth() * 2, bm.getHeight() * 2, true);
_iv.setImageBitmap(newBm);

【问题讨论】:

    标签: android bitmap android-imageview


    【解决方案1】:

    我还没有找到实际的方法来做到这一点。这是我用过的方法:

    创建ImageView 后,滚动到已知位置。

    int ivX = 0;
    int ivY = 0;
    
    _iv.invalidate();
    _iv.scrollTo(ivX, ivY);
    

    这样我就有了我所在位置的准确 (x, y) 坐标。然后,我实现了onScroll() 方法并使用生成的距离重新计算我的(x,y)坐标:

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        //Add the scroll distance to the old X, Y coordinates
        ivX += distanceX;
        ivY += distanceY;
    
        //Scroll to the new location
        _iv.scrollTo(ivX, ivY);
    
        return false;
    } //End onScroll()
    

    此外,为了更好地了解scrollTo() 的工作原理以及图像坐标与其容器之间的关系,请关注link 到我的另一个帖子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 1970-01-01
      相关资源
      最近更新 更多