博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 图片放大和缩小
阅读量:6956 次
发布时间:2019-06-27

本文共 4899 字,大约阅读时间需要 16 分钟。

hot3.png

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);    }}

转载于:https://my.oschina.net/xiahuawuyu/blog/104795

你可能感兴趣的文章
JS心得——判断一个对象是否为空
查看>>
软件工程开学第一课
查看>>
深入理解正则表达式高级教程
查看>>
Selenium webdriver+Java-------如何等待页面元素加载完成
查看>>
学习MySQL(上)
查看>>
我开通了cnblog的博客 大家快来看吧
查看>>
P1115 最大子段和
查看>>
poj 3617 best cow line
查看>>
动态列报表
查看>>
JAVA 8 多态
查看>>
九,编程的三种结构
查看>>
python爬虫系列之爬京东手机数据
查看>>
贪吃蛇
查看>>
iphone-common-codes-ccteam源代码 CCRectangleBlock.h
查看>>
JS实现Ajax---例:获取服务器时间
查看>>
iOS边练边学--定时任务和HUD
查看>>
大型网站架构读后感
查看>>
你必须了解的java内存管理机制(二)-内存分配
查看>>
注释类型 XmlType
查看>>
bzoj 1911: [Apio2010]特别行动队
查看>>