2011-12-06 71 views
45

我必须实现图像缩放,我已经尝试了这么多的代码。但我没有得到完整的手势事件的想法。我想实现当我们应用双击时,图像将根据触摸位置进行缩放(事件x和y)。我必须在此时仅实现缩放不平移。任何物体都可以提示我吗?如何在android中实现图像视图的缩放效果?

编辑: 其实我已经实现了与图像viewflipper,如果我尝试应用平移和缩放效果,有时图像将被改变。 我想实现用户点击的缩放效果

给我的一些想法....

+1

可以在这里找到可能帮助HTTP代码: //stackoverflow.com/questions/5778532/how-to-zoom-in-out-an-imageviewnot-using-canvas-in-android/5935382#5935382 –

+0

利用https://github.com/rahulkapoor1/的优势ZommableLoadingImageView – Rahul

+0

这个答案非常有用。我测试它,非常快乐。 https://stackoverflow.com/a/6650484/395093 – DanialAbdi

回答

5

这是事实也是如此,对于图像缩放和平移在Android上最好的例子,

http://blog.sephiroth.it/2011/04/04/imageview-zoom-and-scroll/

+0

其实我也使用相同的代码,但问题是我必须检测到双击2次,第一件事是放大你点击的图像,第二次恢复图像。我不知道如何实现第一件事情。你应该帮助我吗? –

53

懒惰的人可以使用this lib,只需导入您的项目中,并

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. 
    mAttacher = new PhotoViewAttacher(mImageView); 
} 


// If you later call mImageView.setImageDrawable/setImageBitmap/setImageResource/etc then you just need to call 
mAttacher.update(); 
+0

如何在eclipse中导入这个库,因为导入后我得到错误... – pks

+4

@ pks你可以从这里下载库http://grepcode.com/snapshot/repo1.maven.org/maven2/com。 github.chrisbanes.photoview/library/1.2.1 直接链接:http://repo1.maven.org/maven2/com/github/chrisbanes/photoview/library/1.2.1/library-1.2.1.jar 只是放下jar文件到你的lib文件夹,并享受:) –

+1

@ star18bit非常感谢你分享这是真棒图书馆。它的作品像魅力:) –

49

下面是最有效的方法之一,它工作顺利:

https://github.com/MikeOrtiz/TouchImageView

将放置在您的项目中。然后可以使用与 ImageView相同的方式。

例子:

TouchImageView img = (TouchImageView) findViewById(R.id.img); 

如果您使用的是XML TouchImageView,则必须提供完整的包 的名字,因为它是一个自定义视图。

例子:

<com.example.touch.TouchImageView 
      android:id="@+id/img” 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
+0

这就像一个魅力..简直太棒了。 – Javanator

+0

它给了我一个错误'android.widget.ImageView不能转换为pl.company.project.TouchImageView' – PolGraphic

+0

@PolGraphic你能告诉我你是如何使用TouchImageView? – Victor

2

你可以检查在一个相关的问题的答案。 https://stackoverflow.com/a/16894324/1465756

刚进口图书馆https://github.com/jasonpolites/gesture-imageview

到您的项目,并在你的布局文件添加如下内容:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:gesture-image="http://schemas.polites.com/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

<com.polites.android.GestureImageView 
    android:id="@+id/image" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/image" 
    gesture-image:min-scale="0.1" 
    gesture-image:max-scale="10.0" 
    gesture-image:strict="false"/>` 
+0

这是我之前发布的针对此问题的帖子 https://stackoverflow.com/questions/6578320/how-to-apply-zoom-drag-and-rotation-到的图像功能于机器人/ 47861501#47861501 – user2288580

2

我者优先图书馆davemorrissey/subsampling-scale-image-view 超过chrisbanes/PhotoView (star18bit的答案)

  • 两者都是活跃项目(在2015年都有一些提交)
  • 都在PlayStore上有Demo
  • 子采样尺度图像视图支持放大图(上面1080)无记忆例外由于子采样,which PhotoView doesn't support
  • 子采样尺度图像视图似乎有更大的API和更好的文档

How to convert AAR to JAR,如果你喜欢用二次抽样规模的图像视图使用Eclipse/ADT

8

我希望你在做well.it往往当他们想在你的应用程序,然后通常会添加一些新的功能与所有发生他们所有搜索不好的策略库因为你不知道这个解释器中的代码是什么类型的。所以我总是喜欢分叉库,并在我的应用程序代码中添加有用的类和方法。

因此,当我坚持同样的问题,我做了很多R & D然后我找到一个类,它提供了zoomIn,zoomOut和pinIn和out的能力。所以你可以看到类here.

就像我之前告诉过的那样,这是一个单独的课程。所以你可以把这个类中的任何地方你的项目,如utils的文件夹。而下面放线到您的XML文件,如:

<your_packege_name.TouchImageView 
     android:id="@+id/frag_imageview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="fitCenter" 
     android:src="@drawable/default_flag" 
     android:transitionName="@string/transition_name_phone" /> 

,你可以找到你的尊敬的活动,图像视图,为你做所有的意见 - :

TouchImageView tv=(TouchImageView)findViewById(R.id.frag_imageview); 
tv.setImageResource(R.drawable.ic_play); 

这就是它的TouchImageView。 享受我们的代码:)

8

非常简单的方法(测试): -

PhotoViewAttacher pAttacher; 
pAttacher = new PhotoViewAttacher(Your_Image_View); 
pAttacher.update(); 

添加波纹线的build.gradle: -

compile 'com.commit451:PhotoView:1.2.4' 
相关问题