图片处理 缩放与圆形

缩放:


  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;
        }
    }


发布了29 篇原创文章 · 获赞 23 · 访问量 15万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览