Skip to content

Commit e9a5fdd

Browse files
committed
Update 1.0.2
Added immutable bitmap support.
1 parent 5c4d859 commit e9a5fdd

5 files changed

Lines changed: 49 additions & 6 deletions

File tree

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TintLayout.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<content url="file://$MODULE_DIR$">
1313
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
1414
</content>
15-
<orderEntry type="jdk" jdkName="1.7" jdkType="JavaSDK" />
15+
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
1616
<orderEntry type="sourceFolder" forTests="false" />
1717
</component>
1818
</module>

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22
apply plugin: 'com.github.dcendents.android-maven'
33
apply plugin: "com.jfrog.bintray"
44

5-
version = "1.0.1"
5+
version = "1.0.2"
66

77
android {
88
compileSdkVersion 21
@@ -12,7 +12,7 @@ android {
1212
minSdkVersion 11
1313
targetSdkVersion 21
1414
versionCode 1
15-
versionName "1.0.1"
15+
versionName "1.0.2"
1616
}
1717
buildTypes {
1818
release {

library/library.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.github.gigamole.tintlayout" external.system.module.version="1.0.1" type="JAVA_MODULE" version="4">
2+
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.github.gigamole.tintlayout" external.system.module.version="1.0.2" type="JAVA_MODULE" version="4">
33
<component name="FacetManager">
44
<facet type="android-gradle" name="Android-Gradle">
55
<configuration>

library/src/main/java/com/gigamole/tintlayout/lib/TintLayout.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.gigamole.tintlayout.lib;
1818

19+
import android.annotation.TargetApi;
1920
import android.content.Context;
2021
import android.content.res.TypedArray;
2122
import android.graphics.Bitmap;
@@ -29,11 +30,17 @@
2930
import android.graphics.Shader;
3031
import android.graphics.drawable.BitmapDrawable;
3132
import android.graphics.drawable.Drawable;
33+
import android.os.Build;
3234
import android.util.AttributeSet;
3335
import android.view.Gravity;
3436
import android.view.View;
3537
import android.widget.FrameLayout;
3638

39+
import java.io.File;
40+
import java.io.RandomAccessFile;
41+
import java.nio.MappedByteBuffer;
42+
import java.nio.channels.FileChannel;
43+
3744
/**
3845
* Created by GIGAMOLE on 21.06.2015.
3946
*/
@@ -251,7 +258,7 @@ private void getTintBitmap() {
251258
}
252259

253260
private void getBitmap() {
254-
this.bitmap = Bitmap.createBitmap(drawableToBitmap(getBackground()));
261+
this.bitmap = Bitmap.createBitmap(convertToMutable(drawableToBitmap(getBackground())));
255262
this.canvas = new Canvas(this.bitmap);
256263

257264
this.isGet = true;
@@ -294,4 +301,40 @@ private Bitmap drawableToBitmap(final Drawable drawable) {
294301

295302
return bitmap;
296303
}
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+
}
297340
}

0 commit comments

Comments
 (0)