android学习笔记---图片拉伸和裁剪
2014-05-08 23:42
381 查看
package com.android.imageview;
import java.io.FileNotFoundException;
import java.net.URI;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class ImageViews extends Activity implements
android.view.View.OnClickListener {
private Button selectbutton, catbutton;
private ImageView imageView;
private static final int IMAGE_SELECT = 1;
private static final int IMAGE_CUT = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.imageview);
Button selButton = (Button) this.findViewById(R.id.select);
Button cuButton = (Button) this.findViewById(R.id.cut);
imageView = (ImageView) this.findViewById(R.id.imageview);
selButton.setOnClickListener(this);
cuButton.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == IMAGE_SELECT) {
Uri uri = data.getData();// 取得图片
// 取得手机屏幕的高度,/2避免图片拉伸
int dh = getWindowManager().getDefaultDisplay().getHeight() / 2;//屏高度
int dw = getWindowManager().getDefaultDisplay().getWidth();//屏宽度
// 设置factory参数对图片进行适屏缩放
BitmapFactory.Options factory = new BitmapFactory.Options();
factory.inJustDecodeBounds = true;
try {
Bitmap bmp = BitmapFactory.decodeStream(
getContentResolver().openInputStream(uri), null,
factory);
int hRatio = (int) Math
.ceil(factory.outHeight / (float) dh);
int wRatio = (int) Math.ceil(factory.outWidth / (float) dw);
Log.d("TAG", "hRatio = " + hRatio + " wRatio = " + wRatio);
if (hRatio > 1 || wRatio > 1) {
if (hRatio > wRatio) {
factory.inSampleSize = hRatio;
} else {
factory.inSampleSize = wRatio;
}
}
//必须设置为false,否则得不到缩放后的图片
factory.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri), null, factory);
imageView.setImageBitmap(bmp);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (requestCode == IMAGE_CUT) {
//“data”参数必须与"return-data"保持一致
Bitmap bmp = data.getParcelableExtra("data");
imageView.setImageBitmap(bmp);
;
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.select:
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMAGE_SELECT);
break;
case R.id.cut:
Intent intent2 = getImageCutIntent();
startActivityForResult(intent2, IMAGE_CUT);
default:
break;
}
}
private Intent getImageCutIntent() {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
// 所有类型的图片
intent.setType("image/*");
//注意第二个参数是字符串类型
intent.putExtra("crop", "true");// 允许滑动选择
intent.putExtra("aspectX", 1);// 1:1比例进行裁剪
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 80);// 输出图片的大小
intent.putExtra("outputY", 80);
//注意第一个参数,换做其他字符串都不行
intent.putExtra("return-data", true);// 有返回值
return intent;
}
}
import java.io.FileNotFoundException;
import java.net.URI;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class ImageViews extends Activity implements
android.view.View.OnClickListener {
private Button selectbutton, catbutton;
private ImageView imageView;
private static final int IMAGE_SELECT = 1;
private static final int IMAGE_CUT = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.imageview);
Button selButton = (Button) this.findViewById(R.id.select);
Button cuButton = (Button) this.findViewById(R.id.cut);
imageView = (ImageView) this.findViewById(R.id.imageview);
selButton.setOnClickListener(this);
cuButton.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == IMAGE_SELECT) {
Uri uri = data.getData();// 取得图片
// 取得手机屏幕的高度,/2避免图片拉伸
int dh = getWindowManager().getDefaultDisplay().getHeight() / 2;//屏高度
int dw = getWindowManager().getDefaultDisplay().getWidth();//屏宽度
// 设置factory参数对图片进行适屏缩放
BitmapFactory.Options factory = new BitmapFactory.Options();
factory.inJustDecodeBounds = true;
try {
Bitmap bmp = BitmapFactory.decodeStream(
getContentResolver().openInputStream(uri), null,
factory);
int hRatio = (int) Math
.ceil(factory.outHeight / (float) dh);
int wRatio = (int) Math.ceil(factory.outWidth / (float) dw);
Log.d("TAG", "hRatio = " + hRatio + " wRatio = " + wRatio);
if (hRatio > 1 || wRatio > 1) {
if (hRatio > wRatio) {
factory.inSampleSize = hRatio;
} else {
factory.inSampleSize = wRatio;
}
}
//必须设置为false,否则得不到缩放后的图片
factory.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(uri), null, factory);
imageView.setImageBitmap(bmp);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (requestCode == IMAGE_CUT) {
//“data”参数必须与"return-data"保持一致
Bitmap bmp = data.getParcelableExtra("data");
imageView.setImageBitmap(bmp);
;
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.select:
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMAGE_SELECT);
break;
case R.id.cut:
Intent intent2 = getImageCutIntent();
startActivityForResult(intent2, IMAGE_CUT);
default:
break;
}
}
private Intent getImageCutIntent() {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
// 所有类型的图片
intent.setType("image/*");
//注意第二个参数是字符串类型
intent.putExtra("crop", "true");// 允许滑动选择
intent.putExtra("aspectX", 1);// 1:1比例进行裁剪
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 80);// 输出图片的大小
intent.putExtra("outputY", 80);
//注意第一个参数,换做其他字符串都不行
intent.putExtra("return-data", true);// 有返回值
return intent;
}
}
相关文章推荐
- Android学习笔记19:ImageView实现图片适屏与裁剪
- Android学习笔记19-ImageView实现图片适屏与裁剪
- HTML5 canvas实现图片拉伸、压缩与裁剪
- iOS 选取相册图片并裁剪,解决获取图片拉伸问题
- Android学习笔记19:ImageView实现图片适屏与裁剪
- Android 按指定尺寸 裁剪或拉伸图片的方法
- imgareaselect+java实现图片裁剪保存
- 对图片裁剪框架 ucrop 的二次封装的工具类,添加从图库选择和拍照获取图片,项目中可直接使用,十分便捷
- 图片拉伸方法
- android 调用ZXing包实现二维码预览框图片有拉伸
- android:background 图片被拉伸解决方法
- Android自定义控件系列八:详解onMeasure()(二)--利用onMeasure测量来实现图片拉伸永不变形,解决屏幕适配问题
- 类似9patch效果的iOS图片拉伸
- 图片缩放裁剪
- 解决使用drawRect绘制界面,图片拉伸问题
- 按钮上拉伸图片
- openCV学习笔记(8):利用cvSetImageROI截取/裁剪图片
- 图片固定在图片框内,大小自动适应,宽高避免拉伸[摘自布布分享,tech.bubufx.com]
- android选择图片进行裁剪
- iOS开发 - 技巧 - 01 - 图片拉伸方式