Mobile App Development 2021W Lecture 18: Difference between revisions
Created page with "==Video== Video from the lecture given on March 22, 2021 [https://homeostasis.scs.carleton.ca/~soma/mad-2021w/lectures/comp1601-2021w-lec18-20210322.m4v is now available]." |
No edit summary |
||
| Line 2: | Line 2: | ||
Video from the lecture given on March 22, 2021 [https://homeostasis.scs.carleton.ca/~soma/mad-2021w/lectures/comp1601-2021w-lec18-20210322.m4v is now available]. | Video from the lecture given on March 22, 2021 [https://homeostasis.scs.carleton.ca/~soma/mad-2021w/lectures/comp1601-2021w-lec18-20210322.m4v is now available]. | ||
==Code== | |||
==MainActivity.kt== | |||
<syntaxhighlight lang="kotlin" line> | |||
package carleton.comp1601.mar22 | |||
import androidx.appcompat.app.AppCompatActivity | |||
import android.os.Bundle | |||
import android.text.Editable | |||
import android.text.TextWatcher | |||
import android.widget.EditText | |||
import android.widget.ImageView | |||
class MainActivity : AppCompatActivity() { | |||
private lateinit var p: ImageView | |||
private var r = 0F | |||
private var scale = 1F | |||
private lateinit var rotationInput: EditText | |||
private lateinit var scaleInput: EditText | |||
override fun onCreate(savedInstanceState: Bundle?) { | |||
super.onCreate(savedInstanceState) | |||
setContentView(R.layout.activity_main) | |||
if (savedInstanceState != null) { | |||
// do something | |||
} | |||
p = findViewById(R.id.mypic) | |||
rotationInput = findViewById(R.id.rotation) | |||
scaleInput = findViewById(R.id.scale) | |||
p.setImageResource(R.drawable.roshi) | |||
rotationInput.addTextChangedListener(rotationWatcher) | |||
scaleInput.addTextChangedListener(scaleWatcher) | |||
updateImage() | |||
} | |||
fun updateImage() { | |||
p.setX(0F) | |||
p.setY(0F) | |||
p.setRotation(r) | |||
p.setScaleX(scale) | |||
p.setScaleY(scale) | |||
} | |||
private val rotationWatcher = object : TextWatcher { | |||
override fun afterTextChanged(s: Editable) { | |||
val new_rS = rotationInput.getText().toString() | |||
val new_r = new_rS.toFloatOrNull() | |||
if (new_r != null) { | |||
r = new_r | |||
updateImage() | |||
} | |||
} | |||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { | |||
} | |||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { | |||
} | |||
} | |||
private val scaleWatcher = object : TextWatcher { | |||
override fun afterTextChanged(s: Editable) { | |||
val new_scaleS = rotationInput.getText().toString() | |||
val new_scale = new_scaleS.toFloatOrNull() | |||
if (new_scale != null) { | |||
if (new_scale > 1.5F) { | |||
scale = 1.5F | |||
} else { | |||
scale = new_scale | |||
} | |||
updateImage() | |||
} | |||
} | |||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { | |||
} | |||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { | |||
} | |||
} | |||
} | |||
<syntaxhighlight> | |||
==activity_main.xml== | |||
<syntaxhighlight lang="xml" line> | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |||
xmlns:app="http://schemas.android.com/apk/res-auto" | |||
xmlns:tools="http://schemas.android.com/tools" | |||
android:layout_width="match_parent" | |||
android:layout_height="match_parent" | |||
tools:context=".MainActivity"> | |||
<LinearLayout | |||
android:layout_width="match_parent" | |||
android:layout_height="match_parent" | |||
android:orientation="vertical"> | |||
<EditText | |||
android:id="@+id/rotation" | |||
android:layout_width="match_parent" | |||
android:layout_height="wrap_content" | |||
android:ems="10" | |||
android:inputType="numberDecimal" /> | |||
<EditText | |||
android:id="@+id/scale" | |||
android:layout_width="match_parent" | |||
android:layout_height="wrap_content" | |||
android:ems="10" | |||
android:inputType="numberDecimal" /> | |||
<ImageView | |||
android:id="@+id/mypic" | |||
android:layout_width="match_parent" | |||
android:layout_height="wrap_content" | |||
app:srcCompat="@drawable/kittens" /> | |||
</LinearLayout> | |||
</androidx.constraintlayout.widget.ConstraintLayout> | |||
<syntaxhighlight> | |||
Revision as of 19:49, 22 March 2021
Video
Video from the lecture given on March 22, 2021 is now available.
Code
MainActivity.kt
<syntaxhighlight lang="kotlin" line> package carleton.comp1601.mar22
import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.text.Editable import android.text.TextWatcher import android.widget.EditText import android.widget.ImageView
class MainActivity : AppCompatActivity() {
private lateinit var p: ImageView private var r = 0F private var scale = 1F private lateinit var rotationInput: EditText private lateinit var scaleInput: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState != null) {
// do something
}
p = findViewById(R.id.mypic)
rotationInput = findViewById(R.id.rotation)
scaleInput = findViewById(R.id.scale)
p.setImageResource(R.drawable.roshi)
rotationInput.addTextChangedListener(rotationWatcher)
scaleInput.addTextChangedListener(scaleWatcher)
updateImage()
}
fun updateImage() {
p.setX(0F)
p.setY(0F)
p.setRotation(r)
p.setScaleX(scale)
p.setScaleY(scale)
}
private val rotationWatcher = object : TextWatcher {
override fun afterTextChanged(s: Editable) {
val new_rS = rotationInput.getText().toString()
val new_r = new_rS.toFloatOrNull()
if (new_r != null) {
r = new_r
updateImage()
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
}
private val scaleWatcher = object : TextWatcher {
override fun afterTextChanged(s: Editable) {
val new_scaleS = rotationInput.getText().toString()
val new_scale = new_scaleS.toFloatOrNull()
if (new_scale != null) {
if (new_scale > 1.5F) {
scale = 1.5F
} else {
scale = new_scale
}
updateImage()
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
}
} <syntaxhighlight>
activity_main.xml
<syntaxhighlight lang="xml" line> <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/rotation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/scale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" />
<ImageView
android:id="@+id/mypic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/kittens" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> <syntaxhighlight>