Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.46 KB | None | 0 0
  1. package com.gigamole.progressbuttons;
  2.  
  3. import android.annotation.TargetApi;
  4. import android.content.Context;
  5. import android.graphics.Bitmap;
  6. import android.graphics.Canvas;
  7. import android.graphics.Matrix;
  8. import android.graphics.Paint;
  9. import android.graphics.PorterDuff;
  10. import android.graphics.PorterDuffXfermode;
  11. import android.graphics.drawable.BitmapDrawable;
  12. import android.graphics.drawable.Drawable;
  13. import android.os.Build;
  14. import android.util.AttributeSet;
  15. import android.view.View;
  16. import android.view.animation.Animation;
  17. import android.view.animation.Transformation;
  18.  
  19. import java.io.File;
  20. import java.io.RandomAccessFile;
  21. import java.nio.MappedByteBuffer;
  22. import java.nio.channels.FileChannel;
  23.  
  24. /**
  25. * Created by GIGAMOLE on 23.09.2015.
  26. */
  27. public class MaskImageView extends View {
  28.  
  29. private final static float BITMAP_OFFSET = 60f;
  30.  
  31. private float mOffset;
  32. private int mHeight;
  33. private int mWidth;
  34.  
  35. private Matrix mNullMatrix = new Matrix();
  36.  
  37. private Canvas mCircleCanvas;
  38. private Bitmap mCircleBitmap;
  39. private Matrix mCircleMatrix = new Matrix();
  40.  
  41. private Canvas mRectCanvas;
  42. private Bitmap mRectBitmap;
  43. private Matrix mRectMatrix = new Matrix();
  44.  
  45. private Canvas mEraseCanvas;
  46. private Bitmap mEraseBitmap;
  47.  
  48. private Bitmap mBgBitmap;
  49. private Bitmap mSrcBitmap;
  50.  
  51. final Paint mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG) {
  52. {
  53. setDither(true);
  54. setStyle(Style.FILL);
  55. }
  56. };
  57.  
  58. final Paint mBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG) {
  59. {
  60. setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
  61. }
  62. };
  63.  
  64. final Paint mMaskCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG) {
  65. {
  66. setDither(true);
  67. setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
  68. }
  69. };
  70.  
  71. final Paint mMaskRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG) {
  72. {
  73. setDither(true);
  74. }
  75. };
  76.  
  77. final Paint mRectPaint = new Paint() {
  78. {
  79. setDither(true);
  80. setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
  81. }
  82. };
  83.  
  84. public void setBgBitmap(int bg) {
  85. mBgBitmap = drawableToBitmap(getResources().getDrawable(bg));
  86. postInvalidate();
  87. }
  88.  
  89. public void setSrcBitmap(int src) {
  90. mSrcBitmap = drawableToBitmap(getResources().getDrawable(src));
  91. postInvalidate();
  92. }
  93.  
  94. public MaskImageView(Context context) {
  95. this(context, null);
  96. }
  97.  
  98. public MaskImageView(Context context, AttributeSet attrs) {
  99. this(context, attrs, 0);
  100. }
  101.  
  102. public MaskImageView(Context context, AttributeSet attrs, int defStyleAttr) {
  103. super(context, attrs, defStyleAttr);
  104. }
  105.  
  106. @Override
  107. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  108. super.onSizeChanged(w, h, oldw, oldh);
  109.  
  110. mWidth = w;
  111. mHeight = h;
  112.  
  113. mCircleBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  114. mCircleCanvas = new Canvas(mCircleBitmap);
  115.  
  116. mRectBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  117. mRectCanvas = new Canvas(mRectBitmap);
  118.  
  119. mEraseBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  120. mEraseCanvas = new Canvas(mEraseBitmap);
  121.  
  122. startAnimation(new SlideAnimation());
  123. mOffset = mHeight - (mHeight / 3);
  124. }
  125.  
  126. @Override
  127. protected void onDraw(Canvas canvas) {
  128. final int cWidth = canvas.getClipBounds().width();
  129. final int cHeight = canvas.getClipBounds().height();
  130.  
  131. setCenterCrop(mCircleMatrix, mBgBitmap);
  132. mCircleCanvas.drawCircle(cWidth / 2f, cHeight / 2f, cHeight / 2f - 25f, mCirclePaint);
  133. mCircleCanvas.drawBitmap(mBgBitmap, mCircleMatrix, mBitmapPaint);
  134. canvas.drawBitmap(mCircleBitmap, mNullMatrix, null);
  135.  
  136. updateFitStart(mRectMatrix, mSrcBitmap);
  137. mRectCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
  138. mRectCanvas.drawBitmap(mSrcBitmap, mRectMatrix, null);
  139.  
  140. mEraseCanvas.drawRect(
  141. 0f,
  142. cHeight / 2f,
  143. cWidth,
  144. cHeight,
  145. mMaskRectPaint
  146. );
  147. mEraseCanvas.drawCircle(cWidth / 2f, cHeight / 2f, cHeight / 2f - 25f, mMaskCirclePaint);
  148. mRectCanvas.drawBitmap(mEraseBitmap, mNullMatrix, mRectPaint);
  149.  
  150. canvas.drawBitmap(mRectBitmap, mNullMatrix, null);
  151. }
  152.  
  153. private void updateFitStart(Matrix matrix, final Bitmap bitmap) {
  154. float wRatio = (float) (mWidth - 2f * ((float) mWidth / 6f)) / (float) bitmap.getWidth();
  155. // float hRatio = (float) mHeight / (float) bitmap.getHeight();
  156.  
  157. // float scale = wRatio;
  158. // if (hRatio < wRatio)
  159. // scale = hRatio;
  160.  
  161. matrix.setScale(wRatio, wRatio);
  162. matrix.postTranslate((mWidth / 6f), BITMAP_OFFSET + mOffset);
  163. }
  164.  
  165. private void setCenterCrop(Matrix matrix, final Bitmap bitmap) {
  166. float scale;
  167. float dx = 0, dy = 0;
  168.  
  169. if (bitmap.getWidth() * mHeight > mWidth * bitmap.getHeight()) {
  170. scale = (float) mHeight / (float) bitmap.getHeight();
  171. dx = (mWidth - bitmap.getWidth() * scale) * 0.5f;
  172. } else {
  173. scale = (float) mWidth / (float) bitmap.getWidth();
  174. dy = (mHeight - bitmap.getHeight() * scale) * 0.5f;
  175. }
  176.  
  177. matrix.setScale(scale, scale);
  178. matrix.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
  179. }
  180.  
  181. // private void setFitStart(Matrix matrix, final Bitmap bitmap) {
  182. // float wRatio = (float) (mWidth - 2f * ((float) mWidth / 6f)) / (float) bitmap.getWidth();
  183. // float hRatio = (float) mHeight / (float) bitmap.getHeight();
  184. //
  185. // float scale = wRatio;
  186. // if (hRatio < wRatio)
  187. // scale = hRatio;
  188. //
  189. // matrix.setScale(scale, scale);
  190. // matrix.postTranslate((mWidth / 6f), BITMAP_OFFSET);
  191. // }
  192.  
  193. private Bitmap drawableToBitmap(final Drawable drawable) {
  194. if (drawable instanceof BitmapDrawable) {
  195. return convertToMutable(getContext(), ((BitmapDrawable) drawable.mutate()).getBitmap());
  196. }
  197.  
  198. final Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
  199. final Canvas canvas = new Canvas(bitmap);
  200. drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
  201. drawable.draw(canvas);
  202.  
  203. return convertToMutable(getContext(), bitmap);
  204. }
  205.  
  206. @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  207. private Bitmap convertToMutable(final Context context, final Bitmap imgIn) {
  208. final int width = imgIn.getWidth(), height = imgIn.getHeight();
  209. final Bitmap.Config type = imgIn.getConfig();
  210.  
  211. File outputFile = null;
  212. final File outputDir = context.getCacheDir();
  213. try {
  214. outputFile = File.createTempFile(Long.toString(System.currentTimeMillis()), null, outputDir);
  215. outputFile.deleteOnExit();
  216.  
  217. final RandomAccessFile randomAccessFile = new RandomAccessFile(outputFile, "rw");
  218. final FileChannel channel = randomAccessFile.getChannel();
  219. final MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, 0, imgIn.getRowBytes() * height);
  220.  
  221. imgIn.copyPixelsToBuffer(map);
  222. imgIn.recycle();
  223.  
  224. final Bitmap result = Bitmap.createBitmap(width, height, type);
  225.  
  226. map.position(0);
  227. result.copyPixelsFromBuffer(map);
  228.  
  229. channel.close();
  230. randomAccessFile.close();
  231.  
  232. outputFile.delete();
  233. return result;
  234. } catch (final Exception e) {
  235. } finally {
  236. if (outputFile != null)
  237. outputFile.delete();
  238. }
  239. return null;
  240. }
  241.  
  242. private class SlideAnimation extends Animation {
  243. @Override
  244. public void initialize(int width, int height, int parentWidth, int parentHeight) {
  245. super.initialize(width, height, parentWidth, parentHeight);
  246.  
  247. setDuration(1000);
  248. setRepeatCount(INFINITE);
  249. setRepeatMode(REVERSE);
  250. }
  251.  
  252. @Override
  253. protected void applyTransformation(float interpolatedTime, Transformation t) {
  254. mOffset = (mHeight - (mHeight / 3)) - (interpolatedTime * ((mHeight - (mHeight / 3)) - BITMAP_OFFSET));
  255. invalidate();
  256. }
  257. }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement