调用内置的Gallery查看SD卡中的图片,点击图片并回传,通过缩放与模拟器尺寸大小相适应并进行显示。
MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
private Button gallery;
private ImageView img,img2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iviview();
}
private void iviview() {
gallery = (Button) findViewById(R.id.gallery);
img = (ImageView) findViewById(R.id.img);
img2 = (ImageView) findViewById(R.id.img2);
gallery.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == gallery) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 0);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// 获得地址
Uri imguri = data.getData();
// 获得模拟器的宽和高
Display imgdp = getWindowManager().getDefaultDisplay();
int wd = imgdp.getWidth()/2;
int hd = imgdp.getHeight()/2-100;
// 我们要获得 图片的尺寸,如果图片很大 可能会导致图片溢出
// 加载图片并非是图片本身
BitmapFactory.Options bo = new BitmapFactory.Options();
bo.inJustDecodeBounds = true;
// 这里的流需要转换
try {
Bitmap bmp = BitmapFactory.decodeStream(
getContentResolver().openInputStream(imguri), null,
bo);
// 现在我们获得图片与模拟器的比例
int wdbili = (int) Math.ceil((float) bo.outWidth
/ (float) wd);
int hdbili = (int) Math.ceil((float) bo.outHeight
/ (float) hd);
// 判断比例大于1
if (wdbili > 1 || hdbili > 1) {
// 我们判断比例大小
if (wdbili > hdbili) {
bo.inSampleSize = wdbili;
} else {
bo.inSampleSize = hdbili;
}
}
// 处理完之后 我们还要从新加载图片
bo.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imguri), null, bo);
//在位图上绘制一张图片
Bitmap upbmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
//我们创建一张画布
Canvas cv = new Canvas(upbmp);
//我们创建一个笔
Paint paint = new Paint();
//我们要用笔进行绘制
//cv.drawBitmap(bmp, 0, 0, paint);
//创建一个matrix , 进行大小改变
Matrix matrix = new Matrix();
matrix.setValues(new float[]{
0.5f,0,0,
0,0.5f,0,
0,0,0.5f
});
//旋转(正数顺时针,负数逆时针)
matrix.setRotate(30);
//固定哪一个点进行旋转
matrix.setRotate(30);
matrix.setRotate(30, upbmp.getWidth(), upbmp.getHeight());
//平移
matrix.setTranslate(20, 20);
//缩放(倍数)
matrix.setScale(1.5f, 2);
//镜像的感觉
matrix.setScale(-1, 1);
matrix.postTranslate(upbmp.getWidth(), 0);
//倒立
matrix.setScale(1, -1);
matrix.postTranslate(0, upbmp.getHeight());
//我们通过matrix进行改变图片大小
cv.drawBitmap(bmp, matrix, paint);
//进行显示
img.setImageBitmap(bmp);
//进行显示
img2.setImageBitmap(upbmp);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="24dp"
android:text="打开画廊" />
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/gallery"
android:layout_below="@+id/gallery"
android:layout_marginTop="20dp"
/>
<ImageView
android:id="@+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/gallery"
android:layout_below="@+id/gallery"
android:layout_marginTop="20dp"
/>
</RelativeLayout>
网友评论