Fix file loading and add animation drawable cast method

This commit is contained in:
oupson 2018-10-08 08:05:44 +02:00
parent b4696c55e8
commit ffec8cc269
3 changed files with 81 additions and 16 deletions

View File

@ -1,6 +1,7 @@
package oupson.apng
import android.graphics.*
import android.graphics.drawable.AnimationDrawable
import android.os.Environment
import android.os.Handler
import android.widget.ImageView
@ -12,7 +13,7 @@ import java.io.IOException
import java.net.URL
class ApngAnimator(val imageView : ImageView) {
class ApngAnimator {
var play = true
var Frames = ArrayList<Frame>()
@ -32,10 +33,19 @@ class ApngAnimator(val imageView : ImageView) {
var isDebug = false
val imageView : ImageView?
init {
myHandler = Handler()
}
constructor() {
imageView = null
}
constructor(imageView : ImageView) {
this.imageView = imageView
}
fun load(file: File) {
// Download PNG
@ -80,8 +90,11 @@ class ApngAnimator(val imageView : ImageView) {
// Add current frame to bitmap buffer
// APNG_DISPOSE_OP_BACKGROUND: the frame's region of the output buffer is to be cleared to fully transparent black before rendering the next frame.
else if (it.dispose_op == Utils.Companion.dispose_op.APNG_DISPOSE_OP_BACKGROUND){
canvas.drawRect(lastFrame!!.x_offsets.toFloat(), lastFrame!!.y_offsets.toFloat(), lastFrame!!.x_offsets + lastFrame!!.width.toFloat(), lastFrame!!.y_offsets + lastFrame!!.height.toFloat(), { val paint = Paint(); paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR); paint }())
bitmapBuffer = btm
val res = Bitmap.createBitmap(Frames[0].maxWidth, Frames[0].maxHeight, Bitmap.Config.ARGB_8888)
val can = Canvas(res)
can.drawBitmap(btm, 0f, 0f, null)
can.drawRect(lastFrame!!.x_offsets.toFloat(), lastFrame!!.y_offsets.toFloat(), lastFrame!!.x_offsets + lastFrame!!.width.toFloat(), lastFrame!!.y_offsets + lastFrame!!.height.toFloat(), { val paint = Paint(); paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR); paint }())
bitmapBuffer = res
}
else {
bitmapBuffer = btm
@ -200,10 +213,7 @@ class ApngAnimator(val imageView : ImageView) {
// Write buffer to canvas
canvas.drawBitmap(bitmapBuffer, 0f, 0f, null)
// APNG_DISPOSE_OP_BACKGROUND: the frame's region of the output buffer is to be cleared to fully transparent black before rendering the next frame.
if (lastFrame!!.dispose_op == Utils.Companion.dispose_op.APNG_DISPOSE_OP_BACKGROUND) {
canvas.drawRect(lastFrame!!.x_offsets.toFloat(), lastFrame!!.y_offsets.toFloat(), lastFrame!!.x_offsets + lastFrame!!.width.toFloat(), lastFrame!!.y_offsets + lastFrame!!.height.toFloat(), { val paint = Paint(); paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR); paint }())
}
// Clear current frame rect
// If `blend_op` is APNG_BLEND_OP_SOURCE all color components of the frame, including alpha, overwrite the current contents of the frame's output buffer region.
@ -220,6 +230,14 @@ class ApngAnimator(val imageView : ImageView) {
//Do nothings
}
// Add current frame to bitmap buffer
// APNG_DISPOSE_OP_BACKGROUND: the frame's region of the output buffer is to be cleared to fully transparent black before rendering the next frame.
else if (it.dispose_op == Utils.Companion.dispose_op.APNG_DISPOSE_OP_BACKGROUND){
val res = Bitmap.createBitmap(Frames[0].maxWidth, Frames[0].maxHeight, Bitmap.Config.ARGB_8888)
val can = Canvas(res)
can.drawBitmap(btm, 0f, 0f, null)
can.drawRect(lastFrame!!.x_offsets.toFloat(), lastFrame!!.y_offsets.toFloat(), lastFrame!!.x_offsets + lastFrame!!.width.toFloat(), lastFrame!!.y_offsets + lastFrame!!.height.toFloat(), { val paint = Paint(); paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR); paint }())
bitmapBuffer = res
}
else {
bitmapBuffer = btm
}
@ -250,15 +268,17 @@ class ApngAnimator(val imageView : ImageView) {
}
fun nextFrame() {
if (counter == Frames.size) {
counter = 0
if (imageView != null) {
if (counter == Frames.size) {
counter = 0
}
val delay = Frames[counter].delay
imageView.setImageBitmap(generatedFrame[counter])
counter++
myHandler.postDelayed({
ifmustPlay()
}, delay.toLong() * speed)
}
val delay = Frames[counter].delay
imageView.setImageBitmap(generatedFrame[counter])
counter++
myHandler.postDelayed({
ifmustPlay()
}, delay.toLong() * speed)
}
fun pause() {
@ -278,6 +298,14 @@ class ApngAnimator(val imageView : ImageView) {
}
}
fun toAnimationDrawable() : AnimationDrawable {
val animDrawable = AnimationDrawable()
for (i in 0 until generatedFrame.size) {
animDrawable.addFrame(BitmapDrawable(generatedFrame[i]), Frames[i].delay.toInt())
}
return animDrawable
}
fun setFrameSpeed(speed : Int) {
this.speed = speed
}

View File

@ -0,0 +1,38 @@
package oupson.apng
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.ColorFilter
import android.graphics.PixelFormat
import android.graphics.drawable.Drawable
internal class BitmapDrawable(val bitmap: Bitmap) : Drawable() {
override fun draw(canvas: Canvas) {
canvas.drawBitmap(bitmap, 0.0f, 0.0f, null)
}
override fun getOpacity(): Int {
return PixelFormat.TRANSLUCENT
}
override fun setAlpha(alpha: Int) {}
override fun setColorFilter(cf: ColorFilter?) {}
override fun getIntrinsicWidth(): Int {
return bitmap.width
}
override fun getIntrinsicHeight(): Int {
return bitmap.height
}
override fun getMinimumWidth(): Int {
return bitmap.width
}
override fun getMinimumHeight(): Int {
return bitmap.height
}
}

View File

@ -25,7 +25,6 @@ class Main2Activity : AppCompatActivity() {
animator.load(uri.path)
}
Log.e("TAG", intent.data.toString())
animator.load(getImageRealPath(contentResolver, uri, null))
}