Creating Custom Widgets in Badge Magic Android

In this blog, we are going to have a look on how I created this badge preview in fossasia/badge-magic-android.  What is Canvas? Canvas is a class in Android that performs 2D drawing of different objects onto the screen. The saying “a blank canvas” is very similar to what a Canvas object is on Android. It is basically, an empty space to draw onto. Canvas Coordinate System The coordinate system of the Android canvas starts in the top left corner, where [0,0] represents that point. The y axis is positive downwards, and x axis positive towards the right. Some basics of Canvas, lets see how we drew this Preview Badge. The Badge consists of only 2 components: Rounded Rectangle ( Background )Normal Rectangles ( LED Lights ) Let’s see how we create rounded rectangles in android.  // Draw Background canvas.drawRoundRect(bgBounds, 25f, 25f, bgPaint) Using drawRoundRect() we can easily create the badge background. 25f specified is the corner radius of the rectangle. The LED Lights are just drawable resources which are used according to the current state of the LED. private fun drawLED(condition: Boolean, canvas: Canvas, xValue: Int, yValue: Int) { if (condition) { ledEnabled.bounds = cells[xValue].list[yValue] ledEnabled.draw(canvas) } else { ledDisabled.bounds = cells[xValue].list[yValue] ledDisabled.draw(canvas) } } This function draws the LED Lights if the condition is satisfied. When we consider a custom view, we need to consider the changes which occur according. These layout changes are to be controlled and maintained accordingly, Let’s see how we manage the positioning of the led lights for every android device. Spoiler: Simple 10th Grade Maths xD override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { super.onLayout(changed, left, top, right, bottom) val offset = 30 val singleCell = (right - left - (offset * 3)) / badgeWidth val offsetXToAdd: Int = ((((right - offset).toFloat() - (left + offset).toFloat()) - (singleCell * badgeWidth)) / 2).toInt() + 1 cells = mutableListOf() for (i in 0 until badgeHeight) { cells.add(Cell()) for (j in 0 until badgeWidth) { cells[i].list.add(Rect( (offsetXToAdd * 2) + j * singleCell, (offsetXToAdd * 2) + i * singleCell, (offsetXToAdd * 2) + j * singleCell + singleCell, (offsetXToAdd * 2) + i * singleCell + singleCell )) } } bgBounds = RectF((offsetXToAdd).toFloat(), (offsetXToAdd).toFloat(), ((singleCell * badgeWidth) + (offsetXToAdd * 3)).toFloat(), ((singleCell * badgeHeight) + (offsetXToAdd * 3)).toFloat()) } We create an offset which is nothing but the gap from the screen edge to the badge itself, now we need to have gaps on both sides of the badge and we also leave half the offset inside the badge which is the difference between the badge background and the LED starting point, hence we calculate the value of single cells by:  val singleCell = (right - left - (offset * 3)) / badgeWidth We minus the no of pixels on the right of the display to the left, to get the width of the actual screen. Then we minus the padding from the left and right which is offset * 3…

Continue ReadingCreating Custom Widgets in Badge Magic Android

Understanding PN Junctions with the Pocket Science Lab

The boundary layer between two thin films of a semiconducting material with Positive type and Negative type doping is referred to as a P-N junction, and these are one of the fundamental building blocks of electronics. These junctions exhibit various properties that have given them a rather indispensable status in modern day electronics. The PSLab’s various measurement tools enable us to understand these devices, and in this blog post we shall explain some uses of PN junctions, and visualize their behaviour with the PSLab. One might easily be confused and assume that a positive doping implies that the layer has a net positive charge, but this is not the case. A positive doping involves replacing a minute quantity of the semiconductor molecules with atoms from the next column in the periodic table. These atoms such as phosphorus are also charge neutral, but the number of available mobile charge carriers effectively increases. A diode as a half-wave rectifier A diode is basically just a PN junction. An ideal diode conducts electricity in one direction offering a path of zero resistance, and it is a perfect insulator in the other direction. In practice, we may observe some additional properties. Figure : The circuit used for making the half-wave rectifier and studying it. A bipolar sinusoidal signal is input to a diode, and the output voltage is monitored. The 1uF capacitor is used to filter the output signal and make it more or less constant, but it has not been used while obtaining the data shown in the following image We can observe that only the positive half of the signal passes through the diode. It can also be observed , that since this is not an ideal diode, the conducted portion has lost some amplitude. This loss is a consequence of the forward threshold voltage of the PN junction, and in case of this diode, it is around 0.6 Volts. This threshold voltage depends on the band structure of the diode , and in the next section we shall examine this voltage for various diodes. Measurement of Current-Voltage Characteristics of diodes In practice, diodes only start conducting in the forward direction after a certain threshold potential difference is present. This voltage, also known as the barrier potential, depends on the band gap of the diode, and we shall measure it to determine how the electrical properties affect the externally visible physical properties of the diode. A programmable voltage output of the PSLab (PV1) will be increased in small steps starting from 0 Volts, and a voltmeter input (CH3) will be used to determine the point when the diode starts conducting. The presence of a known resistor between PV1 and CH3 acts as a current limiter, and also enables us to calculate the current flow using some elementary application of the Ohm’s law. I = (PV1-CH3)/1000 . The following image shows I-V characteristics of various diodes ranging from Schottky to Light Emitting Diodes (LEDs). It may be interesting to note that the frequency…

Continue ReadingUnderstanding PN Junctions with the Pocket Science Lab