android 让图片放大,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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值