http://blog.csdn.net/fan476767883/article/details/7283438

在这我把点击按钮缩放的代码贴上来,因为实在讨厌网上普遍流传的那种“替换imageiew的方法”。

主要代码如下

[java] view plain copy print ?
  1. importandroid.app.Activity;
  2. importandroid.graphics.Matrix;
  3. importandroid.graphics.PointF;
  4. importandroid.graphics.Rect;
  5. importandroid.os.Bundle;
  6. importandroid.view.View;
  7. importandroid.view.View.OnClickListener;
  8. importandroid.widget.ImageButton;
  9. importandroid.widget.ImageView;
  10. publicclassMyImageViewActivityextendsActivity{
  11. privateImageViewview;
  12. Matrixmatrix=newMatrix();
  13. Rectrect;
  14. privateImageButtonzoom_in,zoom_out;
  15. privatePointFmid;
  16. @Override
  17. publicvoidonCreate(BundlesavedInstanceState){
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. mid=newPointF();
  21. findAll();
  22. setListener();
  23. }
  24. privatevoidfindAll(){
  25. view=(ImageView)findViewById(R.id.image_View);
  26. zoom_in=(ImageButton)findViewById(R.id.ibtn_zoom_in);
  27. zoom_out=(ImageButton)findViewById(R.id.ibtn_zoom_out);
  28. }
  29. privatevoidsetListener(){
  30. view.setOnTouchListener(newMulitPointTouchListener());
  31. //放大
  32. zoom_in.setOnClickListener(newOnClickListener(){
  33. publicvoidonClick(Viewv){
  34. matrix.set(view.getImageMatrix());
  35. setMid();//设置放大的中心
  36. matrix.postScale(1.3f,1.3f,mid.x,mid.y);
  37. view.setImageMatrix(matrix);
  38. view.invalidate();
  39. }
  40. });
  41. //缩小
  42. zoom_out.setOnClickListener(newOnClickListener(){
  43. publicvoidonClick(Viewv){
  44. matrix.set(view.getImageMatrix());
  45. setMid();//设置放大的中心
  46. matrix.postScale(0.8f,0.8f,mid.x,mid.y);
  47. view.setImageMatrix(matrix);
  48. view.invalidate();
  49. }
  50. });
  51. }
  52. privatevoidsetMid(){
  53. rect=view.getDrawable().getBounds();
  54. mid.x=view.getDrawable().getBounds().centerX();
  55. mid.y=view.getDrawable().getBounds().centerY();
  56. }
  57. }

其中view.setOnTouchListener(new MulitPointTouchListener()); 就是上边那篇文章里的。

layout如下

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <RelativeLayout
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent">
  9. <ImageView
  10. android:id="@+id/image_View"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:scaleType="matrix"
  14. android:layout_centerInParent="true"
  15. android:src="@drawable/test_image"/>
  16. <LinearLayout
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_alignParentBottom="true"
  20. android:gravity="right"
  21. android:orientation="horizontal">
  22. <ImageButton
  23. android:id="@+id/ibtn_zoom_out"
  24. android:layout_width="66dp"
  25. android:layout_height="42dp"
  26. android:adjustViewBounds="true"
  27. android:background="#00000000"
  28. android:padding="0dp"
  29. android:scaleType="fitCenter"
  30. android:src="@drawable/ibtn_gallery_zoom_out"/>
  31. <ImageButton
  32. android:id="@+id/ibtn_zoom_in"
  33. android:layout_width="66dp"
  34. android:layout_height="42dp"
  35. android:adjustViewBounds="true"
  36. android:background="#00000000"
  37. android:padding="0dp"
  38. android:scaleType="fitCenter"
  39. android:src="@drawable/ibtn_gallery_zoom_in"/>
  40. </LinearLayout>
  41. </RelativeLayout>
  42. </LinearLayout>

后面会把全部源码上传上来

最后效果如图:



下载地址:http://download.csdn.net/detail/fan476767883/4081476


更多相关文章

  1. Android(安卓)Chronometer(计时器)
  2. Android(安卓)TextView字体设置
  3. Android设置全屏隐藏状态栏的方法
  4. 安卓实现标题和按钮在一行,按钮靠最右边布局
  5. h5
  6. android之帧动画实现
  7. Android动态布局,并动态为TextView控件设置drawableLeft、drawabl
  8. Android(安卓)RecyclerView 设置分割线(item头部也绘制)
  9. Android之获取画面大小

随机推荐

  1. Android(安卓)重学系列 Ashmem匿名共享内
  2. 开源项目之瀑布流DodoWaterFall
  3. android的popupwindow点击返回按钮关闭
  4. (一)RxJava初体验
  5. Android:逆向工程之资源ID还原小工具
  6. Android简单封装类似JQuery异步请求
  7. Android: 记一个关于获取输入法弹起高度
  8. android文件系统system-ramdisk-userdata
  9. Android(安卓)VNDK限制下的解决方案
  10. Android(安卓)OpenGL库加载过程源码分析