Page Zoom Controls Android Example kotlin

29-Dec-2024

Page Zoom Controls Android Example kotlin


Output:





1/ Modify activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:scaleType="centerCrop"
android:layout_centerInParent="true"
android:src="@drawable/img" />

<ZoomControls
android:id="@+id/simpleZoomControl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/img"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />

</RelativeLayout>



2/ Modify MainActivity.kt



package com.zissofworks.pagezoomcontrols

import android.os.Bundle
import android.widget.ImageView
import android.widget.ZoomControls
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat


class MainActivity : AppCompatActivity() {
private lateinit var simpleZoomControls: ZoomControls
private lateinit var img: ImageView

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

simpleZoomControls = findViewById(R.id.simpleZoomControl);
img = findViewById(R.id.img);

simpleZoomControls.setOnZoomInClickListener{
val x = img.scaleX
val y = img.scaleY

img.scaleX = x + 1
img.scaleY = y + 1
}

simpleZoomControls.setOnZoomOutClickListener(){
val x = img.scaleX
val y = img.scaleY

img.scaleX = x - 1
img.scaleY = y - 1
}
}
}



Comments