Skip to content

Commit

Permalink
1、完善代码
Browse files Browse the repository at this point in the history
2、修复背景模式下的Bug
  • Loading branch information
FlyJingFish committed Mar 15, 2024
1 parent 50f1c85 commit fa88a21
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,54 +181,59 @@ open class GraphicsDrawable(val view: View) : Drawable() {
mDrawableMatrix.reset()
val widthScale = viewWidth / drawableWidth
val heightScale = viewHeight / drawableHeight
if (scaleType == ScaleType.CENTER) {
mDrawableMatrix.postTranslate(
(viewWidth - drawableWidth) / 2f,
(viewHeight - drawableHeight) / 2f
)
} else if (scaleType == ScaleType.CENTER_CROP) {
val scale = Math.max(widthScale, heightScale)
mDrawableMatrix.postScale(scale, scale)
mDrawableMatrix.postTranslate(
(viewWidth - drawableWidth * scale) / 2f,
(viewHeight - drawableHeight * scale) / 2f
)
} else if (scaleType == ScaleType.CENTER_INSIDE) {
val scale = Math.min(1.0f, Math.min(widthScale, heightScale))
mDrawableMatrix.postScale(scale, scale)
mDrawableMatrix.postTranslate(
(viewWidth - drawableWidth * scale) / 2f,
(viewHeight - drawableHeight * scale) / 2f
)
} else {
mTempSrc[0f, 0f, drawableWidth.toFloat()] = drawableHeight.toFloat()
mTempDst[0f, 0f, viewWidth] = viewHeight
when (scaleType) {
ScaleType.FIT_CENTER -> mDrawableMatrix.setRectToRect(
mTempSrc,
mTempDst,
Matrix.ScaleToFit.CENTER
)

ScaleType.FIT_START -> mDrawableMatrix.setRectToRect(
mTempSrc,
mTempDst,
Matrix.ScaleToFit.START
when (scaleType) {
ScaleType.CENTER -> {
mDrawableMatrix.postTranslate(
(viewWidth - drawableWidth) / 2f,
(viewHeight - drawableHeight) / 2f
)

ScaleType.FIT_END -> mDrawableMatrix.setRectToRect(
mTempSrc,
mTempDst,
Matrix.ScaleToFit.END
}
ScaleType.CENTER_CROP -> {
val scale = max(widthScale, heightScale)
mDrawableMatrix.postScale(scale, scale)
mDrawableMatrix.postTranslate(
(viewWidth - drawableWidth * scale) / 2f,
(viewHeight - drawableHeight * scale) / 2f
)

ScaleType.FIT_XY -> mDrawableMatrix.setRectToRect(
mTempSrc,
mTempDst,
Matrix.ScaleToFit.FILL
}
ScaleType.CENTER_INSIDE -> {
val scale = min(1.0f, min(widthScale, heightScale))
mDrawableMatrix.postScale(scale, scale)
mDrawableMatrix.postTranslate(
(viewWidth - drawableWidth * scale) / 2f,
(viewHeight - drawableHeight * scale) / 2f
)

else -> {}
}
else -> {
mTempSrc[0f, 0f, drawableWidth.toFloat()] = drawableHeight.toFloat()
mTempDst[0f, 0f, viewWidth] = viewHeight
when (scaleType) {
ScaleType.FIT_CENTER -> mDrawableMatrix.setRectToRect(
mTempSrc,
mTempDst,
Matrix.ScaleToFit.CENTER
)

ScaleType.FIT_START -> mDrawableMatrix.setRectToRect(
mTempSrc,
mTempDst,
Matrix.ScaleToFit.START
)

ScaleType.FIT_END -> mDrawableMatrix.setRectToRect(
mTempSrc,
mTempDst,
Matrix.ScaleToFit.END
)

ScaleType.FIT_XY -> mDrawableMatrix.setRectToRect(
mTempSrc,
mTempDst,
Matrix.ScaleToFit.FILL
)

else -> {}
}
}
}
getDrawableRect()
Expand All @@ -252,10 +257,10 @@ open class GraphicsDrawable(val view: View) : Drawable() {
val right: Float
val bottom: Float
if (mBackgroundMode) {
left = mMatrixRectF.left
top = mMatrixRectF.top
right = mMatrixRectF.right
bottom = mMatrixRectF.bottom
left = max(mMatrixRectF.left,0f)
top = max(mMatrixRectF.top,0f)
right = min(mMatrixRectF.right,viewWidth)
bottom = min(mMatrixRectF.bottom,viewHeight)
} else {
val paddingLeft = getViewPaddingLeft().toFloat()
val paddingTop = getViewPaddingTop().toFloat()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.widget.ImageView
import coil.target.ImageViewTarget
import com.flyjingfish.graphicsdrawablelib.GraphicsDrawable

open class CoilGraphicsImageViewTarget(view: ImageView, private val mGraphicsDrawable: GraphicsDrawable) : ImageViewTarget(view) {
open class CoilGraphicsImageViewTarget(private val mGraphicsDrawable: GraphicsDrawable) : ImageViewTarget(mGraphicsDrawable.view as ImageView) {
private val mStartGraphicsDrawable: GraphicsDrawable = mGraphicsDrawable.copy()
private val mFailedGraphicsDrawable: GraphicsDrawable = mGraphicsDrawable.copy()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.flyjingfish.graphicsdrawablecoillib

import android.graphics.drawable.Drawable
import android.widget.ImageView
import coil.target.ImageViewTarget
import com.flyjingfish.graphicsdrawablelib.GraphicsDrawable

open class CoilGraphicsViewBackgroundTarget(view: ImageView, private val graphicsDrawable: GraphicsDrawable) : ViewBackgroundTarget(view) {
open class CoilGraphicsViewBackgroundTarget(graphicsDrawable: GraphicsDrawable) : ViewBackgroundTarget(graphicsDrawable.view) {
private val mGraphicsDrawable: GraphicsDrawable
private val mStartGraphicsDrawable: GraphicsDrawable
private val mFailedGraphicsDrawable: GraphicsDrawable
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
```gradle
dependencies {
//必选项
implementation 'io.github.FlyJingFish:GraphicsDrawable:1.0.1'
implementation 'io.github.FlyJingFish:GraphicsDrawable:1.0.2'
//可选项,如果您使用 Glide 则使用这个
implementation 'io.github.FlyJingFish:GraphicsDrawableGlideLib:1.0.1'
implementation 'io.github.FlyJingFish:GraphicsDrawableGlideLib:1.0.2'
//可选项,如果您使用 Coil 则使用这个
implementation 'io.github.FlyJingFish:GraphicsDrawableCoilLib:1.0.1'
implementation 'io.github.FlyJingFish:GraphicsDrawableCoilLib:1.0.2'
}
```
## 第二步、使用说明
Expand Down Expand Up @@ -135,7 +135,7 @@ val request = ImageRequest.Builder(context)
.data(url)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.target(CoilGraphicsImageViewTarget(view, pic1Drawable))
.target(CoilGraphicsImageViewTarget(pic1Drawable))
.build()

imageLoader.enqueue(request)
Expand All @@ -154,7 +154,7 @@ val request = ImageRequest.Builder(context)
.data(url)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.target(CoilGraphicsViewBackgroundTarget(view, pic4Drawable))
.target(CoilGraphicsViewBackgroundTarget(pic4Drawable))
.build()
imageLoader.enqueue(request)

Expand Down
40 changes: 26 additions & 14 deletions app/src/main/java/com/flyjingfish/graphicsdrawable/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,28 @@ import com.flyjingfish.graphicsdrawablelib.GraphicsDrawable
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var itemData: String
private lateinit var pic1Drawable : GraphicsDrawable
private lateinit var pic2Drawable : GraphicsDrawable
private lateinit var pic3Drawable : GraphicsDrawable
private lateinit var pic4Drawable : GraphicsDrawable
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
pic1Drawable = GraphicsDrawable(binding.iv1)
pic1Drawable.setShapeType(GraphicsDrawable.ShapeType.RECTANGLE)
pic1Drawable.setRadius(MyImageLoader.dp2px(20f).toFloat())

pic2Drawable = GraphicsDrawable(binding.iv2)
pic2Drawable.setShapeType(GraphicsDrawable.ShapeType.RECTANGLE)
pic2Drawable.setRelativeRadius(MyImageLoader.dp2px(10f).toFloat(),MyImageLoader.dp2px(20f).toFloat(),MyImageLoader.dp2px(30f).toFloat(),MyImageLoader.dp2px(40f).toFloat())

pic3Drawable = GraphicsDrawable(binding.iv3)
pic3Drawable.setShapeType(GraphicsDrawable.ShapeType.OVAL)

pic4Drawable = GraphicsDrawable(binding.iv4)
pic4Drawable.setShapeType(GraphicsDrawable.ShapeType.CUSTOM)
pic4Drawable.setCustomDrawable(resources.getDrawable(R.drawable.ic_vector_flower))
binding.rgLoad.setOnCheckedChangeListener { _, checkedId ->
if (checkedId == R.id.rb_glide) {
MyImageLoader.loadType = MyImageLoader.LoaderType.GLIDE
Expand Down Expand Up @@ -74,20 +91,7 @@ class MainActivity : AppCompatActivity() {
binding.iv4.setImageDrawable(null)
}

val pic1Drawable = GraphicsDrawable(binding.iv1)
pic1Drawable.setShapeType(GraphicsDrawable.ShapeType.RECTANGLE)
pic1Drawable.setRadius(MyImageLoader.dp2px(20f).toFloat())

val pic2Drawable = GraphicsDrawable(binding.iv2)
pic2Drawable.setShapeType(GraphicsDrawable.ShapeType.RECTANGLE)
pic2Drawable.setRelativeRadius(MyImageLoader.dp2px(10f).toFloat(),MyImageLoader.dp2px(20f).toFloat(),MyImageLoader.dp2px(30f).toFloat(),MyImageLoader.dp2px(40f).toFloat())

val pic3Drawable = GraphicsDrawable(binding.iv3)
pic3Drawable.setShapeType(GraphicsDrawable.ShapeType.OVAL)

val pic4Drawable = GraphicsDrawable(binding.iv4)
pic4Drawable.setShapeType(GraphicsDrawable.ShapeType.CUSTOM)
pic4Drawable.setCustomDrawable(resources.getDrawable(R.drawable.ic_vector_flower))

// val uri = Uri.parse(
// ContentResolver.SCHEME_ANDROID_RESOURCE
Expand Down Expand Up @@ -175,6 +179,8 @@ class MainActivity : AppCompatActivity() {
(view as RadioButton).isChecked = true
}

private var mScaleType: ScaleType ?= null

private fun setScaleType(scaleType: ScaleType){
binding.iv1.scaleType = scaleType
binding.iv2.scaleType = scaleType
Expand All @@ -187,7 +193,13 @@ class MainActivity : AppCompatActivity() {
binding.tvFitStart.isChecked = false
binding.tvFitEnd.isChecked = false
binding.tvFixXY.isChecked = false

mScaleType = scaleType
if (MyImageLoader.backgroundMode){
pic1Drawable.setScaleType(scaleType)
pic2Drawable.setScaleType(scaleType)
pic3Drawable.setScaleType(scaleType)
pic4Drawable.setScaleType(scaleType)
}
setData()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,15 @@ import android.util.TypedValue
import android.widget.ImageView
import androidx.annotation.DrawableRes
import coil.Coil
import coil.request.ErrorResult
import coil.request.ImageRequest
import coil.request.SuccessResult
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.flyjingfish.graphicsdrawablecoillib.CoilGraphicsImageViewTarget
import com.flyjingfish.graphicsdrawablecoillib.CoilGraphicsViewBackgroundTarget
import com.flyjingfish.graphicsdrawableglidelib.GlideGraphicsImageViewTarget
import com.flyjingfish.graphicsdrawableglidelib.GlideGraphicsViewBackgroundTarget
import com.flyjingfish.graphicsdrawablelib.GraphicsDrawable
import jp.wasabeef.glide.transformations.BlurTransformation

object MyImageLoader {
var loadType: LoaderType = LoaderType.GLIDE
Expand Down Expand Up @@ -61,9 +54,9 @@ object MyImageLoader {

if (useDrawable) {
if (backgroundMode){
requestBuilder.target(CoilGraphicsViewBackgroundTarget(iv, graphicsDrawable))
requestBuilder.target(CoilGraphicsViewBackgroundTarget(graphicsDrawable))
}else{
requestBuilder.target(CoilGraphicsImageViewTarget(iv, graphicsDrawable))
requestBuilder.target(CoilGraphicsImageViewTarget(graphicsDrawable))
}
} else {
requestBuilder.target(iv)
Expand Down
4 changes: 2 additions & 2 deletions version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Fri Mar 15 23:39:02 CST 2024
PROJ_VERSION=1.0.1
#Sat Mar 16 00:32:34 CST 2024
PROJ_VERSION=1.0.2

0 comments on commit fa88a21

Please sign in to comment.