|
16 | 16 |
|
17 | 17 | package com.gigamole.tintlayout.lib; |
18 | 18 |
|
| 19 | +import android.annotation.TargetApi; |
19 | 20 | import android.content.Context; |
20 | 21 | import android.content.res.TypedArray; |
21 | 22 | import android.graphics.Bitmap; |
|
29 | 30 | import android.graphics.Shader; |
30 | 31 | import android.graphics.drawable.BitmapDrawable; |
31 | 32 | import android.graphics.drawable.Drawable; |
| 33 | +import android.os.Build; |
32 | 34 | import android.util.AttributeSet; |
33 | 35 | import android.view.Gravity; |
34 | 36 | import android.view.View; |
35 | 37 | import android.widget.FrameLayout; |
36 | 38 |
|
| 39 | +import java.io.File; |
| 40 | +import java.io.RandomAccessFile; |
| 41 | +import java.nio.MappedByteBuffer; |
| 42 | +import java.nio.channels.FileChannel; |
| 43 | + |
37 | 44 | /** |
38 | 45 | * Created by GIGAMOLE on 21.06.2015. |
39 | 46 | */ |
@@ -251,7 +258,7 @@ private void getTintBitmap() { |
251 | 258 | } |
252 | 259 |
|
253 | 260 | private void getBitmap() { |
254 | | - this.bitmap = Bitmap.createBitmap(drawableToBitmap(getBackground())); |
| 261 | + this.bitmap = Bitmap.createBitmap(convertToMutable(drawableToBitmap(getBackground()))); |
255 | 262 | this.canvas = new Canvas(this.bitmap); |
256 | 263 |
|
257 | 264 | this.isGet = true; |
@@ -294,4 +301,40 @@ private Bitmap drawableToBitmap(final Drawable drawable) { |
294 | 301 |
|
295 | 302 | return bitmap; |
296 | 303 | } |
| 304 | + |
| 305 | + @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) |
| 306 | + private Bitmap convertToMutable(final Bitmap imgIn) { |
| 307 | + final int width = imgIn.getWidth(), height = imgIn.getHeight(); |
| 308 | + final Bitmap.Config type = imgIn.getConfig(); |
| 309 | + |
| 310 | + File outputFile = null; |
| 311 | + final File outputDir = getContext().getCacheDir(); |
| 312 | + try { |
| 313 | + outputFile = File.createTempFile(Long.toString(System.currentTimeMillis()), null, outputDir); |
| 314 | + outputFile.deleteOnExit(); |
| 315 | + |
| 316 | + final RandomAccessFile randomAccessFile = new RandomAccessFile(outputFile, "rw"); |
| 317 | + final FileChannel channel = randomAccessFile.getChannel(); |
| 318 | + final MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, 0, imgIn.getRowBytes() * height); |
| 319 | + |
| 320 | + imgIn.copyPixelsToBuffer(map); |
| 321 | +// imgIn.recycle(); |
| 322 | + |
| 323 | + final Bitmap result = Bitmap.createBitmap(width, height, type); |
| 324 | + |
| 325 | + map.position(0); |
| 326 | + result.copyPixelsFromBuffer(map); |
| 327 | + |
| 328 | + channel.close(); |
| 329 | + randomAccessFile.close(); |
| 330 | + |
| 331 | + outputFile.delete(); |
| 332 | + return result; |
| 333 | + } catch (final Exception e) { |
| 334 | + } finally { |
| 335 | + if (outputFile != null) |
| 336 | + outputFile.delete(); |
| 337 | + } |
| 338 | + return null; |
| 339 | + } |
297 | 340 | } |
0 commit comments