Skip to main content

Posts

Showing posts with the label Bitmap

android kotlin - BitmapShader example

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 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) // set bitmap shader as text view paint textView2.paint.shader = toBitmapShader() } } } // extension function to get bitma...

android kotlin - Bitmap tint solid color

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 bitmaps from assets folder val bitmap = assetsToBitmap("transparent_rose.png") bitmap?.apply { // show original bitmap in first image view imageView.setImageBitmap(this) // tint bitmap to solid color imageView2.setImageBitmap( tintSolidColor( Color.parseColor(...

android kotlin - Bitmap mask

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 bitmaps from assets folder val bitmap = assetsToBitmap("flower103.jpg") val maskBitmap = assetsToBitmap("transparent_rose.png") bitmap?.apply { // show original bitmap in first image view imageView.setImageBitmap(this) // mask bitmap maskBitmap?.let { imageView2...

android kotlin - Bitmap crop center

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 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 - 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()) } ...