public class MainActivity extends Activity {
private ImageView img;
private View.OnClickListener imgViewListener;
private Bitmap myBitmap;
private byte[] mContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)this.findViewById(R.id.img);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final CharSequence[] items = { "相册", "拍照" };
AlertDialog dlg = new AlertDialog.Builder(MainActivity.this).setTitle("选择照片").setItems(items,
new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
if(which==1){
Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(getImageByCamera, 1);
}else{
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType("image/jpg");
startActivityForResult(getImage, 0);
}
}
}).create();
dlg.show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
ContentResolver contentResolver =getContentResolver();
if(requestCode==0){
//方式一
try {
//获得图片的uri
Uri orginalUri = data.getData();
// 将图片内容解析成字节数组
mContent = readStream(contentResolver.openInputStream(Uri.parse(orginalUri.toString())));
//将字节数组转换为ImageView可调用的Bitmap对象
myBitmap =getPicFromBytes(mContent,null);
把得到的图片绑定在控件上显示
img.setImageBitmap(myBitmap);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
//方式二
try {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
img.setImageBitmap(BitmapFactory.decodeFile(picturePath));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}else if(requestCode==1){
try {
Bundle extras = data.getExtras();
myBitmap = (Bitmap) extras.get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG , 100, baos);
mContent=baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
img.setImageBitmap(myBitmap);
}
}
public static Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) {
if (bytes != null)
if (opts != null)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,opts);
else
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return null;
}
public static byte[] readStream(InputStream in) throws Exception{
byte[] buffer =new byte[1024];
int len =-1;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while((len=in.read(buffer))!=-1){
outStream.write(buffer, 0, len);
}
byte[] data =outStream.toByteArray();
outStream.close();
in.close();
return data;
}
}
上面都是正常的图片调用并绑定到imageView上的,让它同比剪切的关键是在ImageView上添加一句:android:scaleType="centerCrop"
Android:scaleType是控制图片如何resized/moved来匹对ImageView的size。
ImageView.ScaleType / android:scaleType值的意义区别:
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 用矩阵来绘制,动态缩小放大图片来显示。
** 要注意一点,Drawable文件夹里面的图片命名是不能大写的