首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有安卓开源ImageView吗?

问有安卓开源ImageView吗?
EN

Stack Overflow用户
提问于 2016-11-26 01:36:23
回答 2查看 261关注 0票数 0

我需要一个ImageView,可以触摸,放大/缩小和平底锅。每次我触摸ImageView时,我都需要知道源图像的触摸位置。例如,图像分辨率为1280*720,即使ImageView中的图像被放大,我仍然确切地知道图像的触摸位置(不是ImageView的触摸位置)。

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-29 10:27:49

最后,我使用了这个开源代码https://github.com/sephiroth74/ImageViewZoom。

我修改了代码,但是在这个开源项目中仍然有很多bug,我已经修复了其中的几个。

下面的函数被添加到ImageViewTouch.GestureListener类中,它用于将接触点(在屏幕中)传输到真实图像中的点(无论图像是放大还是缩小)。

代码语言:javascript
复制
    private PointF calculatePositionInSourceImage(PointF touchPointF) {
        //point relative to imageRect
        PointF touchPointRelativeToImageRect = new PointF();
        RectF imageRect = getBitmapRect();
        touchPointRelativeToImageRect.set(touchPointF.x - imageRect.left,
                touchPointF.y - imageRect.top);

        //real image resolution
        int imageWidth = getDrawable().getIntrinsicWidth();
        int imageHeight = getDrawable().getIntrinsicHeight();

        //touch point in image
        PointF touchPointRelativeToImage = new PointF();
        touchPointRelativeToImage.set(touchPointRelativeToImageRect.x/imageRect.width() * imageWidth,
                touchPointRelativeToImageRect.y/imageRect.height() * imageHeight);

        if(touchPointRelativeToImage.x < 0 || touchPointRelativeToImage.y < 0 )
            touchPointRelativeToImage.set(0,0);

        return  touchPointRelativeToImage;
    }
票数 0
EN

Stack Overflow用户

发布于 2016-11-26 01:59:32

为什么不跳过所有这些,使用现有的库呢?

试试这个:PhotoView在API 23上非常适合我,只要有几行代码,你就可以放大/缩小和平移。

代码语言:javascript
复制
compile 'com.github.chrisbanes:PhotoView:1.3.0'

repositories {
    ...
    maven { url "https://jitpack.io" }
}

示例

代码语言:javascript
复制
ImageView mImageView;
PhotoViewAttacher mAttacher;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Any implementation of ImageView can be used!
mImageView = (ImageView) findViewById(R.id.iv_photo);

// Set the Drawable displayed
Drawable bitmap = getResources().getDrawable(R.drawable.wallpaper);
mImageView.setImageDrawable(bitmap);

// Attach a PhotoViewAttacher, which takes care of all of the zooming functionality.
// (not needed unless you are going to change the drawable later)
mAttacher = new PhotoViewAttacher(mImageView);
}

// If you later call mImageView.setImageDrawable/setImageBitmap/setImageResource/etc then you just need to call
mAttacher.update();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40814127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档