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!!
  • 相关阅读:
    游戏服务器架构概要
    牛客网_Go语言相关练习_选择题(3)
    Go语言实践_实现一(服务器端)对多(客户端)在线聊天室
    Go语言实践_实现一(客户端)对一(服务器端)聊天室
    牛客网_Go语言相关练习_选择题(2)
    飞鱼48小时游戏创作嘉年华_厦门Pitch Time总结与收获
    牛客网_Go语言相关练习_选择题(1)
    Go语言_iota用法
    游戏服务器概述
    LeetCode_1. Two Sum_Solution
  • 原文地址:https://www.cnblogs.com/Catherine-Brain/p/3766714.html
Copyright © 2011-2022 走看看