图片处理 缩放与圆形

缩放:


  private class UpdateThumbnailTask extends AsyncTask<Void, Void, Bitmap> {
        private final byte[] mJpegData;
        private final boolean mCheckOrientation;

        public UpdateThumbnailTask(final byte[] jpegData, boolean checkOrientation) {
            mJpegData = jpegData;
            mCheckOrientation = checkOrientation;
        }

        @Override
        protected Bitmap doInBackground(Void... params) {
            if (mJpegData != null)
                return decodeImageCenter(null);

            LocalDataAdapter adapter = getDataAdapter();
            ImageData img = adapter.getImageData(1);
            if (img == null) {
                return null;
            }
            Uri uri = img.getContentUri();
            String path = getPathFromUri(uri);
            if (path == null) {
                return null;
            }
            else {
                if (img.isPhoto()) {
                    return decodeImageCenter(path);
                } else {
                    return ThumbnailUtils
                            .createVideoThumbnail(path, MediaStore.Video.Thumbnails.MINI_KIND);
                }
            }
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (bitmap == null) {
                if (mThumbnail != null) {
                    mThumbnail.setVisibility(View.GONE);
                }
            } else {
                updateThumbnail(bitmap);
            }
        }

        private Bitmap decodeImageCenter(final String path) {
            // Check photo orientation for Panorama. This is necessary during app launch because
            // Panorama module generates thumbnail bitmap with orientation adjustment but only
            // saves jpeg with orientation tag set.
            int orientation = 0;
            if (mCheckOrientation) {
                ExifInterface exif = new ExifInterface();
                try {
                    if (mJpegData != null) {
                        exif.readExif(mJpegData);
                    } else {
                        exif.readExif(path);
                    }
                    orientation = Exif.getOrientation(exif);
                } catch (IOException e) {
                    // ignore
                }
            }

            final BitmapFactory.Options opt = new BitmapFactory.Options();
            opt.inJustDecodeBounds = true;
            if (mJpegData != null) {
                BitmapFactory.decodeByteArray(mJpegData, 0, mJpegData.length, opt);
            } else {
                BitmapFactory.decodeFile(path, opt);
            }

            int w = opt.outWidth;
            int h = opt.outHeight;
            int d = w > h ? h : w;
            final Rect rect = w > h ? new Rect((w - h) / 2, 0, (w + h) / 2, h)
                    : new Rect(0, (h - w) / 2, w, (h + w) / 2);

            final int target = getResources().getDimensionPixelSize(R.dimen.capture_size); // capture_size is 32dip
            int sample = 1;
            if (d > target) {
                while (d / sample / 2 > target) {
                    sample *= 2;
                }
            }

            opt.inJustDecodeBounds = false;
            opt.inSampleSize = sample;
            final BitmapRegionDecoder decoder;
            try {
                if (mJpegData == null) {
                    decoder = BitmapRegionDecoder.newInstance(path, true);
                } else {
                    decoder = BitmapRegionDecoder.newInstance(mJpegData, 0, mJpegData.length, true);
                }
            } catch (IOException e) {
                return null;
            }
            Bitmap bitmap = decoder.decodeRegion(rect, opt);
            if (orientation != 0) {
                Matrix matrix = new Matrix();
                matrix.setRotate(orientation);
                bitmap = Bitmap.createBitmap(bitmap, 0, 0,
                        bitmap.getWidth(), bitmap.getHeight(), matrix, false);
            }
            return bitmap;
        }
    }

圆形


 private class CircularDrawable extends Drawable {
        private final BitmapShader mBitmapShader;
        private final Paint mPaint;
        private Rect mRect;
        private int mLength;

        public CircularDrawable(Bitmap bitmap) {
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();
            if (w > h) {
                mLength = h;
                bitmap = Bitmap.createBitmap(bitmap, (w - h) / 2, 0, h, h);
            } else if (w < h) {
                mLength = w;
                bitmap = Bitmap.createBitmap(bitmap, 0, (h - w) / 2, w, w);
            }else{
                // h=w;
                //yanglei add for bug 73627 20150806
                mLength = h;
            }

            mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setShader(mBitmapShader);
        }

        @Override
        protected void onBoundsChange(Rect bounds) {
            super.onBoundsChange(bounds);
            mRect = bounds;
        }

        @Override
        public void draw(Canvas canvas) {
            canvas.drawRoundRect(new RectF(mRect), (mRect.right - mRect.left) / 2,
                    (mRect.bottom - mRect.top) / 2, mPaint);
        }

        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }

        @Override
        public void setAlpha(int alpha) {
            mPaint.setAlpha(alpha);
        }

        @Override
        public void setColorFilter(ColorFilter filter) {
            mPaint.setColorFilter(filter);
        }

        @Override
        public int getIntrinsicWidth() {
            return mLength;
        }

        @Override
        public int getIntrinsicHeight() {
            return mLength;
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值