Skip to content

Commit

Permalink
WIP file caption - custom UI
Browse files Browse the repository at this point in the history
Signed-off-by: rapterjet2004 <[email protected]>
  • Loading branch information
rapterjet2004 committed Oct 30, 2023
1 parent e655b78 commit 5100228
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions app/src/main/java/com/nextcloud/talk/ui/ImageStackContainer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.example.custom_component_experiments.custom_views

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.graphics.Rect
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.core.graphics.scale
import androidx.core.graphics.toRectF

class ImageStackContainer(context: Context, attrs: AttributeSet) : View(context, attrs) {

// TODO eventually, I want this to be fed a list of files to be displayed, so that
// it dynamically draws 1 - 3 images. Any more can be ignored.
// private val image1 = ContextCompat.getDrawable(context, R.drawable.artwork_3)!!.toBitmap()
private var image1Bounds: Rect = Rect()
private var image1Path = Path()
private var image1Paint = Paint(Paint.ANTI_ALIAS_FLAG)
// private val image2 = ContextCompat.getDrawable(context, R.drawable.artwork_2)!!.toBitmap()
private var image2Bounds: Rect = Rect()
private var image2Path = Path()
private var image2Paint = Paint(Paint.ANTI_ALIAS_FLAG)
// private val image3 = ContextCompat.getDrawable(context, R.drawable.artwork_1)!!.toBitmap()
private var image3Bounds: Rect = Rect()
private var image3Path = Path()
private var image3Paint = Paint(Paint.ANTI_ALIAS_FLAG)
private val corners = floatArrayOf(
RADIUS, RADIUS, // Top left radius in px
RADIUS, RADIUS, // Top right radius in px
RADIUS, RADIUS, // Top left radius in px
RADIUS, RADIUS // Bottom left radius in px
)
private var clippingRect: Rect? = null

init {
Log.d(TAG, "ImageStackContainer Initialized")
image1Paint.apply {
color = 0xff424242.toInt()
}

image2Paint.apply {
color = 0xff424242.toInt()
}

image3Paint.apply {
color = 0xff424242.toInt()
}

}

// override fun onDraw(canvas: Canvas?) {
// super.onDraw(canvas)
// canvas?.apply {
// clippingRect = clipBounds
// val left = (clipBounds.right - WIDTH)/2
// val top = (clipBounds.bottom - HEIGHT)/2
// val right = (clipBounds.right - left)
// val bottom = (clipBounds.bottom - top)
// save()
// image1Bounds.apply {
// set(left, top, right, bottom)
// offset(15, -30)
// }
// val bitmap = getCroppedBitmap(image1, image1Bounds, image1Paint, image1Path)!!
// drawBitmap(bitmap, image1Bounds, image1Bounds, image1Paint)
// restore()
// save()
// image2Bounds.apply {
// set(left, top, right, bottom)
// offset(-15, 30)
// }
// val bitmap2 = getCroppedBitmap(image2, image2Bounds, image2Paint, image2Path)!!
// drawBitmap(bitmap2, image2Bounds, image2Bounds, image2Paint)
// restore()
// image3Bounds.apply {
// set(left, top, right, bottom)
// }
// val bitmap3 = getCroppedBitmap(image3, image3Bounds, image3Paint, image3Path)!!
// drawBitmap(bitmap3, image3Bounds, image3Bounds, image3Paint)
// }
// }

private fun getCroppedBitmap(bitmap: Bitmap, bounds: Rect, paint: Paint, path: Path): Bitmap? {
val scaleW = (clippingRect!!.right/100)
val scaleH = (clippingRect!!.bottom/100)
val original = bitmap.scale(bounds.width()*scaleW, bounds.height()*scaleH)
val output = Bitmap.createBitmap(
original.width,
original.height, Bitmap.Config.ARGB_8888
)
val canvas = Canvas(output)
val color = -0xbdbdbe // preview color if no image
paint.apply {
this.color = color
}
path.apply {
addRoundRect(bounds.toRectF(), corners, Path.Direction.CCW)
}
canvas.drawPath(path, paint)
paint.setXfermode(PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP))
canvas.drawBitmap(original, bounds, bounds, paint)
return output
}

companion object {
val TAG: String? = ImageStackContainer::class.java.simpleName
private const val WIDTH = 100
private const val HEIGHT = 150
private const val RADIUS = 25f
}
}

0 comments on commit 5100228

Please sign in to comment.