Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Kotlin का उपयोग करके Android ImageView में जूम-इन और जूम-आउट कैसे करें?

परिचय

यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android ImageView में ScaleGestureDetector का उपयोग करके पिंच टू जूम (जूम-इन और जूम-आउट) कैसे लागू किया जाए।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।

चरण 2: लेआउट XML कॉन्फ़िगर करें

res/layout/activity_main.xml में निम्नलिखित कोड जोड़ें:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/image"
        android:scaleType="matrix" />

</LinearLayout>

चरण 3: Kotlin कोड लागू करें

src/MainActivity.kt में निम्नलिखित कोड जोड़ें:

import android.os.Bundle
import android.view.MotionEvent
import android.view.ScaleGestureDetector
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import kotlin.math.max
import kotlin.math.min

class MainActivity : AppCompatActivity() {
    private lateinit var scaleGestureDetector: ScaleGestureDetector
    private var scaleFactor = 1.0f
    private lateinit var imageView: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        
        imageView = findViewById(R.id.imageView)
        scaleGestureDetector = ScaleGestureDetector(this, ScaleListener())
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        scaleGestureDetector.onTouchEvent(event)
        return true
    }

    private inner class ScaleListener : SimpleOnScaleGestureListener() {
        override fun onScale(detector: ScaleGestureDetector): Boolean {
            scaleFactor *= detector.scaleFactor
            // जूम सीमा: 0.1x से 10x तक
            scaleFactor = max(0.1f, min(scaleFactor, 10.0f))
            imageView.scaleX = scaleFactor
            imageView.scaleY = scaleFactor
            return true
        }
    }
}

चरण 4: मैनिफेस्ट कॉन्फ़िगर करें

androidManifest.xml में निम्नलिखित कोड जोड़ें:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.q11">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

ऐप चलाएं

अपने एंड्रॉइड डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और इमेज पर दो उंगलियों से पिंच करके जूम टेस्ट करें।

उन्नत: पैन और जूम दोनों

बेहतर यूजर अनुभव के लिए, PhotoView लाइब्रेरी का उपयोग करें या कस्टम TouchListener में ट्रांसलेशन (पैन) भी जोड़ें:

// PhotoView लाइब्रेरी (build.gradle में जोड़ें)
// implementation 'com.github.chrisbanes:PhotoView:2.3.0'

// XML में:
<com.github.chrisbanes.photoview.PhotoView
    android:id="@+id/photoView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

// कोड में:
photoView.setImageResource(R.drawable.image)
  1. Android में Picasso का उपयोग करके URL से ImageView में इमेज लोड करना Android में Picasso का उपयोग करके URL से ImageView में इमेज लोड करना

    परिचयPicasso एक लोकप्रिय इमेज लोडिंग लाइब्रेरी है जो URL से इमेज डाउनलोड, कैशिंग और ImageView में प्रदर्शित करने को एक लाइन में सरल बनाती है।कार्यान्वयनचरण 1: build.gradle में निर्भरता जोड़ेंdependencies { implementation com.squareup.picasso:picasso:2.71828 }चरण 2: लेआउट (activity_main.xml)<

  1. Android में कोड का उपयोग करके ImageView का मार्जिन कैसे सेट करें Android में कोड का उपयोग करके ImageView का मार्जिन कैसे सेट करें

    परिचयरनटाइम पर व्यू के मार्जिन को बदलने के लिए, उसके `LayoutParams` को प्राप्त करें, मार्जिन सेट करें, और फिर `setLayoutParams()` कॉल करें।कार्यान्वयनचरण 1: लेआउट (activity_main.xml)<ImageView android:id=@+id/imageView android:layout_width=wrap_content android:layout_height=wrap_content android:b

  1. Android ImageView में जूम-इन और जूम-आउट कैसे करें? Android ImageView में जूम-इन और जूम-आउट कैसे करें?

    ScaleGestureDetector का उपयोग करके ImageView पर पिंच-टू-जूम कार्यक्षमता लागू करने का तरीका। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट res/layout/activity_main.xml: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=https://schemas.android.