Android手机开发:图片的放大和缩小显示ImageView

源码如下:

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ImageZoomActivity extends Activity {
	private Button myButtonZoomIn;
	private Button myButtonZoomOut;
	private LinearLayout myImgLayout;
	private ImageView myImageView;
	private Bitmap bmp;
	private int id = 0;
	private int displayWidth;
	private int displayHeight;
	private float scaleWidth = 1;
	private float scaleHeight = 1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.zoom_in_out);
		
		//取得屏幕分辨率大小
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		displayWidth = dm.widthPixels;
		//高度为屏幕分辨率,减去按钮高度
		displayHeight = dm.heightPixels - 80;
		//初始化bmp
		bmp = BitmapFactory.decodeResource(getResources(), R.drawable.imgqr);
		
		myImageView = (ImageView)findViewById(R.id.imageView);
		myButtonZoomIn = (Button) findViewById(R.id.ZoomInButton);
		myButtonZoomOut = (Button) findViewById(R.id.ZoomOutButton);
		myImgLayout = (LinearLayout) findViewById(R.id.imglayout);
		myButtonZoomIn.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				big();
			}			
		});
		myButtonZoomOut.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				small();
			}			
		});
	}
	
	//图片缩小的方法
	private void small() {
		int bmpWidth = bmp.getWidth();
		int bmpHeight = bmp.getHeight();
		//设置图片缩小比例
		double scale = 0.8;
		//计算出这次要缩小的比例
		scaleWidth = (float) (scaleWidth * scale);
		scaleHeight = (float) (scaleHeight * scale);
		
		//产生ReSize之后的bmp对象
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
		if(id == 0) {
			//如果是第一次按,就删除原来默认的ImageView
			myImgLayout.removeView(myImageView);
		}else {
			//否则,删除上一次放大缩小后产生的ImageView
			myImgLayout.removeView((ImageView)findViewById(id));
		}
		//产生新的ImageView,并放入 resize的Bmp图像,再放入Layout
		id++;
		ImageView imageView = new ImageView(ImageZoomActivity.this);
		imageView.setId(id);
		imageView.setImageBitmap(resizeBmp);
		myImgLayout.addView(imageView);
		myButtonZoomIn.setEnabled(true);
		//如果太小,则将缩小按钮设为false
		if(scaleWidth*scale*bmpWidth<10 || scaleHeight*scale*bmpHeight<10) {
			myButtonZoomOut.setEnabled(false);
		}
	}
	
	//图片放大的方法
	private void big() {
		int bmpWidth = bmp.getWidth();
		int bmpHeight = bmp.getHeight();
		//设置图片放大比例
		double scale = 1.25;
		//计算出这次要缩小的比例
		scaleWidth = (float) (scaleWidth * scale);
		scaleHeight = (float) (scaleHeight * scale);
		//产生ReSize之后的bmp对象
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
		if(id == 0) {
			//如果是第一次按,就删除原来默认的ImageView
			myImgLayout.removeView(myImageView);
		}else {
			//否则,删除上一次放大缩小后产生的ImageView
			myImgLayout.removeView((ImageView)findViewById(id));
		}
		//产生新的ImageView,并放入 resize的Bmp图像,再放入Layout	
		id++;
		ImageView imageView = new ImageView(ImageZoomActivity.this);
		imageView.setId(id);
		imageView.setImageBitmap(resizeBmp);
		myImgLayout.addView(imageView);
		myButtonZoomOut.setEnabled(true);
		//如果再放大将超过屏幕大小,则设置放大按钮为false
		if(scaleWidth*scale*bmpWidth>displayWidth || scaleHeight*scale*bmpHeight>displayHeight) {
			myButtonZoomIn.setEnabled(false);
		}
	}

}

对应的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="wrap_content">
<RelativeLayout
        android:id="@+id/main"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
       <LinearLayout
            android:id="@+id/imglayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">    

<ImageView 
  android:id="@+id/imageView"
  android:src="@drawable/imgqr"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"/>
  </LinearLayout>
  
        <LinearLayout
            android:id="@+id/bottom_panel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            >
            <Button
                android:id="@+id/ZoomInButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:text="Zoom In"
                />
            <Button
                android:id="@+id/ZoomOutButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:text="Zoom Out"
                />
         </LinearLayout>
   </RelativeLayout>
</LinearLayout>


你可能感兴趣的:(android,layout,手机,float,button,Matrix)