ImageView实现图片适应屏幕大小显示,和图片裁剪的功能.

实现的效果

主界面:
 


 

适应屏幕:


 

裁剪图片:


 


 

显示裁剪图片到ImagView:

 


 

源代码:

MainActivity.java

 

[html] view plain copy print ?
  1. package com.p_w_picpathview.activity;  
  2.   
  3. import java.io.FileNotFoundException;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.BitmapFactory;  
  8. import android.net.Uri;  
  9. import android.os.Bundle;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.ImageView;  
  15. import android.widget.Toast;  
  16.   
  17. public class MainActivity extends Activity implements OnClickListener {  
  18.     private Button p_w_picpathSelectBtn;  
  19.     private Button p_w_picpathCutBtn;  
  20.     private ImageView p_w_picpathView;  
  21.     // 声明两个静态整型变量,用于意图的返回标志  
  22.     private static final int IMAGE_SELECT = 1; // 选择图片  
  23.     private static final int IMAGE_CUT = 2; // 裁剪图片  
  24.   
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.         setupViews();  
  30.     }  
  31.   
  32.     // 我的一贯作风呵呵  
  33.     public void setupViews() {  
  34.         p_w_picpathSelectBtn = (Button) findViewById(R.id.p_w_picpathSelectButton);  
  35.         p_w_picpathSelectBtn.setOnClickListener(this);  
  36.         p_w_picpathCutBtn = (Button) findViewById(R.id.p_w_picpathCutButton);  
  37.         p_w_picpathCutBtn.setOnClickListener(this);  
  38.         p_w_picpathView = (ImageView) findViewById(R.id.p_w_picpathView);  
  39.     }  
  40.   
  41.     @Override  
  42.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  43.         super.onActivityResult(requestCode, resultCode, data);  
  44.         if (resultCode == RESULT_OK) {  
  45.             // 处理图片按照手机屏幕大小显示  
  46.             if (requestCode == IMAGE_SELECT) {  
  47.                 // 获得图片的路径  
  48.                 Uri uri = data.getData();   
  49.                 // 获得屏幕宽度  
  50.                 int dw = getWindowManager().getDefaultDisplay().getWidth();   
  51.                 // 获得屏幕宽度  
  52.                 int dh = getWindowManager().getDefaultDisplay().getHeight() / 2;   
  53.                 try {  
  54.                     // 实现对图片裁剪的类,是一个匿名内部类  
  55.                     BitmapFactory.Options factory = new BitmapFactory.Options();  
  56.                     // 如果设置为true,允许查询图片不是按照像素分配内存  
  57.                     factory.inJustDecodeBounds = true;  
  58.                     Bitmap bmp = BitmapFactory.decodeStream(  
  59.                             getContentResolver().openInputStream(uri), null,  
  60.                             factory);  
  61.                     // 对图片的高度和宽度对应手机屏幕进行匹配  
  62.                     // 宽度之比  
  63.                     int wRatio = (int) Math.ceil(factory.outWidth / (float) dw);   
  64.                     // 高度之比  
  65.                     int hRatio = (int) Math.ceil(factory.outHeight / (float) dh);   
  66.                     // 如果wRatio大于1,表示图片的宽度大于屏幕宽度,类似hRatio  
  67.                     if (wRatio > 1 || hRatio > 1) {  
  68.                         // inSampleSize>1则返回比原图更小的图片  
  69.                         if (hRatio > wRatio) {  
  70.                             factory.inSampleSize = hRatio;  
  71.                         } else {  
  72.                             factory.inSampleSize = wRatio;  
  73.                         }  
  74.                     }  
  75.                     // 该属性为false则允许调用者查询图片无需为像素分配内存  
  76.                     factory.inJustDecodeBounds = false;  
  77.                     // 再次使用BitmapFactory对象图像进行适屏操作  
  78.                     bmp = BitmapFactory.decodeStream(getContentResolver()  
  79.                             .openInputStream(uri), null, factory);  
  80.                     p_w_picpathView.setImageBitmap(bmp);  
  81.                 } catch (FileNotFoundException e) {  
  82.                     e.printStackTrace();  
  83.                 }  
  84.             } else if (requestCode == IMAGE_CUT) { // 裁剪图片  
  85.                 // 一定要和"return-data"返回的标签"data"一致  
  86.                 Bitmap bmp = data.getParcelableExtra("data");   
  87.                 p_w_picpathView.setImageBitmap(bmp);  
  88.             }  
  89.         }  
  90.     }  
  91.   
  92.     @Override  
  93.     public void onClick(View v) {  
  94.         switch (v.getId()) {  
  95.         case R.id.p_w_picpathSelectButton:  
  96.             // 如何提取手机的图片,并且进行图片的选择  
  97.             Intent intent = new Intent(  
  98.                     Intent.ACTION_PICK,  
  99.                     android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
  100.             startActivityForResult(intent, IMAGE_SELECT);  
  101.             break;  
  102.         case R.id.p_w_picpathCutButton:  
  103.             Intent intent2 = getImageClipIntent();  
  104.             startActivityForResult(intent2, IMAGE_CUT);  
  105.             break;  
  106.         default:  
  107.             break;  
  108.         }  
  109.     }  
  110.   
  111.     // 获取裁剪图片意图的方法  
  112.     private Intent getImageClipIntent() {  
  113.         Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);  
  114.         // 实现对图片的裁剪,必须要设置图片的属性和大小  
  115.         intent.setType("p_w_picpath/*"); // 设置属性,表示获取任意类型的图片  
  116.         intent.putExtra("crop", "true");// 设置可以滑动选选择区域的属性,注意这里是字符串"true"  
  117.         intent.putExtra("aspectX", 1);// 设置剪切框1:1比例的效果  
  118.         intent.putExtra("aspectY", 1);  
  119.         intent.putExtra("outputX", 80);  
  120.         intent.putExtra("outputY", 80);  
  121.         intent.putExtra("return-data", true);  
  122.         return intent;  
  123.     }  
  124. }  



 

 

 

布局文件main.xml

 

[html] view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button   
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="选择图片"  
  11.         android:id="@+id/p_w_picpathSelectButton"/>  
  12.     <Button   
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="选择图片进行裁剪"  
  16.         android:id="@+id/p_w_picpathCutButton"/>  
  17.       
  18.     <ImageView   
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:id="@+id/p_w_picpathView"/>  
  22.   
  23. LinearLayout>  

更多相关文章

  1. Android中屏幕密度和图片大小的关系分析
  2. MixtureTextView 支持Android图文混排、文字环绕图片等效果
  3. Android多点触控揭秘
  4. Android在Activity中获得控件宽高和截屏操作
  5. Android(安卓)Button 上添加图片
  6. 剪切图片-扩展android 选择图片(从手机照相机或手机图片)
  7. Android中屏幕密度和图片大小的关系分析
  8. 剪切图片-扩展android 选择图片(从手机照相机或手机图片)
  9. Android(安卓)的状态栏设置图片填充或者颜色填充

随机推荐

  1. CAMERA(12)---[Android相机]光线传感器识
  2. Webview实现Android和JS通信
  3. android inputType属性
  4. android EditText中inputType的属性列表
  5. Android笔试总结
  6. Android的线程
  7. 搭建Android(安卓)UI Testing自动化测试
  8. 初学者---Android(安卓)Fragment之间数据
  9. Android和Linux kernel版本对应表
  10. Android开发之旅:android架构