Android 图形处理

一、Load图形到内存中
对于像数码相机相片特别大,内存吃不消,此时加载到内存前,需要处理,防止出现OOM异常。
方法1:
            原理:只显示原图的1/8,通过BitmapFactory.Options来实现 。
            实现:
                     BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
                     bmpFactoryOptions.inSampleSize = 8;
                     Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
                     imv.setImageBitmap(bmp);
方法2:
         原理:根据当前屏幕分辨率的大小,加载图片。
         实现:
                  BitmapFactory.Options opt = new BitmapFactory.Options();
                  opt.inJustDecodeBounds = true;
                  Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, opt);
                  int heightRatio = (int)Math.ceil(opt.outHeight/(float)dh);
                  int widthRatio = (int)Math.ceil(opt.outWidth/(float)dw);
                      
                  //判断是否要进行缩放
                  if (heightRatio > 1 && widthRatio > 1){
                         if (heightRatio > widthRatio){
                                opt.inSampleSize = heightRatio;//高度变化大,按高度缩放
                         }else{
                                opt.inSampleSize = widthRatio;    // 宽度变化大,按宽度缩放
                         }
                   }
                   opt.inJustDecodeBounds = false;
                   bmp = BitmapFactory.decodeFile(imageFilePath, opt);   


二、获取Exif图片信息
       //从文件获取exif信息
       ExifInterface ei = new ExifInterface(imageFilePath);
       String imageDescription = ei.getAttribute("ImageDescription");
       if (imageDescription != null)  {
            Log.v("EXIF", imageDescription);
        }
        //把exif信息写到文件:
        ExifInterface ei = new ExifInterface(imageFilePath);
        ei.setAttribute("ImageDescription","Something New");


三、从gallery获取一个图形
       Intent intent = new Intent(Intent.ACTION_PICK);
       intent.setType(“image/*”);
       intent.getData() ;//获取image的uri
       Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().??
       openInputStream(imageFileUri), null, opt);


四、创建bitmap拷贝
       Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().??
       openInputStream(imageFileUri), null, opt);
       Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(),??
       bmp.getConfig());
       Canvas canvas = new Canvas(alteredBitmap);
       Paint paint = new Paint();
       canvas.drawBitmap(bmp, 0, 0, paint);
五、图形缩放
       Matrix matrix = new Matrix();
       matrix.setValues(new float[] {
               1, 0, 0,
               0, 1, 0,
               0, 0, 1
        });
        x = 1x + 0y + 0z
        y = 0x + 1y + 0z
        z = 0x + 0y + 1z

       通过canvas.drawBitmap(bmp, matrix, paint);创建bitmap
       1.水平缩放0.5
       2.垂直拉扯2倍

        matrix.setScale(1.5f,1);//水平点放大到1.5f,垂直1


六、图形旋转
        Matrix matrix = new Matrix();
        matrix.setRotate(15);
        canvas.drawBitmap(bmp, matrix, paint);
        paint.setAntiAlias(true);//消除锯齿
        matrix.setRotate(15,bmp.getWidth()/2,bmp.getHeight()/2);  //指定圆心的旋转
        Matrix matrix = new Matrix();
        matrix.setRotate(15,bmp.getWidth()/2,bmp.getHeight()/2);
        alteredBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(),matrix, false);
        alteredImageView.setImageBitmap(alteredBitmap);

七、图形平移
       setTranslate(1.5f,-10);

八、镜子效果
       matrix.setScale(-1, 1);
       matrix.postTranslate(bmp.getWidth(),0);

九、倒影效果
       matrix.setScale(1, -1);
       matrix.postTranslate(0, bmp.getHeight());

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。