Android 图片放大和缩小

package com.example.ws;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main extends Activity {

	private EditText edit;
	private Button btn;
	Bitmap bmp;

	private int displayWidth,displayHeight;  
	 private float scaleWidth=1,scaleHeight=1;  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		//取得屏幕分辨率  
        DisplayMetrics dm=new DisplayMetrics();  
        getWindowManager().getDefaultDisplay().getMetrics(dm);  
        displayWidth=dm.widthPixels;  
        displayHeight=dm.heightPixels; 
        
        System.out.println("displayWidth"+displayWidth);
        System.out.println("displayHeight"+displayHeight);
        
		edit = (EditText) this.findViewById(R.id.edit);
		btn = (Button) this.findViewById(R.id.btn);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				Intent picture = new Intent(Intent.ACTION_GET_CONTENT);
				picture.setType("image/*");
				picture.addCategory(Intent.CATEGORY_OPENABLE);
				startActivityForResult(Intent.createChooser(picture, "选择图片"), 0);

			}
		});

	}

	public void srcrem(Bitmap bmps) {
		System.out.println("bWidth"+bmps.getWidth());
		System.out.println("bHeight"+bmps.getHeight());
		Drawable drawable = new BitmapDrawable(bmps);
		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
				drawable.getIntrinsicHeight());
		String str = "0";
		SpannableString spannable = new SpannableString(str);
		ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
		spannable.setSpan(span, 0, 0, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		edit.setText(spannable);
		//bmp.recycle();
		scaleWidth = 1;
		scaleHeight =1;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/*if (resultCode == RESULT_OK) {
			Uri uri = data.getData();
			String[] proj = { MediaStore.Images.Media.DATA };
			Cursor cursor = managedQuery(uri, proj, // Which columns to return
					null, // WHERE clause; which rows to return (all rows)
					null, // WHERE clause selection arguments (none)
					null); // Order-by clause (ascending by name)

			int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			cursor.moveToFirst();

			String path = cursor.getString(column_index);
			bmp = BitmapFactory.decodeFile(path);
			System.out.println("the path is :" + path);
			//srcrem(bmp);
			smallMap(bmp);
		} else {
			Toast.makeText(Main.this, "请重新选择图片", Toast.LENGTH_SHORT).show();
		}*/
		 if(resultCode == RESULT_OK){  
		        //选择图片  
		        Uri uri = data.getData();   
		        ContentResolver cr = this.getContentResolver();   
		        try {  
		            if(bmp != null)//如果不释放的话,不断取图片,将会内存不够  
		                bmp.recycle();  
		            bmp = BitmapFactory.decodeStream(cr.openInputStream(uri));  
		        } catch (FileNotFoundException e) {  
		            // TODO Auto-generated catch block  
		            e.printStackTrace();  
		        }  
		       //smallMap(bmp);
		        //srcrem(bmp);;
		        zoomBitmap(bmp,200,200);
		    }else{  
		        Toast.makeText(Main.this, "请重新选择图片", Toast.LENGTH_SHORT).show();  
		    }  
	}
	
	public void smallMap(Bitmap bmp){
		int bWidth = bmp.getWidth();
		int bHeight = bmp.getHeight();
		System.out.println("yuanWidth"+bWidth);
		System.out.println("yuanHeight"+bHeight);
		
		
		double scale = 0.1;
		
		scaleWidth  =(float)(scaleWidth * scale);
		scaleHeight = (float)(scaleHeight* scale);
		System.out.println("scaleWidth"+scaleWidth);
		System.out.println("scaleHeight"+scaleHeight);
		Matrix matrix = new Matrix();
		matrix.postScale(scaleHeight, scaleWidth);
		Bitmap createMap = Bitmap.createBitmap(bmp, 0, 0, bWidth, bHeight, matrix, true);
		
		srcrem(createMap);
		
		
		
	}
	
	
	public void zoomBitmap(Bitmap bitmap,int w,int h){   
        int width = bitmap.getWidth();   
        int height = bitmap.getHeight();   
        Matrix matrix = new Matrix();   
        float scaleWidht = ((float)w / width);   
        float scaleHeight = ((float)h / height);   
        matrix.postScale(scaleWidht, scaleHeight);   
        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);   
        
        
        srcrem(newbmp);
    }

}

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