Skip to main content

Posts

Showing posts with the label Kotlin Good

android kotlin - Coroutines Room ViewModel LiveData

MainActivity.kt package com.example.coroutine import android.os.Bundle import android.text.method.ScrollingMovementMethod import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider import kotlinx.android.synthetic.main.activity_main.* import java.util.UUID import kotlin.random.Random class MainActivity : AppCompatActivity() { private lateinit var model: StudentViewModel override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // make text view text scrollable textView.movementMethod = ScrollingMovementMethod() // initialize the student view model model = ViewModelProvider(this).get(StudentViewModel::class.java) // observe the students live data model.students.observe(this, Observer { students-> textView.text = "Students(${students.size})..." ...

android kotlin - Paint linear gradient

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.graphics.* import android.os.Bundle import android.widget.* class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val imageView = findViewById<ImageView>(R.id.imageView) // show drawing on image view imageView.setImageBitmap( drawLinearGradient() ) } } // function to draw linear gradient on canvas fun drawLinearGradient():Bitmap?{ val bitmap = Bitmap.createBitmap( 1500, 850, Bitmap.Config.ARGB_8888 ) // canvas for drawing val canvas = Canvas(bitmap).apply { drawColor(Color.parseColor("#FEFEFA")) } // initialize a rectF instance val margin = 50F val rectF = RectF( ...

android kotlin - Canvas draw path

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.graphics.* import android.os.Bundle import android.widget.ImageView class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val imageView = findViewById<ImageView>(R.id.imageView) // show path drawing on image view imageView.setImageBitmap(drawPath()) } } // function to draw path on canvas fun drawPath():Bitmap?{ val bitmap = Bitmap.createBitmap( 1500, 850, Bitmap.Config.ARGB_8888 ) // canvas to draw path val canvas = Canvas(bitmap).apply { drawColor(Color.parseColor("#A2A2D0")) } // paint to draw path fill color val paintFill = Paint().apply { isAntiAlias = true color = Col...

android kotlin - Canvas draw text rotate

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.graphics.* import android.os.Bundle import android.text.TextPaint import android.widget.ImageView class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val imageView = findViewById<ImageView>(R.id.imageView) // draw rotated text on canvas imageView.setImageBitmap(drawTextRotate()) } } // function to draw text rotate on canvas fun drawTextRotate():Bitmap?{ val bitmap = Bitmap.createBitmap( 1500, 850, Bitmap.Config.ARGB_8888 ) // canvas to draw text val canvas = Canvas(bitmap).apply { drawColor(Color.parseColor("#A2A2D0")) } // text paint to draw text val paint = TextPaint().apply { ...

android kotlin - Canvas draw text in rectangle

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.graphics.* import android.os.Bundle import android.text.TextPaint import android.widget.ImageView class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val imageView = findViewById<ImageView>(R.id.imageView) // draw text on canvas inside rectangle and get bitmap val bitmap = drawTextInsideRectangle( textToDraw = "Text to draw inside rectangle", textSize = 185F, textColor = Color.parseColor("#F0FFFF"), rectangleColor = Color.parseColor("#333399"), canvasColor = Color.parseColor("#A2A2D0") ) // show drawing on image view imageView.setImageBitmap(...

android kotlin - Canvas draw text inside circle

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.graphics.* import android.os.Bundle import android.text.TextPaint import android.widget.ImageView class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val imageView = findViewById<ImageView>(R.id.imageView) // draw text on canvas inside circle and get bitmap val bitmap = drawTextInCircle( textToDraw = "Text to draw inside circle", textSize = 90F, radius = 300F, textColor = Color.parseColor("#F0FFFF"), circleColor = Color.parseColor("#333399"), canvasColor = Color.parseColor("#A2A2D0") ) // show drawing on image view imageView.setIm...

android kotlin - Canvas draw circle

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.graphics.* import android.os.Bundle import android.widget.ImageView import android.widget.TextView import kotlin.math.min class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val imageView = findViewById<ImageView>(R.id.imageView) val textView = findViewById<TextView>(R.id.textView) // draw circle on canvas and get bitmap val bitmap = drawCircle( color = Color.parseColor("#333399"), radius = 300f, canvasBackground = Color.parseColor("#A2A2D0") ) // show drawing on image view imageView.setImageBitmap(bitmap) // about canvas drawing textView.text = "bi...

android kotlin - Bitmap crop rectangle

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.content.Context import android.graphics.* import android.os.Bundle import android.widget.* import java.io.IOException class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val imageView = findViewById<ImageView>(R.id.imageView) val imageView2 = findViewById<ImageView>(R.id.imageView2) val textView = findViewById<TextView>(R.id.textView) val textView2 = findViewById<TextView>(R.id.textView2) // get the bitmap from assets folder val bitmap = assetsToBitmap("rose.jpg") bitmap?.apply { // show original bitmap in first image view imageView.setImageBitmap(this) textView.text = ...

android kotlin - Bitmap crop square

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.content.Context import android.graphics.* import android.os.Bundle import android.widget.* import java.io.IOException import kotlin.math.min class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val imageView = findViewById<ImageView>(R.id.imageView) val imageView2 = findViewById<ImageView>(R.id.imageView2) val textView = findViewById<TextView>(R.id.textView) val textView2 = findViewById<TextView>(R.id.textView2) // get the bitmap from assets folder val bitmap = assetsToBitmap("flower103.jpg") bitmap?.apply { // show original bitmap in first image view imageView.setImageBitmap(this) ...

android kotlin - Bitmap add padding

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.content.Context import android.graphics.* import android.os.Bundle import android.widget.* import java.io.IOException class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val imageView = findViewById<ImageView>(R.id.imageView) val imageView2 = findViewById<ImageView>(R.id.imageView2) val textView2 = findViewById<TextView>(R.id.textView2) // get the bitmap from assets folder val bitmap = assetsToBitmap("flower103.jpg") bitmap?.apply { // show original bitmap in first image view imageView.setImageBitmap(this) // add padding to bitmap // and keep bitmap transparency ...

android kotlin - Draw rectangle on bitmap

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.content.Context import android.graphics.* import android.os.Bundle import android.widget.* import java.io.IOException class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val imageView = findViewById<ImageView>(R.id.imageView) val imageView2 = findViewById<ImageView>(R.id.imageView2) // get the bitmap from assets folder val bitmap = assetsToBitmap("flower2.jpg") bitmap?.apply { // show original bitmap in first image view imageView.setImageBitmap(this) // draw rectangle on bitmap // show bitmap with rectangle in second image view imageView2.setImageBitmap(drawRectangle()) ...

android kotlin - Draw text on bitmap

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.content.Context import android.graphics.* import android.os.Bundle import android.widget.* import java.io.IOException class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val imageView = findViewById<ImageView>(R.id.imageView) val imageView2 = findViewById<ImageView>(R.id.imageView2) // get the bitmap from assets folder val bitmap = assetsToBitmap("rose.jpg") bitmap?.apply { // show original bitmap in first image view imageView.setImageBitmap(this) // draw text on bitmap // show bitmap with text in second image view imageView2.setImageBitmap(drawText()) } } } ...

android kotlin - Bitmap remove transparency

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.content.Context import android.graphics.* import android.os.Bundle import android.renderscript.* import android.widget.* import java.io.IOException class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val imageView = findViewById<ImageView>(R.id.imageView) val imageView2 = findViewById<ImageView>(R.id.imageView2) val textView2 = findViewById<TextView>(R.id.textView2) val radioGroup = findViewById<RadioGroup>(R.id.radioGroup) // get the bitmap from assets folder val bitmap = assetsToBitmap("transparent_rose.png") bitmap?.apply { // show original bitmap in first image view imageView.se...

android kotlin - Bitmap blur effect

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.content.Context import android.graphics.* import android.os.Bundle import android.renderscript.* import android.widget.* import java.io.IOException class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val imageView = findViewById<ImageView>(R.id.imageView) val imageView2 = findViewById<ImageView>(R.id.imageView2) val textView2 = findViewById<TextView>(R.id.textView2) val seekBar = findViewById<SeekBar>(R.id.seekBar) val context = this // get the bitmap from assets folder val bitmap = assetsToBitmap("rose.jpg") val duplicate = assetsToBitmap("rose.jpg") bitmap?.apply { ...

android kotlin - Bitmap change brightness contrast

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.content.Context import android.graphics.* import android.os.Bundle import android.widget.* import java.io.IOException class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val imageView = findViewById<ImageView>(R.id.imageView) val imageView2 = findViewById<ImageView>(R.id.imageView2) val textView = findViewById<TextView>(R.id.textView) val seekBarBrightness = findViewById<SeekBar>(R.id.seekBarBrightness) val tvBrightness = findViewById<TextView>(R.id.tvBrightness) val seekBarContrast = findViewById<SeekBar>(R.id.seekBarContrast) val tvContrast = findViewById<TextView>(R.id.tvContrast) // get ...