android 图片放大缩小 多点缩放

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

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

主要代码如下

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

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

layout如下

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns: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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值