Skip to content

Commit f3d0b4a

Browse files
alex-signalcody-signal
authored andcommitted
Fix incorrect gradient rotation.
1 parent 83b9fba commit f3d0b4a

File tree

3 files changed

+20
-29
lines changed

3 files changed

+20
-29
lines changed

app/src/main/java/org/thoughtcrime/securesms/components/RotatableGradientDrawable.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.graphics.Point;
99
import android.graphics.Rect;
1010
import android.graphics.Shader;
11+
import android.graphics.Xfermode;
1112
import android.graphics.drawable.Drawable;
1213

1314
import androidx.annotation.NonNull;
@@ -100,8 +101,8 @@ public void setBounds(int left, int top, int right, int bottom) {
100101
fillPaint.setShader(new LinearGradient(fillRect.left, fillRect.top, fillRect.right, fillRect.bottom, colors, positions, Shader.TileMode.CLAMP));
101102
}
102103

103-
public @Nullable Shader getShader() {
104-
return fillPaint.getShader();
104+
public void setXfermode(@NonNull Xfermode xfermode) {
105+
fillPaint.setXfermode(xfermode);
105106
}
106107

107108
private static Point cornerPrime(@NonNull Point origin, @NonNull Point corner, float degrees) {

app/src/main/java/org/thoughtcrime/securesms/conversation/colors/ChatColors.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import android.graphics.ColorFilter
55
import android.graphics.Path
66
import android.graphics.PorterDuff
77
import android.graphics.PorterDuffColorFilter
8-
import android.graphics.Shader
98
import android.graphics.drawable.ColorDrawable
109
import android.graphics.drawable.Drawable
1110
import android.graphics.drawable.ShapeDrawable
@@ -32,6 +31,8 @@ class ChatColors private constructor(
3231
private val singleColor: Int?
3332
) {
3433

34+
fun isGradient(): Boolean = Build.VERSION.SDK_INT >= 21 && linearGradient != null
35+
3536
/**
3637
* Returns the Drawable to render the linear gradient, or null if this ChatColors is a single color.
3738
*/
@@ -54,18 +55,6 @@ class ChatColors private constructor(
5455
}
5556
}
5657

57-
fun asShader(left: Int, top: Int, right: Int, bottom: Int): Shader? {
58-
return linearGradient?.let {
59-
RotatableGradientDrawable(
60-
linearGradient.degrees,
61-
linearGradient.colors,
62-
linearGradient.positions
63-
).apply {
64-
setBounds(left, top, right, bottom)
65-
}
66-
}?.shader
67-
}
68-
6958
/**
7059
* Returns the ColorFilter to apply to a conversation bubble or other relevant piece of UI.
7160
*/

app/src/main/java/org/thoughtcrime/securesms/conversation/colors/RecyclerViewColorizer.kt

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.view.View
1010
import android.widget.EdgeEffect
1111
import androidx.recyclerview.widget.LinearLayoutManager
1212
import androidx.recyclerview.widget.RecyclerView
13+
import org.thoughtcrime.securesms.components.RotatableGradientDrawable
1314

1415
/**
1516
* Draws the ChatColors color or gradient following this procedure:
@@ -77,7 +78,6 @@ class RecyclerViewColorizer(private val recyclerView: RecyclerView) {
7778
color = Color.BLACK
7879
}
7980

80-
private val shaderPaint = Paint()
8181
private val colorPaint = Paint()
8282

8383
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
@@ -107,24 +107,25 @@ class RecyclerViewColorizer(private val recyclerView: RecyclerView) {
107107

108108
private fun drawShaderMask(canvas: Canvas, parent: RecyclerView, chatColors: ChatColors) {
109109
if (useLayer) {
110-
shaderPaint.xfermode = layerXfermode
111110
colorPaint.xfermode = layerXfermode
112111
} else {
113-
shaderPaint.xfermode = noLayerXfermode
114112
colorPaint.xfermode = noLayerXfermode
115113
}
116114

117-
val shader = chatColors.asShader(0, 0, parent.width, parent.height)
118-
shaderPaint.shader = shader
119-
colorPaint.color = chatColors.asSingleColor()
120-
121-
canvas.drawRect(
122-
0f,
123-
0f,
124-
parent.width.toFloat(),
125-
parent.height.toFloat(),
126-
if (shader == null) colorPaint else shaderPaint
127-
)
115+
if (chatColors.isGradient()) {
116+
val mask = chatColors.chatBubbleMask as RotatableGradientDrawable
117+
mask.setXfermode(colorPaint.xfermode)
118+
mask.setBounds(0, 0, parent.width, parent.height)
119+
mask.draw(canvas)
120+
} else {
121+
canvas.drawRect(
122+
0f,
123+
0f,
124+
parent.width.toFloat(),
125+
parent.height.toFloat(),
126+
colorPaint
127+
)
128+
}
128129
}
129130
}
130131

0 commit comments

Comments
 (0)