Android放大缩小显示图片

效果图:

Android放大缩小显示图片_第1张图片Android放大缩小显示图片_第2张图片Android放大缩小显示图片_第3张图片

Activity 代码:

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;
import android.widget.Toast;

public class MainActivity extends Activity
{
	private ImageView photoView;
	private Button smallButton,bigButton,turnButton; 
   	private LinearLayout photoLayout; 
	private int displayWidth, displayHeight;
	private float scaleWidth = 1, scaleHeight = 1;
	private int degree = 30;
	private Bitmap bmp; 
	private int id=0; 

	private void showPhoto()
	{
		photoView.setImageBitmap(bmp);
		//BufferedInputStream bis;
		//bis = new BufferedInputStream(getAssets().open(studentId + ".jpg"));
		//bmp = BitmapFactory.decodeStream(bis);
		bmp = BitmapFactory.decodeResource(this.getResources(),R.drawable.ic_launcher);//设置默认照片
		photoView.setImageBitmap(bmp);	
	}
	private void findViews()
	{
		photoView = (ImageView) findViewById(R.id.photoView);
		photoLayout = (LinearLayout) findViewById(R.id.photoLayout);
		smallButton = (Button) findViewById(R.id.smallButton);
		bigButton = (Button) findViewById(R.id.bigButton);
		turnButton = (Button) findViewById(R.id.turnButton);
	}
	private OnClickListener buttonClickListener=new OnClickListener()
	{
		public void onClick(View v)
		{
			switch(v.getId())
			{
			case R.id.smallButton:small();
				break;
			case R.id.bigButton: big();
				break;			
			case R.id.turnButton: turn();
				break;
			}
		}
	};	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findViews();

		showPhoto();//显示照片
		
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		displayWidth = dm.widthPixels;
		displayHeight = dm.heightPixels - 80;
		
		System.out.println(displayWidth + "," + displayHeight);

		smallButton.setOnClickListener(buttonClickListener);	
		bigButton.setOnClickListener(buttonClickListener);
		turnButton.setOnClickListener(buttonClickListener);
	}
	private void small()
	{
		int bmpWidth = bmp.getWidth();// 获得Bitmap的高和宽
		int bmpHeight = bmp.getHeight();
		
		double scale = 0.8;//设置缩小比例
		if(scaleWidth * scale < 0.35)
		{
			Toast.makeText(this, "再缩小还看得清吗?",Toast.LENGTH_SHORT).show();
			return;
		}	
		scaleWidth = (float) (scaleWidth * scale);// 计算出这次要缩小的比例
		scaleHeight = (float) (scaleHeight * scale);	
			
		Matrix matrix = new Matrix();// 产生resize后的Bitmap对象
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,matrix, true);
		if (id == 0)
			photoLayout.removeView(photoView);
		else
			photoLayout.removeView((ImageView) findViewById(id));
		id++;
		ImageView imageView = new ImageView(this);
		imageView.setId(id);
		imageView.setImageBitmap(resizeBmp);
		photoLayout.addView(imageView);
		setContentView(photoLayout);
		bigButton.setEnabled(true);
	}
	private void big()
	{
		int bmpWidth = bmp.getWidth();// 获得Bitmap的高和宽
		int bmpHeight = bmp.getHeight();		
		double scale = 1.25;// 设置放大比例
		scaleWidth = (float) (scaleWidth * scale);// 计算出这次要放大的比例
		scaleHeight = (float) (scaleHeight * scale);
		Matrix matrix = new Matrix();// 产生resize后的Bitmap对象
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,matrix, true);
		if (id == 0)
			photoLayout.removeView(photoView);
		else
			photoLayout.removeView((ImageView) findViewById(id));
		id++;
		ImageView imageView = new ImageView(this);
		imageView.setId(id);
		imageView.setImageBitmap(resizeBmp);
		photoLayout.addView(imageView);
		setContentView(photoLayout);
		if (scaleWidth * scale * bmpWidth > displayWidth|| scaleHeight * scale * scaleHeight > displayHeight)//大到一定程度不能再放大
		{
			Toast.makeText(this, "已经不能再放大!",Toast.LENGTH_SHORT).show();
			bigButton.setEnabled(false);
		}
	}
	private void turn()// 位图的旋转
	{
		int bmpWidth = bmp.getWidth();// 获得Bitmap的高和宽
		int bmpHeight = bmp.getHeight();
		Matrix matrix = new Matrix();// 产生resize后的Bitmap对象
		matrix.postRotate(degree);
		degree += 30;
		Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,matrix, true);
		if (id == 0)
			photoLayout.removeView(photoView);
		else
			photoLayout.removeView((ImageView) findViewById(id));
		id++;
		ImageView imageView = new ImageView(this);
		imageView.setId(id);
		imageView.setImageBitmap(resizeBmp);
		photoLayout.addView(imageView);
		setContentView(photoLayout);
	}
}


activity_main.xml代码

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

	<LinearLayout 
	    android:paddingLeft="40sp"
	    android:paddingRight="40sp"
	    android:paddingTop="30sp"
	    android:paddingBottom="20sp"
	    android:orientation="horizontal" 
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" > 
	    
		<Button 
			android:id="@+id/smallButton" 
			android:layout_width="80dp" 
			android:layout_height="40dp" 
			android:text="缩小"
			android:textSize="18sp"/> 
	    <Button 
			android:id="@+id/bigButton" 
			android:layout_width="80dp" 
			android:layout_height="40dp" 
			android:text="放大"
			android:textSize="18sp"/> 
	    <Button 
			android:id="@+id/turnButton" 
			android:layout_width="80dp" 
			android:layout_height="40dp" 
			android:text="旋转"
			android:textSize="18sp"/>   
    </LinearLayout>
    
    <TextView android:id="@+id/photoInfo"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:paddingBottom="20sp"
		android:layout_gravity="center_horizontal" />
    
    <ImageView 
		android:id="@+id/photoView" 
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content" > 
    </ImageView> 
</LinearLayout>


参考 http://blog.sina.com.cn/s/blog_6ffbcfdb0100qoog.html


你可能感兴趣的:(Android放大缩小显示图片)