Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gigamole.progressbuttons;
- import android.annotation.TargetApi;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Matrix;
- import android.graphics.Paint;
- import android.graphics.PorterDuff;
- import android.graphics.PorterDuffXfermode;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.Drawable;
- import android.os.Build;
- import android.util.AttributeSet;
- import android.view.View;
- import android.view.animation.Animation;
- import android.view.animation.Transformation;
- import java.io.File;
- import java.io.RandomAccessFile;
- import java.nio.MappedByteBuffer;
- import java.nio.channels.FileChannel;
- /**
- * Created by GIGAMOLE on 23.09.2015.
- */
- public class MaskImageView extends View {
- private final static float BITMAP_OFFSET = 60f;
- private float mOffset;
- private int mHeight;
- private int mWidth;
- private Matrix mNullMatrix = new Matrix();
- private Canvas mCircleCanvas;
- private Bitmap mCircleBitmap;
- private Matrix mCircleMatrix = new Matrix();
- private Canvas mRectCanvas;
- private Bitmap mRectBitmap;
- private Matrix mRectMatrix = new Matrix();
- private Canvas mEraseCanvas;
- private Bitmap mEraseBitmap;
- private Bitmap mBgBitmap;
- private Bitmap mSrcBitmap;
- final Paint mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG) {
- {
- setDither(true);
- setStyle(Style.FILL);
- }
- };
- final Paint mBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG) {
- {
- setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
- }
- };
- final Paint mMaskCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG) {
- {
- setDither(true);
- setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
- }
- };
- final Paint mMaskRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG) {
- {
- setDither(true);
- }
- };
- final Paint mRectPaint = new Paint() {
- {
- setDither(true);
- setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
- }
- };
- public void setBgBitmap(int bg) {
- mBgBitmap = drawableToBitmap(getResources().getDrawable(bg));
- postInvalidate();
- }
- public void setSrcBitmap(int src) {
- mSrcBitmap = drawableToBitmap(getResources().getDrawable(src));
- postInvalidate();
- }
- public MaskImageView(Context context) {
- this(context, null);
- }
- public MaskImageView(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
- }
- public MaskImageView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- super.onSizeChanged(w, h, oldw, oldh);
- mWidth = w;
- mHeight = h;
- mCircleBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
- mCircleCanvas = new Canvas(mCircleBitmap);
- mRectBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
- mRectCanvas = new Canvas(mRectBitmap);
- mEraseBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
- mEraseCanvas = new Canvas(mEraseBitmap);
- startAnimation(new SlideAnimation());
- mOffset = mHeight - (mHeight / 3);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- final int cWidth = canvas.getClipBounds().width();
- final int cHeight = canvas.getClipBounds().height();
- setCenterCrop(mCircleMatrix, mBgBitmap);
- mCircleCanvas.drawCircle(cWidth / 2f, cHeight / 2f, cHeight / 2f - 25f, mCirclePaint);
- mCircleCanvas.drawBitmap(mBgBitmap, mCircleMatrix, mBitmapPaint);
- canvas.drawBitmap(mCircleBitmap, mNullMatrix, null);
- updateFitStart(mRectMatrix, mSrcBitmap);
- mRectCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
- mRectCanvas.drawBitmap(mSrcBitmap, mRectMatrix, null);
- mEraseCanvas.drawRect(
- 0f,
- cHeight / 2f,
- cWidth,
- cHeight,
- mMaskRectPaint
- );
- mEraseCanvas.drawCircle(cWidth / 2f, cHeight / 2f, cHeight / 2f - 25f, mMaskCirclePaint);
- mRectCanvas.drawBitmap(mEraseBitmap, mNullMatrix, mRectPaint);
- canvas.drawBitmap(mRectBitmap, mNullMatrix, null);
- }
- private void updateFitStart(Matrix matrix, final Bitmap bitmap) {
- float wRatio = (float) (mWidth - 2f * ((float) mWidth / 6f)) / (float) bitmap.getWidth();
- // float hRatio = (float) mHeight / (float) bitmap.getHeight();
- // float scale = wRatio;
- // if (hRatio < wRatio)
- // scale = hRatio;
- matrix.setScale(wRatio, wRatio);
- matrix.postTranslate((mWidth / 6f), BITMAP_OFFSET + mOffset);
- }
- private void setCenterCrop(Matrix matrix, final Bitmap bitmap) {
- float scale;
- float dx = 0, dy = 0;
- if (bitmap.getWidth() * mHeight > mWidth * bitmap.getHeight()) {
- scale = (float) mHeight / (float) bitmap.getHeight();
- dx = (mWidth - bitmap.getWidth() * scale) * 0.5f;
- } else {
- scale = (float) mWidth / (float) bitmap.getWidth();
- dy = (mHeight - bitmap.getHeight() * scale) * 0.5f;
- }
- matrix.setScale(scale, scale);
- matrix.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
- }
- // private void setFitStart(Matrix matrix, final Bitmap bitmap) {
- // float wRatio = (float) (mWidth - 2f * ((float) mWidth / 6f)) / (float) bitmap.getWidth();
- // float hRatio = (float) mHeight / (float) bitmap.getHeight();
- //
- // float scale = wRatio;
- // if (hRatio < wRatio)
- // scale = hRatio;
- //
- // matrix.setScale(scale, scale);
- // matrix.postTranslate((mWidth / 6f), BITMAP_OFFSET);
- // }
- private Bitmap drawableToBitmap(final Drawable drawable) {
- if (drawable instanceof BitmapDrawable) {
- return convertToMutable(getContext(), ((BitmapDrawable) drawable.mutate()).getBitmap());
- }
- final Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
- final Canvas canvas = new Canvas(bitmap);
- drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
- drawable.draw(canvas);
- return convertToMutable(getContext(), bitmap);
- }
- @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
- private Bitmap convertToMutable(final Context context, final Bitmap imgIn) {
- final int width = imgIn.getWidth(), height = imgIn.getHeight();
- final Bitmap.Config type = imgIn.getConfig();
- File outputFile = null;
- final File outputDir = context.getCacheDir();
- try {
- outputFile = File.createTempFile(Long.toString(System.currentTimeMillis()), null, outputDir);
- outputFile.deleteOnExit();
- final RandomAccessFile randomAccessFile = new RandomAccessFile(outputFile, "rw");
- final FileChannel channel = randomAccessFile.getChannel();
- final MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, 0, imgIn.getRowBytes() * height);
- imgIn.copyPixelsToBuffer(map);
- imgIn.recycle();
- final Bitmap result = Bitmap.createBitmap(width, height, type);
- map.position(0);
- result.copyPixelsFromBuffer(map);
- channel.close();
- randomAccessFile.close();
- outputFile.delete();
- return result;
- } catch (final Exception e) {
- } finally {
- if (outputFile != null)
- outputFile.delete();
- }
- return null;
- }
- private class SlideAnimation extends Animation {
- @Override
- public void initialize(int width, int height, int parentWidth, int parentHeight) {
- super.initialize(width, height, parentWidth, parentHeight);
- setDuration(1000);
- setRepeatCount(INFINITE);
- setRepeatMode(REVERSE);
- }
- @Override
- protected void applyTransformation(float interpolatedTime, Transformation t) {
- mOffset = (mHeight - (mHeight / 3)) - (interpolatedTime * ((mHeight - (mHeight / 3)) - BITMAP_OFFSET));
- invalidate();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement