Skip to content

Commit bce4d8f

Browse files
committed
Fixed image stream
1 parent f6317e9 commit bce4d8f

File tree

4 files changed

+75
-54
lines changed

4 files changed

+75
-54
lines changed

app/src/main/java/omega_r/com/omegatypesexample/MainActivity.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package omega_r.com.omegatypesexample
22

3+
import android.graphics.Bitmap
4+
import android.graphics.BitmapFactory
35
import android.os.Bundle
46
import android.widget.ImageView
57
import android.widget.TextView
68
import com.omega_r.libs.omegatypes.Image
79
import com.omega_r.libs.omegatypes.Text
810
import com.omega_r.libs.omegatypes.applyTo
911
import com.omega_r.libs.omegatypes.picasso.from
12+
import com.omega_r.libs.omegatypes.setBackground
13+
import kotlinx.android.synthetic.main.activity_main.*
14+
import kotlin.concurrent.thread
1015

1116
class MainActivity : BaseActivity() {
1217

@@ -19,7 +24,16 @@ class MainActivity : BaseActivity() {
1924
val text = Text.from(R.string.hello_world)
2025
text.applyTo(exampleTextView) // or exampleTextView.setText(text)
2126
val image = Image.from("https://avatars1.githubusercontent.com/u/28600571")
22-
image.applyTo(imageView) // or imageView.setImage(image)
27+
28+
thread {
29+
val stream = image.getStream(this, Bitmap.CompressFormat.PNG)
30+
val bitmap = BitmapFactory.decodeStream(stream)
31+
runOnUiThread {
32+
imageView.setImageBitmap(bitmap)
33+
}
34+
35+
}
36+
2337
}
2438

2539
}

omegatypes/src/main/java/com/omega_r/libs/omegatypes/Image.kt

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@ import android.graphics.Canvas
77
import android.graphics.Rect
88
import android.graphics.drawable.BitmapDrawable
99
import android.graphics.drawable.Drawable
10-
import android.os.Build
1110
import android.support.annotation.DrawableRes
1211
import android.support.annotation.Px
13-
import android.support.annotation.RequiresApi
14-
import android.support.v4.content.ContextCompat
1512
import android.support.v4.view.ViewCompat
16-
import android.system.Os.read
1713
import android.view.View
1814
import android.widget.ImageView
1915
import java.io.*
@@ -29,8 +25,9 @@ open class Image : Serializable {
2925
}
3026

