Jetpack compose gradient image on top of image - gradient

I have the below code in xml layout, which I would like to move to compose and havong a hard time to get it right
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<ImageView
android:id="#+id/image1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/image"
app:layout_constraintDimensionRatio="375:258"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/image_gradient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/gradient"
app:layout_constraintBottom_toBottomOf="#id/image1" />
</androidx.constraintlayout.widget.ConstraintLayout>
I tried in Compose using a Box that didn't work
Box {
Image(painter = painterResource(
id = R.drawable.image1),
contentDescription = null,
)
Image(painter = painterResource(
id = R.drawable.gradient),
contentDescription = null,
contentScale = ContentScale.FillBounds
)
}

Most probably your gradient image has zero size.
You can use Modifier.matchParentSize: it is available in BoxScope. Apply it to the gradient image to make its size equal to the size of the main image.
Box {
Image(painter = painterResource(
id = R.drawable.image1),
contentDescription = null,
)
Image(painter = painterResource(
id = R.drawable.gradient),
contentDescription = null,
contentScale = ContentScale.FillBounds,
modifier = Modifier.matchParentSize()
)
}

I think you should create the gradient in a composy way
Column(Modifier.fillMaxSize()
.background(Color.White)) {
Box(
modifier = Modifier
.fillMaxWidth()
.background(color = Color.White)
.height(200.dp)
) {
Image(
painter = painterResource(
id = R.drawable.ty
),
contentDescription = null,
modifier = Modifier
.fillMaxWidth(),
contentScale = ContentScale.Crop
)
Box(
modifier = Modifier
.background(
brush = Brush.verticalGradient(
colors = listOf(
MaterialTheme.colors.primary.copy(alpha = 0.5f),
MaterialTheme.colors.primaryVariant.copy(alpha = 0.5f)
)
)
)
.fillMaxSize()
)
}
}
}

Related

Compose desktop(JVM) BasicTextField, Korean input duplicating

I'm implementing a simple search input field on Compose desktop.
My code looks as below.
BasicTextField(
modifier = Modifier.align(Alignment.CenterVertically).onPreviewKeyEvent {
if(it.key == Key.Enter && it.type == KeyEventType.KeyDown){
println("enter down: $textFieldState")
true
}else {
false
}
},
value = textFieldState,
onValueChange = { input ->
textFieldState = input
},
textStyle = TextStyle(
fontSize = 14.sp,
textAlign = TextAlign.Start,
fontWeight = FontWeight.Normal,
fontFamily = NotoSans,
color = Color.Black
),
maxLines = 1,
decorationBox = { innerTextField ->
Row(modifier = Modifier.fillMaxWidth()) {
if (textFieldState.isEmpty()) {
Text(
text = "Search with user name.",
fontSize = 14.sp,
color = Color(0xFF909ba9),
textAlign = TextAlign.Start,
fontWeight = FontWeight.Normal,
fontFamily = NotoSans,
modifier = Modifier.fillMaxWidth()
.align(Alignment.CenterVertically),
)
}
}
innerTextField()
}
)
This code will create a textfield which has 1 max lines.
It works without any problem on english inputs.
But when I type in Korean inputs, keys such as space, enter, or even numbers will duplicate the last Korean character. For example, in english, if I type in H, I, !,
it will be HII!.
Is there some locale settings that can be done to the textField?
I found no working solution in here or in the Compose multiplatform git issue page. I found a workaround using SwingPanel and JTextField.
SwingPanel(background = Color(0xFFf5f6f6), modifier = Modifier.fillMaxSize(), factory = {
//Some JTextfield I've obtaines from stackoverflow to show place holder text.
//Can be replaced to JTextField(columnCount:Int)
HintTextField("Enter in name",1).apply {
background = java.awt.Color(0xf5, 0xf6, 0xf6)
border = null
}
}, update = {
//SimpleDocumentListener is an implementation of DocumentListener.
//Which means it can be replaced by it.
it.document.addDocumentListener(object : SimpleDocumentListener{
override fun update(e: DocumentEvent) {
try{
val text = it.text
textFieldState = text
} catch(e : Exception) {
e.printStackTrace()
}
}
})
//I need an enter key to trigger some search logics.
//textFieldState seems to print the value as I intended
it.addKeyListener(object : KeyAdapter(){
override fun keyPressed(e: KeyEvent?) {
if(e?.keyCode == KeyEvent.VK_ENTER){
println("ENTER : $textFieldState")
}
}
})
})
Really hope the compose multiplatform team comes up with a better solution.

