Skip to content

Commit

Permalink
handle image fit item
Browse files Browse the repository at this point in the history
  • Loading branch information
hantrungkien committed Jul 20, 2018
1 parent 3dcb25b commit 7e3fd78
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class AsyncActivity : AppCompatActivity(), BubblePickerListener {
title = titles[position]
gradient = BubbleGradient(colors.getColor((position * 2) % 8, 0),
colors.getColor((position * 2) % 8 + 1, 0), BubbleGradient.VERTICAL)
imgUrl = "http://sohanews.sohacdn.com/2018/4/11/hat9-1523392964439195574255.jpg"
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions bubblepicker/src/main/java/com/kienht/bubblepicker/extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,17 @@ fun Bitmap.toTexture(textureUnit: Int) {
}

fun Int.even() = this % 2 == 0

fun Bitmap.resizeBitmap(newWidth: Int, newHeight: Int): Bitmap {
val width = this.width
val height = this.height
val scaleWidth = newWidth.toFloat() / width
val scaleHeight = newHeight.toFloat() / height

val matrix = android.graphics.Matrix()
matrix.postScale(scaleWidth, scaleHeight)

val resizedBitmap = Bitmap.createBitmap(this, 0, 0, width, height, matrix, false)
this.recycle()
return resizedBitmap
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.support.annotation.ColorInt
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.SurfaceHolder
import android.view.View
import com.kienht.bubblepicker.BubblePickerListener
import com.kienht.bubblepicker.R
Expand All @@ -31,7 +32,7 @@ class BubblePicker(context: Context?, attrs: AttributeSet?) : GLSurfaceView(cont
set(value) {
field = value
renderer.pickerList = value
onResume()
super.onResume()
}

var adapter: BubblePickerAdapter? = null
Expand All @@ -41,7 +42,7 @@ class BubblePicker(context: Context?, attrs: AttributeSet?) : GLSurfaceView(cont
renderer.pickerList = ArrayList((0 until value.totalCount)
.map { value.getItem(it) }.toList())
}
onResume()
super.onResume()
}

var maxSelectedCount: Int? = null
Expand Down Expand Up @@ -153,6 +154,7 @@ class BubblePicker(context: Context?, attrs: AttributeSet?) : GLSurfaceView(cont
override fun onPause() {
if (renderer.pickerList.isNotEmpty()) {
super.onPause()
renderer.clear()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kienht.bubblepicker.rendering

import android.content.Context
import android.content.res.Resources
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.opengl.GLES20.*
Expand All @@ -10,20 +11,23 @@ import android.text.StaticLayout
import android.text.TextPaint
import android.text.TextUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.kienht.bubblepicker.model.BubbleGradient
import com.kienht.bubblepicker.model.PickerItem
import com.kienht.bubblepicker.physics.CircleBody
import com.kienht.bubblepicker.rendering.BubbleShader.U_MATRIX
import com.kienht.bubblepicker.toTexture
import org.jbox2d.common.Vec2
import java.lang.ref.WeakReference
import android.graphics.Bitmap
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.kienht.bubblepicker.resizeBitmap


/**
* Created by irinagalata on 1/19/17.
*/
data class Item(val context: WeakReference<Context>, val pickerItem: PickerItem, val circleBody: CircleBody, val isAlwaysSelected: Boolean) {
data class Item(val context: WeakReference<Context>,
val pickerItem: PickerItem, val circleBody: CircleBody, val isAlwaysSelected: Boolean) {

val x: Float
get() = circleBody.physicalBody.position.x
Expand Down Expand Up @@ -80,12 +84,14 @@ data class Item(val context: WeakReference<Context>, val pickerItem: PickerItem,

private fun createBitmap(isSelected: Boolean): Bitmap {
var bitmap = if (!TextUtils.isEmpty(pickerItem.imgUrl) && pickerItem.isUseImgUrl) {
Glide.with(context.get())
.load("https://avatar-nct.nixcdn.com/playlist/2017/04/09/9/8/9/4/1491713063467_500.jpg")
val bmLoaded = Glide.with(context.get())
.load(pickerItem.imgUrl)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.skipMemoryCache(true)
.into(bitmapSize.toInt() / 2, bitmapSize.toInt() / 2)
.get()

bmLoaded.resizeBitmap(bitmapSize.toInt(), bitmapSize.toInt())
} else {
Bitmap.createBitmap(bitmapSize.toInt(), bitmapSize.toInt(), Bitmap.Config.ARGB_4444)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class PickerRenderer(val glView: View) : GLSurfaceView.Renderer {
}
}

private fun clear() {
fun clear() {
circles.clear()
Engine.clear()
}
Expand Down
Binary file added bubblepicker/src/main/res/drawable/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7e3fd78

Please sign in to comment.