xml如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"     android:orientation="vertical">    <Button        android:id="@+id/selectImageBtn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="selectImageBtn" />    <Button        android:id="@+id/catImageBtn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="catImageBtn" />    <ImageView        android:id="@+id/imageView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/ic_action_search" /></LinearLayout>



package com.xy.caijiantupian;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity implements View.OnClickListener {/** Called when the activity is first created. */private Button selectImageBtn;private Button cutImageBtn;private ImageView imageView;private static final int IMAGE_SELECT = 1;private static final int IMAGE_CUT = 2;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);selectImageBtn = (Button) findViewById(R.id.selectImageBtn);cutImageBtn = (Button) findViewById(R.id.catImageBtn);imageView = (ImageView) findViewById(R.id.imageView);cutImageBtn.setOnClickListener(this);selectImageBtn.setOnClickListener(this);}public void onClick(View v) {// 截取适合屏幕大小的图片if (v == selectImageBtn) {Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);startActivityForResult(intent, IMAGE_SELECT);}// 一般用于头像等需要设置指定大小else if (v == cutImageBtn) {Intent intent = getImageClipIntent();startActivityForResult(intent, IMAGE_CUT);}}@Overrideprotected void onActivityResult(int requestCode, int resultCode,Intent intent) {// TODO Auto-generated method stubif (resultCode == RESULT_OK) {if (requestCode == IMAGE_SELECT) {Uri imageFileUri = intent.getData();int dw = getWindowManager().getDefaultDisplay().getWidth();int dh = getWindowManager().getDefaultDisplay().getHeight() / 2;// 已屏幕宽 和一般的高作为图片显示的最大尺寸try {BitmapFactory.Options factory = new BitmapFactory.Options();factory.inJustDecodeBounds = true; // 当为true时 允许查询图片不为// 图片像素分配内存Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri),null, factory);int hRatio = (int) Math.ceil(factory.outHeight / (float) dh); // 图片是高度的几倍int wRatio = (int) Math.ceil(factory.outWidth / (float) dw); // 图片是宽度的几倍System.out.println("hRatio:" + hRatio + "  wRatio:"+ wRatio);// 缩小到 1/ratio的尺寸和 1/ratio^2的像素if (hRatio > 1 || wRatio > 1) {if (hRatio > wRatio) {factory.inSampleSize = hRatio;} elsefactory.inSampleSize = wRatio;}factory.inJustDecodeBounds = false;bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, factory);imageView.setImageBitmap(bmp);} catch (Exception ex) {}} else if (requestCode == IMAGE_CUT) {Bitmap bmp = intent.getParcelableExtra("data");imageView.setImageBitmap(bmp);}}}/** * 获取剪切后的图片 */public static Intent getImageClipIntent() {Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);intent.setType("image/*");intent.putExtra("crop", "true");intent.putExtra("aspectX", 1);// 裁剪框比例intent.putExtra("aspectY", 1);intent.putExtra("outputX", 80);// 输出图片大小intent.putExtra("outputY", 80);intent.putExtra("return-data", true);return intent;}}



   

更多相关文章

  1. android左右滑动翻页查看图片
  2. Android内存缓存图片的标准方法
  3. Android根据不同语言切换图片
  4. Android 图片处理工具类汇总
  5. Android获取手屏幕尺寸
  6. android里图片下载工具类AsyncImageLoader分析
  7. android 自由缩放图片
  8. android中实现图片的上下移动
  9. android图片压缩并转为base64字符串

随机推荐

  1. 使用线程执行堆栈StackTraceElement设计A
  2. android中-----JSON数据解析
  3. Android(安卓)TextView文字超出一屏不能
  4. android media server 解析1-media playe
  5. Android媒体相关开发应用程序接口
  6. MyEclipse9.0 安装Android(安卓)ADT14
  7. adb wifi 链接调试Android设备
  8. android 放大镜--ShapeDrawable妙用
  9. Android(安卓)ActionBar 使用详解
  10. ClipboardService(CBS)中的权限管理