Android之UI学习篇七:ImageView实现适屏和裁剪图片的功能
16lz
2022-04-15
ImageView实现图片适应屏幕大小显示,和图片裁剪的功能.
实现的效果
主界面:
适应屏幕:
裁剪图片:
显示裁剪图片到ImagView:
源代码:
MainActivity.java
[html] view plain copy print ?
- package com.p_w_picpathview.activity;
- import java.io.FileNotFoundException;
- import android.app.Activity;
- 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.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.Toast;
- public class MainActivity extends Activity implements OnClickListener {
- private Button p_w_picpathSelectBtn;
- private Button p_w_picpathCutBtn;
- private ImageView p_w_picpathView;
- // 声明两个静态整型变量,用于意图的返回标志
- private static final int IMAGE_SELECT = 1; // 选择图片
- private static final int IMAGE_CUT = 2; // 裁剪图片
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- setupViews();
- }
- // 我的一贯作风呵呵
- public void setupViews() {
- p_w_picpathSelectBtn = (Button) findViewById(R.id.p_w_picpathSelectButton);
- p_w_picpathSelectBtn.setOnClickListener(this);
- p_w_picpathCutBtn = (Button) findViewById(R.id.p_w_picpathCutButton);
- p_w_picpathCutBtn.setOnClickListener(this);
- p_w_picpathView = (ImageView) findViewById(R.id.p_w_picpathView);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (resultCode == RESULT_OK) {
- // 处理图片按照手机屏幕大小显示
- if (requestCode == IMAGE_SELECT) {
- // 获得图片的路径
- Uri uri = data.getData();
- // 获得屏幕宽度
- int dw = getWindowManager().getDefaultDisplay().getWidth();
- // 获得屏幕宽度
- int dh = getWindowManager().getDefaultDisplay().getHeight() / 2;
- try {
- // 实现对图片裁剪的类,是一个匿名内部类
- BitmapFactory.Options factory = new BitmapFactory.Options();
- // 如果设置为true,允许查询图片不是按照像素分配内存
- factory.inJustDecodeBounds = true;
- Bitmap bmp = BitmapFactory.decodeStream(
- getContentResolver().openInputStream(uri), null,
- factory);
- // 对图片的高度和宽度对应手机屏幕进行匹配
- // 宽度之比
- int wRatio = (int) Math.ceil(factory.outWidth / (float) dw);
- // 高度之比
- int hRatio = (int) Math.ceil(factory.outHeight / (float) dh);
- // 如果wRatio大于1,表示图片的宽度大于屏幕宽度,类似hRatio
- if (wRatio > 1 || hRatio > 1) {
- // inSampleSize>1则返回比原图更小的图片
- if (hRatio > wRatio) {
- factory.inSampleSize = hRatio;
- } else {
- factory.inSampleSize = wRatio;
- }
- }
- // 该属性为false则允许调用者查询图片无需为像素分配内存
- factory.inJustDecodeBounds = false;
- // 再次使用BitmapFactory对象图像进行适屏操作
- bmp = BitmapFactory.decodeStream(getContentResolver()
- .openInputStream(uri), null, factory);
- p_w_picpathView.setImageBitmap(bmp);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- } else if (requestCode == IMAGE_CUT) { // 裁剪图片
- // 一定要和"return-data"返回的标签"data"一致
- Bitmap bmp = data.getParcelableExtra("data");
- p_w_picpathView.setImageBitmap(bmp);
- }
- }
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.p_w_picpathSelectButton:
- // 如何提取手机的图片,并且进行图片的选择
- Intent intent = new Intent(
- Intent.ACTION_PICK,
- android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- startActivityForResult(intent, IMAGE_SELECT);
- break;
- case R.id.p_w_picpathCutButton:
- Intent intent2 = getImageClipIntent();
- startActivityForResult(intent2, IMAGE_CUT);
- break;
- default:
- break;
- }
- }
- // 获取裁剪图片意图的方法
- private Intent getImageClipIntent() {
- Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
- // 实现对图片的裁剪,必须要设置图片的属性和大小
- intent.setType("p_w_picpath/*"); // 设置属性,表示获取任意类型的图片
- intent.putExtra("crop", "true");// 设置可以滑动选选择区域的属性,注意这里是字符串"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;
- }
- }
布局文件main.xml
[html] view plain copy print ?
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="选择图片"
- android:id="@+id/p_w_picpathSelectButton"/>
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="选择图片进行裁剪"
- android:id="@+id/p_w_picpathCutButton"/>
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/p_w_picpathView"/>
- LinearLayout>
更多相关文章
- Android中屏幕密度和图片大小的关系分析
- MixtureTextView 支持Android图文混排、文字环绕图片等效果
- Android多点触控揭秘
- Android在Activity中获得控件宽高和截屏操作
- Android(安卓)Button 上添加图片
- 剪切图片-扩展android 选择图片(从手机照相机或手机图片)
- Android中屏幕密度和图片大小的关系分析
- 剪切图片-扩展android 选择图片(从手机照相机或手机图片)
- Android(安卓)的状态栏设置图片填充或者颜色填充