by Guest » 15 Jan 2025, 17:08
Der folgende Code nimmt eine Liniendiagrammansicht, wandelt sie in ein PNG-Bild um und speichert es auf dem Telefon. Ich möchte jedoch die vertikale oder obere Position des Liniendiagramms auf der Leinwand anpassen. Egal was ich versuche, die Bilddatei wird nicht erstellt. Ich kann den Text jedoch problemlos verschieben. Kann mir bitte jemand helfen?
Code: Select all
val thelinechart = findViewById(R.id.lineChart) as LineChart
thelinechart.setOnClickListener {
thelinechart.viewTreeObserver.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
override fun onGlobalLayout() {
thelinechart.viewTreeObserver.removeOnGlobalLayoutListener(this)
val b = Bitmap.createBitmap(
thelinechart.measuredWidth,
thelinechart.measuredHeight+57,
Bitmap.Config.RGB_565
)
val c = Canvas(b)
val backgroundColor = Color.argb(255, 200, 220, 255)
c.drawColor(backgroundColor)
val paint = Paint()
paint.color = Color.BLACK
paint.textSize = 19f
paint.setTextAlign(Paint.Align.CENTER)
val typeface = Typeface.DEFAULT_BOLD
paint.typeface = typeface
val current = Date()
c.drawText("My Chart - $current",
(c.width/2).toFloat(), 25f, paint)
thelinechart.layout(thelinechart.left, thelinechart.top, thelinechart.right, thelinechart.bottom)
thelinechart.draw(c)
val filename = "thegraph.png"
val sd: String = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).toString()
val dest = File(sd, filename)
try {
val out = FileOutputStream(dest)
b.compress(Bitmap.CompressFormat.PNG, 90, out)
out.flush()
out.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
})
}
BEARBEITEN: Hier ist das R.id.lineChart
Der folgende Code nimmt eine Liniendiagrammansicht, wandelt sie in ein PNG-Bild um und speichert es auf dem Telefon. Ich möchte jedoch die vertikale oder obere Position des Liniendiagramms auf der Leinwand anpassen. Egal was ich versuche, die Bilddatei wird nicht erstellt. Ich kann den Text jedoch problemlos verschieben. Kann mir bitte jemand helfen?
[code] val thelinechart = findViewById(R.id.lineChart) as LineChart
thelinechart.setOnClickListener {
thelinechart.viewTreeObserver.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
override fun onGlobalLayout() {
thelinechart.viewTreeObserver.removeOnGlobalLayoutListener(this)
val b = Bitmap.createBitmap(
thelinechart.measuredWidth,
thelinechart.measuredHeight+57,
Bitmap.Config.RGB_565
)
val c = Canvas(b)
val backgroundColor = Color.argb(255, 200, 220, 255)
c.drawColor(backgroundColor)
val paint = Paint()
paint.color = Color.BLACK
paint.textSize = 19f
paint.setTextAlign(Paint.Align.CENTER)
val typeface = Typeface.DEFAULT_BOLD
paint.typeface = typeface
val current = Date()
c.drawText("My Chart - $current",
(c.width/2).toFloat(), 25f, paint)
thelinechart.layout(thelinechart.left, thelinechart.top, thelinechart.right, thelinechart.bottom)
thelinechart.draw(c)
val filename = "thegraph.png"
val sd: String = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).toString()
val dest = File(sd, filename)
try {
val out = FileOutputStream(dest)
b.compress(Bitmap.CompressFormat.PNG, 90, out)
out.flush()
out.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
})
}
[/code]
[img]https://i.sstatic.net/pzcD4IAf.png[/img]
[b]BEARBEITEN:[/b] Hier ist das R.id.lineChart
[code]
[/code]