Android Application

Kotlin 이미지(Image) : 비트맵-이미지의 기하학적 변환(앱바)

작성자 임베디드코리아 작성일23-10-17 00:51 조회1,395회 댓글0건

첨부파일

Kotlin  비트맵 이미지의 기하학적 변환 -  앱바에서 메뉴 선택
***** activity_main.xml 은 처음 생성된 상태로 사용함  ****
------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    private lateinit var mgv: MyGraphicView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mgv = MyGraphicView(this)
        setContentView(mgv)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        super.onCreateOptionsMenu(menu)

        menu?.add(0, 1, 0, "회전")
        menu?.add(0, 2, 0, "이동")
        menu?.add(0, 3, 0, "확대")
        menu?.add(0, 4, 0, "기울이기")
        menu?.add(0, 5, 0, "원래대로")

        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            1 -> {
                choice = 1
                mgv.invalidate()
                return true
            }
            2 -> {
                choice = 2
                mgv.invalidate()
                return true
            }
            3 -> {
                choice = 3
                mgv.invalidate()
                return true
            }
            4 -> {
                choice = 4
                mgv.invalidate()
                return true
            }
            5 -> {
                choice = 5
                mgv.invalidate()
                return true
            }
        }
        return false
    }
}

------------------------------------------------------------------------------------
      MyGraphicViewy.kt  : New -->
------------------------------------------------------------------------------------
class MyGraphicView(context: Context?) : View(context) {
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val picture = BitmapFactory.decodeResource(resources, R.drawable.majang)
        //s 는 이미지의 이름
        //이미지를 읽어와서 bitmap 객체로 전환
        //확장자 bmp파일은 아니고
        //벡터 방식은 좌표로 이미지 표현
        //비트맵 방식은 점으로 이미지 표현
        // 원본 이미지를 변형하기 좋은 비트맵 임시 파일로 전환.

        val picX = (this.width - picture.width) / 2
        //(화면 크기 - 이미지 크기)/2
        //이미지 표현 시작 좌표

        val picY = (this.height - picture.height) / 2
        val cenX = this.width / 2
        val cenY = this.height / 2

        when (choice) {
            0 -> canvas?.drawBitmap(picture, picX.toFloat(), picY.toFloat(), null)
            //원본대로 읽어와서 표시
            1 -> {
                canvas?.rotate(45f, cenX.toFloat(), cenY.toFloat())
                canvas?.drawBitmap(picture, picX.toFloat(), picY.toFloat(), null)
                //회전
            }
            2 -> {
                canvas?.translate(-150f, 200f)
                canvas?.drawBitmap(picture, picX.toFloat(), picY.toFloat(), null)
                //이동
            }
            3 -> {
                canvas?.scale(2f, 2f, cenX.toFloat(), cenY.toFloat())
                canvas?.drawBitmap(picture, picX.toFloat(), picY.toFloat(), null)
                //확대
            }
            4 -> {
                canvas?.skew(0.3f, 0.3f)
                canvas?.drawBitmap(picture, picX.toFloat(), picY.toFloat(), null)
                //비틀기
            }
            5 -> {
                canvas?.drawBitmap(picture, picX.toFloat(), picY.toFloat(), null)
            }

        }
        picture.recycle() //해당 이미지를 재사용하겠다라는 의미.
    }

    companion object {
        internal var choice = 0
        //
    }
}

*********  앱바(App Bar)  설정  ***************

 ******  themes.xml **  res/values
--------------------------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.BitMapGeometryAppBar" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Customize your light theme here. -->
        <!-- <item name="colorPrimary">@color/my_light_primary</item> -->
        <item name="colorPrimary">#3949AB</item>
        <item name="colorPrimaryDark">#4682B4</item>
    </style>

    <style name="Theme.BitMapGeometryAppBar" parent="Base.Theme.BitMapGeometryAppBar" />
</resources>

 ******  strings.xml **  res/values
-------------------------------------------------------------------------------------------------------
<resources>
    <string name="app_name">비트맵 기학적 변환</string>
</resources>