-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wangshouheng
committed
Dec 18, 2021
1 parent
f177614
commit 0e503ff
Showing
9 changed files
with
520 additions
and
46 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...uheng/sample/utils/AlwaysHalfAlgorithm.kt → ...heng/sample/custom/AlwaysHalfAlgorithm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
sample/app/src/main/java/me/shouheng/sample/custom/AssetsResourceSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package me.shouheng.sample.custom | ||
|
||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import me.shouheng.compress.strategy.IImageSource | ||
import me.shouheng.compress.strategy.Size | ||
import me.shouheng.compress.utils.copy | ||
import me.shouheng.compress.utils.pixelSize | ||
import me.shouheng.utils.app.ResUtils | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
|
||
data class AssetsResource(val name: String) | ||
|
||
/** | ||
* Image source for [AssetsResource]. | ||
* | ||
* @Author wangshouheng | ||
* @Time 2021/12/18 | ||
*/ | ||
class AssetsResourceSource(private val res: AssetsResource): IImageSource<AssetsResource> { | ||
|
||
override fun getSize(): Size { | ||
val options = BitmapFactory.Options() | ||
options.inJustDecodeBounds = true | ||
options.inSampleSize = 1 | ||
val ins = ResUtils.getAssets().open(res.name) | ||
BitmapFactory.decodeStream(ins, null, options) | ||
return Size(options.outWidth, options.outHeight) | ||
} | ||
|
||
override fun getOriginBitmapByOptions( | ||
options: BitmapFactory.Options | ||
): Bitmap? { | ||
val ins = ResUtils.getAssets().open(res.name) | ||
return BitmapFactory.decodeStream(ins, null, options) | ||
} | ||
|
||
override fun shouldIgnoreForSize(ignoreSize: Int): Boolean { | ||
val ins = ResUtils.getAssets().open(res.name) | ||
val options = BitmapFactory.Options() | ||
options.inJustDecodeBounds = true | ||
options.inSampleSize = 1 | ||
BitmapFactory.decodeStream(ins, null, options) | ||
val size = options.outWidth*options.outHeight*options.pixelSize() | ||
return size < ignoreSize shl 10 | ||
} | ||
|
||
override fun getRotation(): Int = 0 | ||
|
||
override fun copyTo( | ||
dest: File, | ||
format: Bitmap.CompressFormat, | ||
quality: Int | ||
): Boolean { | ||
val ins = ResUtils.getAssets().open(res.name) | ||
return copy(ins, FileOutputStream(dest)) | ||
} | ||
|
||
class Adapter: IImageSource.Adapter<AssetsResource> { | ||
override fun get( | ||
context: Context, source: AssetsResource | ||
): IImageSource<AssetsResource> = AssetsResourceSource(source) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.