ImageView的属性android:scaleType,即 ImageView.setScaleType(ImageView.ScaleType)。android:scaleType是控制图片如何 resized/moved来匹对ImageView的size。
CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示
CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)
CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽
FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示
FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
FIT_XY / fitXY 把图片不按比例扩大/缩小到View的大小显示
MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。
下面是我写的通过照相获取图片并进行截取,然后显示在imageview中
photo_layout.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:id="@+id/button1" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:onClick="click1" 12 android:text="photo" /> 13 <Button 14 android:id="@+id/button2" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:onClick="click2" 18 android:text="xiangche"/> 19 20 <ImageButton 21 android:id="@+id/picture1" 22 android:layout_width="200dp" 23 android:layout_height="200dp" 24 android:layout_gravity="center_horizontal" 25 android:scaleType="fitXY" 26 /> 27 28 </LinearLayout>
然后是activity
package com.example.photo; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import com.example.dazuoye.R; import android.annotation.TargetApi; import android.app.Activity; import android.content.ContentUris; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.provider.DocumentsContract; import android.provider.MediaStore; import android.provider.MediaStore.Images.Media; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.ImageView; import android.widget.Toast; public class tupian extends Activity { public static final int TAKE_PHOTO=1; public static final int CROP_PHOTO=2; public static final int CHOOSE_PHOTO=3; private Uri imageUri; private ImageView picture; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.photo_layout); picture=(ImageView) findViewById(R.id.picture1); WindowManager wm=(WindowManager) getSystemService(WINDOW_SERVICE); // int height=wm.getDefaultDisplay().getHeight(); // int width=wm.getDefaultDisplay().getWidth(); // System.out.println("width"+width+"height"+height); } public void click1(View v){ File outputImage=new File(Environment.getExternalStorageDirectory(), "output_image.jpg"); try{ if(outputImage.exists()){ outputImage.delete(); } outputImage.createNewFile(); }catch(IOException e){ e.printStackTrace(); } imageUri=Uri.fromFile(outputImage); Intent intent=new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, TAKE_PHOTO); } public void click2(View v){ Intent intent=new Intent("android.intent.action.GET_CONTENT"); intent.setType("image/*"); startActivityForResult(intent, CHOOSE_PHOTO); } @Override protected void onActivityResult(int requestCode,int resultCode,Intent data){ switch(requestCode){ case TAKE_PHOTO: if(resultCode == RESULT_OK){ Intent intent=new Intent("com.android.camera.action.CROP"); intent.setDataAndType(imageUri, "image/*"); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, CROP_PHOTO); } break; case CROP_PHOTO: if(resultCode==RESULT_OK){ try{ Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); // int imgwidth=bitmap.getWidth(); // int imgheight=bitmap.getHeight(); // System.out.println("heightasd"+imgheight+"width"+imgwidth); System.out.println("sad"); picture.setImageBitmap(bitmap); }catch(FileNotFoundException e){ e.printStackTrace(); } } break; default: break; } } }