Mobile App Development 2021W Lecture 18

From Soma-notes
Revision as of 20:02, 22 March 2021 by Soma (talk | contribs) (→‎Code)

Video

Video from the lecture given on March 22, 2021 is now available.

Code

MainActivity.kt

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) {
        }
    }
}

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>