zoukankan      html  css  js  c++  java
  • ImageView使用(适屏、缩放功能)

    1、适屏

    提取手机的图片库,并且进行选择图片的功能:

    Button onClick:
    Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 打开手机的图片库
                startActivityForResult(intent, IMAGE_SELECT);

    处理图片,按照手机的屏幕大小显示:

    if (requestCode == IMAGE_SELECT) {
                    Uri url = data.getData();// 获得图片的路径
                    int dw = getWindowManager().getDefaultDisplay().getWidth();// 获得屏幕的宽度
                    int dh = getWindowManager().getDefaultDisplay().getWidth() / 2;// 获得屏幕的高度/2
                    try {
                        /**
                         * BitampFactory :可以创建一个Bitmap位图对象
                         * BitampFactory.Options为匿名内部类
                         */
                        // 实现对图片的裁剪的类,是一个匿名内部类
                        BitmapFactory.Options factory = new BitmapFactory.Options();
                        factory.inJustDecodeBounds = true;// 如果设置为true,允许查询图片不是按照像素分配给内存
                        // 将一个流转换为Bitmap位图对象
                        Bitmap bmp = BitmapFactory.decodeStream(
                                getContentResolver().openInputStream(url), null,
                                factory);
                        // 对图片的宽度和高度对应手机的屏幕进行匹配
                        int hRatio = (int) Math
                                .ceil(factory.outHeight / (float) dh);
                        // 如果大于1 表示图片的高度大于手机屏幕的高度
                        int wRatio = (int) Math.ceil(factory.outWidth / (float) dw);
                        // 如果大于1表示图片的宽度大于手机屏幕的宽度
                        // 缩放到1/radio的尺寸 和1/radio^2像素
                        if (hRatio > 1 || wRatio > 1) {
                            if (hRatio > wRatio) {
                                factory.inSampleSize = hRatio;
                            } else {
                                factory.inSampleSize = wRatio;
                            }
                        }
    
                        factory.inJustDecodeBounds = false;
                        // 使用BitmapFactory对图片进行适屏的操作
                        bmp = BitmapFactory.decodeStream(getContentResolver()
                                .openInputStream(url), null, factory);
                        imageView.setImageBitmap(bmp);
    
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }

    2、缩放:

        Intent intent2 = getImageClipIntent();
                startActivityForResult(intent2, IMAGE_CUT);
     if (requestCode == IMAGE_CUT) {
                    Bitmap bitmap = data.getParcelableExtra("data");
                    imageView.setImageBitmap(bitmap);
                }
    Android 成长之路
  • 相关阅读:
    C++的常量折叠(一)
    如何写面向互联网公司的求职简历
    所有的程序员都是自学成才
    [一个经典的多线程同步问题]解决方案一:关键段CS
    [一个经典的多线程同步问题]问题引入
    多线程笔记--原子操作Interlocked系列函数
    【分治法】归并分类
    内存字节对齐一网打尽,再也不纠结
    在C语言中基本数据类型所占的字节数
    多线程笔记--先了解工具
  • 原文地址:https://www.cnblogs.com/liende/p/3901306.html
Copyright © 2011-2022 走看看