Skip to main content

Posts

Showing posts from June, 2020

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 - Rounded corners 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 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 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) // create rounded corners bitmap imageView2...

Android Kotlin: How to create a circular bitmap with a border

Introduction This Android Kotlin tutorial demonstrates how to create a circular bitmap with a border using a custom extension function. The example covers extracting a bitmap image from the assets folder, transforming it into a circular shape, and adding a border around it. This is particularly useful for displaying profile pictures, icons, or thumbnails in a circular form, which is a common design pattern in modern Android apps. In this example, the image manipulation is done using the Canvas and Paint classes from Android's graphics library, along with a couple of helper functions. The project also makes use of ConstraintLayout to align and display two ImageViews: one for the original image and the other for the circular, bordered version. MainActivity Breakdown The MainActivity class extends the Activity class and sets the content view to activity_main.xml , which contains the layout for the app. The layout includes two ImageView components and one TextView to la...

android kotlin - Bitmap crop circular area

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) // get the bitmap from assets folder val bitmap = assetsToBitmap("flower103.jpg") bitmap?.apply { // show original bitmap in first image view imageView.setImageBitmap(this) // crop circular area from bitmap imageView2.setImageBitmap(cropCircularArea()) } } } // extensio...

android kotlin - Bitmap add border

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 border to bitmap // and keep bitmap transparency ...

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 - Bitmap draw shadow

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("flower103.jpg") bitmap?.apply { // show original bitmap in first image view imageView.setImageBitmap(this) // draw shadow on bitmap // show bitmap with shadow in second image view imageView2.setImageBitmap(drawShadow()) } ...

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 line 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 line on bitmap // show bitmap with line in second image view imageView2.setImageBitmap(drawLine()) } } } ...

android kotlin - Draw circle 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 circle on bitmap // show bitmap with circle in second image view imageView2.setImageBitmap(drawCircle()) } ...

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 color matrix set scale

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 seekBarRed = findViewById<SeekBar>(R.id.seekBarRed) val seekBarGreen = findViewById<SeekBar>(R.id.seekBarGreen) val seekBarBlue = findViewById<SeekBar>(R.id.seekBarBlue) val seekBarAlpha = findViewById<SeekBar>(R.id.seekBarAlpha) val tvIntensity = findViewById<TextView>(R.id.tvIntensity) // get the bitmap fro...

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...