zoukankan      html  css  js  c++  java
  • Android中将自定义图片的比例

         虽然,以前也有那种获取相册里面的相片,然后,通过剪切图片来得到自己的想要的图片,到那时对于如果想要一批图片的处理效果,那么,相对于剪切来说,我们自动在代码里面实现自己想要的图片的比例来剪切。然后来得到自己想要的图片比例。

        其实这里就是先将图片转换为bitmap,然后再将它存进字节里面,然后再根据自己需要的比例来得到压塑后的图片,最后得到。

      这里就是关键代码:然后实现这个方法就okay了:

        // to define a method to make the image
        public Bitmap getBitmap(byte[] data, int width, int height) {
            Bitmap bitmap = null;
            Options opts = new Options();
            opts.inJustDecodeBounds = true;
            opts.inSampleSize = 2;
            opts.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
            return bitmap;
        }

       会看到这里会需要一个参数,那就是一个字节,当然,这个字节就是将自己需要的图片然后转换为这个字节就okay了:

    // first to change the bitmap
            Resources res = getResources();
            Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.demo);
            // to get the byte
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
    
            bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
    
            byte[] data = baos.toByteArray();
    
            Bitmap bit = getBitmap(data, 300, 300);
            // to exchange
            BitmapDrawable draw = new BitmapDrawable(bit);
            img_Two.setBackground(draw);

    以上就是将一个资源下的图片换成bitmap形式的,然后在存进byte里面,然后叜调用上一个转换的方法,然后返回一个bitmap,然后在用相应的控件来得到就okay了。

    希望这个对大家有用哦!

    一切只是为了充实自己!!stay hungry and stay foolish!!
  • 相关阅读:
    ARC109 题解&总结 ABCDEF
    List、Set、Map的学习
    数据结构和算法-栈
    spring-常见知识点(一)
    spring-AOP实现原理(二)
    spring-AOP实现原理(一)
    MVCC详解
    MVCC能否解决幻读
    ArrayList和LinkedList的区别
    架构设计-秒杀架构设计
  • 原文地址:https://www.cnblogs.com/Catherine-Brain/p/3766714.html
Copyright © 2011-2022 走看看