3127
@Throws(IOException::class)
32-
open fun getStream(context: Context): InputStream {
33-
return object:InputStream() {
28+
open fun getStream(context: Context,
29+
compressFormat: Bitmap.CompressFormat = Bitmap.CompressFormat.JPEG, quality: Int = 100): InputStream {
30+
return object : InputStream() {
3431
override fun read() = -1
3532
}
3633
}
@@ -60,9 +57,9 @@ open class Image : Serializable {
6057
view.setBackgroundResource(resId)
6158
}
6259

63-
override fun getStream(context: Context): InputStream {
60+
override fun getStream(context: Context, compressFormat: Bitmap.CompressFormat, quality: Int): InputStream {
6461
return BitmapFactory.decodeResource(context.resources, resId)
65-
.toInputStream()
62+
.toInputStream(compressFormat, quality)
6663
}
6764

6865
}
@@ -77,8 +74,8 @@ open class Image : Serializable {
7774
ViewCompat.setBackground(view, drawable)
7875
}
7976

80-
override fun getStream(context: Context): InputStream {
81-
return drawable.toBitmap().toInputStream()
77+
override fun getStream(context: Context, compressFormat: Bitmap.CompressFormat, quality: Int): InputStream {
78+
return drawable.toBitmap().toInputStream(compressFormat, quality)
8279
}
8380

8481
}
@@ -93,16 +90,16 @@ open class Image : Serializable {
9390
ViewCompat.setBackground(view, BitmapDrawable(view.resources, bitmap))
9491
}
9592

96-
override fun getStream(context: Context): InputStream {
97-
return bitmap.toInputStream()
93+
override fun getStream(context: Context, compressFormat: Bitmap.CompressFormat, quality: Int): InputStream {
94+
return bitmap.toInputStream(compressFormat, quality)
9895
}
9996
}
10097

10198
}
10299

103-
fun Bitmap.toInputStream(): InputStream {
100+
fun Bitmap.toInputStream(compressFormat: Bitmap.CompressFormat, quality: Int ): InputStream {
104101
val stream = ByteArrayOutputStream()
105-
compress(Bitmap.CompressFormat.JPEG, 100, stream)
102+
compress(compressFormat, quality, stream)
106103
val byteArray = stream.toByteArray()
107104
return ByteArrayInputStream(byteArray)
108105
}

picasso/src/main/java/com/omega_r/libs/omegatypes/picasso/ImageExtension.kt

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import android.content.Context
44
import android.graphics.Bitmap
55
import android.graphics.drawable.BitmapDrawable
66
import android.graphics.drawable.Drawable
7+
import android.os.Handler
8+
import android.os.Looper
79
import android.support.v4.view.ViewCompat
810
import android.view.View
911
import android.widget.ImageView
@@ -32,59 +34,64 @@ class PicassoImage(private val url: String) : Image() {
3234

3335
override fun applyBackground(view: View) {
3436
ViewCompat.setBackground(view, null)
35-
Picasso.get()
36-
.load(url)
37-
.resize(view.width, view.height)
38-
.into(object : Target {
37+
val requestCreator = Picasso.get().load(url)
38+
39+
if (view.width > 0 && view.height > 0) {
40+
requestCreator.resize(view.width, view.height)
41+
}
42+
43+
requestCreator.into(object : Target {
3944

40-
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
41-
ViewCompat.setBackground(view, placeHolderDrawable)
42-
}
45+
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
46+
ViewCompat.setBackground(view, placeHolderDrawable)
47+
}
4348

44-
override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
45-
ViewCompat.setBackground(view, errorDrawable)
46-
}
49+
override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
50+
ViewCompat.setBackground(view, errorDrawable)
51+
}
4752

48-
override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
49-
ViewCompat.setBackground(view, BitmapDrawable(view.resources, bitmap))
50-
}
53+
override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
54+
ViewCompat.setBackground(view, BitmapDrawable(view.resources, bitmap))
55+
}
5156

52-
})
57+
})
5358
}
5459

55-
override fun getStream(context: Context): InputStream {
60+
override fun getStream(context: Context, compressFormat: Bitmap.CompressFormat, quality: Int): InputStream {
5661
val stream = WrapperInputStream()
5762

58-
Picasso.get()
59-
.load(url)
60-
.into(object : Target {
6163

62-
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
63-
// stream can only send data once
64-
}
64+
val runnable = Runnable {
65+
Picasso.get()
66+
.load(url)
67+
.into(object : Target {
6568

66-
override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
67-
if (errorDrawable != null) {
68-
stream.inputStream = errorDrawable.toBitmap().toInputStream()
69-
} else {
70-
stream.inputStream = null
69+
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
70+
// stream can only send data once
7171
}
7272

73-
}
74-
75-
override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
76-
if (bitmap != null) {
77-
stream.inputStream = bitmap.toInputStream()
78-
} else {
73+
override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
7974
stream.inputStream = null
8075
}
81-
}
8276

83-
})
77+
override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
78+
if (bitmap != null) {
79+
stream.inputStream = bitmap.toInputStream(compressFormat, quality)
80+
} else {
81+
stream.inputStream = null
82+
}
83+
}
8484

85-
return stream
86-
}
85+
})
86+
}
8787

88+
if (Looper.myLooper() != Looper.getMainLooper()) {
89+
Handler(Looper.getMainLooper()).post(runnable)
90+
} else {
91+
runnable.run()
92+
}
8893

94+
return stream
95+
}
8996

9097
}

picasso/src/main/java/com/omega_r/libs/omegatypes/picasso/WrapperInputStream.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import java.io.InputStream
77
*/
88
internal class WrapperInputStream : InputStream() {
99

10-
1110
internal var inputStream: InputStream? = null
1211
set(value) {
1312
field = value
1413
availableInitValue = 0
15-
o.notifyAll()
14+
synchronized(o){
15+
o.notify()
16+
}
1617
}
1718

1819
private val o = Object()
@@ -21,7 +22,9 @@ internal class WrapperInputStream : InputStream() {
2122

2223
override fun read(): Int {
2324
if (inputStream == null) {
24-
o.wait()
25+
synchronized(o) {
26+
o.wait()
27+
}
2528
}
2629
val inputStream = inputStream ?: return -1
2730
return inputStream.read()

0 commit comments

Comments
 (0)