android 图片放大缩小 多点缩放

网上搜索”imageview 图片缩小放大“,搜到的文章都一模一样,都是什么用matrix把图片放大后,删除现在的imageiew,重新new一个新的替代,看到这中动不动就替换组件的,我就蛋疼。

最后经过自己研究,和参照http://www.cnblogs.com/h3clikejava/archive/2012/02/11/2346552.html这篇文章

实现了多点缩放和 通过点击按钮缩小放大的功能。

多点缩放代码请参考http://www.cnblogs.com/h3clikejava/archive/2012/02/11/2346552.html


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

主要代码如下

import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MyImageViewActivity extends Activity {
		private ImageView view;
		Matrix matrix = new Matrix();
		Rect rect;
		private ImageButton zoom_in,zoom_out;
		private PointF mid;
	    @Override  
	    public void onCreate(Bundle savedInstanceState) {  
	        super.onCreate(savedInstanceState);  
	        setContentView(R.layout.main);  
	        mid = new PointF();
	        findAll();
	        setListener();
	    }  
	    
	    private void findAll(){
	    	view = (ImageView) findViewById(R.id.image_View);  
	    	zoom_in = (ImageButton) findViewById(R.id.ibtn_zoom_in);
	    	zoom_out = (ImageButton) findViewById(R.id.ibtn_zoom_out);
	    }
	    
	    private void setListener(){
	    	 view.setOnTouchListener(new MulitPointTouchListener());
	    	 //放大
	    	 zoom_in.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					matrix.set(view.getImageMatrix());
					setMid();//设置放大的中心
					matrix.postScale(1.3f, 1.3f, mid.x,mid.y);
					view.setImageMatrix(matrix);
					view.invalidate();
				}
			});
	    	 //缩小
	    	 zoom_out.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					matrix.set(view.getImageMatrix());
					setMid();//设置放大的中心
					matrix.postScale(0.8f, 0.8f, mid.x,mid.y);
					view.setImageMatrix(matrix);
					view.invalidate();
				}
			});
	    }
	    private void setMid(){
	    	rect = view.getDrawable().getBounds();
			mid.x = view.getDrawable().getBounds().centerX();
			mid.y = view.getDrawable().getBounds().centerY();
	    }
 }

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

layout如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   <RelativeLayout 
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">
        <ImageView
        android:id="@+id/image_View"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="matrix"
        android:layout_centerInParent="true"
        android:src="@drawable/test_image" />
        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="right"
            android:orientation="horizontal">
            	<ImageButton
            android:id="@+id/ibtn_zoom_out"
            android:layout_width="66dp"
            android:layout_height="42dp"
            android:adjustViewBounds="true"
            android:background="#00000000"
            android:padding="0dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ibtn_gallery_zoom_out" />
            	            	<ImageButton
            android:id="@+id/ibtn_zoom_in"
            android:layout_width="66dp"
            android:layout_height="42dp"
            android:adjustViewBounds="true"
            android:background="#00000000"
            android:padding="0dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ibtn_gallery_zoom_in" />
        </LinearLayout>
        </RelativeLayout>
</LinearLayout>

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

最后效果如图:



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


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值