Endless/Carousel horizontal(Lazyrow) scrollview

I have made a lazyrow (horizontal) scroll with 10 items. My problem now is that when I scroll to item number 10, it stops. And I want it to go back to item number 1 again like a carousel. Does anyone know this? Here is my code for the 10 items:
//Horizontal Scroll view
item {
LazyRow {
items(10) { count->
Card(
modifier = Modifier
.width(110.dp)
.height(140.dp)
.padding(10.dp, 5.dp, 5.dp, 0.dp)
.clip(RoundedCornerShape(10.dp))
.background(Color.White),
elevation = 5.dp
) {
Column(
modifier = Modifier.padding(5.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Image(
painter = painterResource(id = hi.imageId),
contentDescription = "profile Image",
contentScale = ContentScale.Crop,
modifier = Modifier
.size(60.dp)
.clip(CircleShape)
)
Spacer(modifier = Modifier.padding(5.dp))
Text(
text = hi.talk,
color = Color.Black,
fontWeight = FontWeight.Bold,
fontSize = 16.sp)
}
}
}
}
}
I'm pretty new to kotlin, so it is taking some fair time to learn. I couldn't find anything related to this in Jetpack Compose, and it is hard for me to convert from Java.

render Image not outputting Shiny

output$plot <- renderImage({
outfile <- tempfile(fileext = '.png')
png(outfile, width = 400, height = 300)
venn.diagram(
x = list(
T = T,
I = I
),
main = "Venn Diagram ",
filename =outfile, output=TRUE,
lwd = 2,na = "remove",
fill = c("orange", "blue"),
alpha = c(0.5,0.5),
label.col = "black",
cex=1.5,
fontface = "plain",
cat.col = c("cornflowerblue", "pink"),
cat.cex = 1.5,
cat.fontfamily = "serif",
cat.fontface = "plain",
cat.dist = c(0.05, 0.05),
cat.pos = c(-20, 14),
cat.default.pos = "text",
scaled = FALSE
)
dev.off()
list(src = outfile,
contentType = 'image/png',
width = 400,
height = 300,
alt = "This is alternate text")
}, deleteFile = TRUE)
I was trying plot a venn diagram using this code. But it only displays This is alternate text and not outputting any image on the app, Any Idea ?
Try to create a reactive graph as shown below
output$plot <- renderImage({
vennd <- reactive({venn.diagram(
x = list(
T = T,
I = I
),
main = "Venn Diagram ",
filename =outfile, output=TRUE,
lwd = 2,na = "remove",
fill = c("orange", "blue"),
alpha = c(0.5,0.5),
label.col = "black",
cex=1.5,
fontface = "plain",
cat.col = c("cornflowerblue", "pink"),
cat.cex = 1.5,
cat.fontfamily = "serif",
cat.fontface = "plain",
cat.dist = c(0.05, 0.05),
cat.pos = c(-20, 14),
cat.default.pos = "text",
scaled = FALSE
)
})
outfile <- tempfile(fileext = '.png')
png(outfile, width = 400, height = 300)
vennd()
dev.off()
list(src = outfile,
contentType = 'image/png',
width = 400,
height = 300,
alt = "This is alternate text")
}, deleteFile = TRUE)
output$plot <- renderImage({
vennd <- reactive({venn.diagram(
x = list(
T = T,
I = I
),
main = "",
filename =outfile, output=TRUE,
lwd = 2,na = "remove",imagetype="png",
fill = c("orange", "blue"),
alpha = c(0.5,0.5),
label.col = "black",
cex=1.5,
fontface = "plain",
cat.col = c("cornflowerblue", "pink"),
cat.cex = 1.5,
cat.fontfamily = "serif",
cat.fontface = "plain",
cat.dist = c(0.05, 0.05),
cat.pos = c(-20, 14),
cat.default.pos = "text",
scaled = FALSE
)
})
outfile <- tempfile(fileext = '.png')
png(outfile, width = 500, height = 500,type="cairo")
vennd()
dev.off()
list(src = outfile,
contentType = 'image/png',
width = 500,
height = 500,
alt = "This is alternate text")
}, deleteFile = TRUE)
Need to add imagetype="png" and type="cairo" thank you #YBS

Problem customize the height of column bar

The column bar is too small. And I couldn't adjust their height. The screen shot is attached here: https://prnt.sc/p09hj9.
I have tried all the methods of column series at https://www.amcharts.com/docs/v4/reference/columnseries/.
am4core.ready(function() {
// Create chart instance
var chart = am4core.create("historical_monthly_chart_range", am4charts.XYChart);
// Push data into the charts
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
var series = chart.series.push(new am4charts.LineSeries());
series.name = "Price Range";
valueAxis.title.text = 'Price (S$ psf)';
series.dataFields.dateX = "date";
series.dataFields.openValueY = "min";
series.dataFields.valueY = "max";
series.tooltipText = "{date} \n Maximum: {max} \n Average: {average} \n Minimum: {min} \n Volume: {value}";
// Setting the appearance
series.tooltip.background.cornerRadius = 20;
series.tooltip.background.strokeOpacity = 0;
series.tooltip.pointerOrientation = "vertical";
series.tooltip.label.minWidth = 40;
series.tooltip.label.minHeight = 40;
series.tooltip.label.textAlign = "left";
series.tooltip.label.textValign = "middle";
series.fillOpacity = 0.5;
series.tensionX = 0.8;
series.fill = am4core.color("#697e69");
var series2 = chart.series.push(new am4charts.LineSeries());
series2.name = "Minimum Price";
series2.dataFields.dateX = "date";
series2.dataFields.valueY = "min";
series2.stroke = am4core.color("#697e69");
series2.tensionX = 0.8;
var series_average = chart.series.push(new am4charts.LineSeries());
series_average.name = "Average Price";
series_average.dataFields.valueY = "average";
series_average.dataFields.dateX = "date";
series_average.stroke = am4core.color("#000");
/* Bar chart series */
var barSeries = chart.series.push(new am4charts.ColumnSeries());
barSeries.dataFields.valueY = "value";
barSeries.dataFields.dateX = "date";
barSeries.fill = am4core.color("#000");
barSeries.columns.width = am4core.percent(60);
chart.cursor = new am4charts.XYCursor();
chart.cursor.xAxis = dateAxis;
chart.legend = new am4charts.Legend();
});
There is a bounty on this issue from AM charts however one workaround can be using scrollbar in Y axis like this following:
chart.scrollbarY = new am4core.Scrollbar();
This is not the best solution i agree but you can use it to slightly zoom with the buttons and scale and see
let me know if it works!

How to draw a line between the widgets in Qscrollarea using pyqt4

Here is my code i created multiple widget in qscrollarea. In between the each widget i want to draw a line.Can any one please tell me how can i draw line in between the each widget of the scroll area.
self.mainw = QtGui.QWidget()
self.scrollArea_left.setWidget(self.mainw)
self.ordersvbox = QtGui.QGridLayout(self.mainw)
self.w1 = QtGui.QWidget()
self.v1 = QtGui.QVBoxLayout(spacing=0)
self.v1.setContentsMargins(0, 0, 0, 0)
self.w1.setLayout(self.v1)
self.h1 = QtGui.QHBoxLayout()
self.l1 = QtGui.QLabel("#A")
self.lprogress = QtGui.QLabel("Porgress")
self.lamount = QtGui.QLabel("200 Rs")
self.items_count = QtGui.QLabel("2 items")
self.h1.addWidget(self.l1)
self.h1.addWidget(self.lprogress)
self.h1.addWidget(self.lamount)
self.v1.addLayout(self.h1)
self.v1.addWidget(self.items_count)
self.ordersvbox.addWidget(self.w1,0,0)
self.w2 = QtGui.QWidget()
self.v2 = QtGui.QVBoxLayout(self.w2)
self.h2 = QtGui.QHBoxLayout()
self.l2 = QtGui.QLabel("#B")
self.lprogress2 = QtGui.QLabel("x")
self.lamount2 = QtGui.QLabel("300 RS")
self.items_count2 = QtGui.QLabel(" 2 items")
self.h2.addWidget(self.l2)
self.h2.addWidget(self.lprogress2)
self.h2.addWidget(self.lamount2)
self.v2.addLayout(self.h2)
self.v2.addWidget(self.items_count2)
self.ordersvbox.addWidget(self.w2,1,0)
self.w3 = QtGui.QWidget()
self.v3 = QtGui.QVBoxLayout(self.w3)
self.h3 = QtGui.QHBoxLayout()
self.l3 = QtGui.QLabel("#C")
self.lprogress3 = QtGui.QLabel("z")
self.lamount3 = QtGui.QLabel("200 RS")
self.items_count3 = QtGui.QLabel(" 2 items")
self.h3.addWidget(self.l3)
self.h3.addWidget(self.lprogress3)
self.h3.addWidget(self.lamount3)
self.v3.addLayout(self.h3)
self.v3.addWidget(self.items_count3)
self.ordersvbox.addWidget(self.w3,2,0)