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

Android में Picasso लाइब्रेरी से इमेज रिसाइज कैसे करें?

Picasso लाइब्रेरी से इमेज लोडिंग और रिसाइजिंग का तरीका

Picasso (Square द्वारा) एक लोकप्रिय इमेज लोडिंग लाइब्रेरी है जो कैशिंग, रिसाइजिंग, प्लेसहोल्डर और एरर हैंडलिंग स्वचालित रूप से संभालती है।

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

Android Studio में File → New Project चुनें।

चरण 2: build.gradle (Module: app) में डिपेंडेंसी जोड़ें

dependencies {
    implementation 'com.squareup.picasso:picasso:2.8'
    // नवीनतम वर्जन के लिए mvnrepository.com चेक करें
}

सिंक करने के बाद Sync Now क्लिक करें।

चरण 3: लेआउट फ़ाइल (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:orientation="vertical"
    android:padding="8dp"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

चरण 4: MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;

public class MainActivity extends AppCompatActivity {
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageView);
        String url = "https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg";

        // इमेज लोड करें, 200x500 में रिसाइज करें और ImageView में सेट करें
        Picasso.get()
            .load(url)
            .resize(200, 500)
            .centerCrop() // अनुपात बनाए रखते हुए क्रॉप करें
            .placeholder(R.drawable.placeholder) // वैकल्पिक: लोडिंग तक प्लेसहोल्डर
            .error(R.drawable.error_image)     // वैकल्पिक: एरर पर इमेज
            .into(imageView);
    }
}

चरण 5: AndroidManifest.xml

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

    <uses-permission android:name="android.permission.INTERNET" />

    <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>

उपयोगी Picasso फीचर्स

  • .fit() - ImageView के आकार अनुसार ऑटो-रिसाइज (layout पूरा होने पर)
  • .centerInside() - इमेज को बाउंड्स के अंदर फिट करे बिना क्रॉप किए
  • .rotate(90f) - इमेज घुमाएं
  • .transform(new CircleTransform()) - गोल इमेज के लिए कस्टम ट्रांसफॉर्मेशन
  • .priority(Picasso.Priority.HIGH) - लोडिंग प्राथमिकता सेट करें

आधुनिक विकल्प: Glide या Coil

Glide (Google द्वारा अनुशंसित) और Coil (Kotlin-first, कोरूटीन सपोर्ट) नए प्रोजेक्ट्स के लिए बेहतर विकल्प हैं।

  1. एंड्रॉइड में स्मूथ इमेज रोटेशन कैसे बनाएं? एंड्रॉइड में स्मूथ इमेज रोटेशन कैसे बनाएं?

    एंड्रॉइड में XML एनिमेशन से स्मूथ इमेज रोटेशन यह उदाहरण दिखाता है कि res/anim/ फोल्डर में XML एनिमेशन परिभाषित करके इमेज को स्मूथली कैसे घुमाया जाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं। चरण 2: लेआउट XML res/layout/activity_main.xml: <?x

  1. Android में Picasso लाइब्रेरी से इमेज डाउनलोड/लोड कैसे करें? Android में Picasso लाइब्रेरी से इमेज डाउनलोड/लोड कैसे करें?

    परिचय Picasso Square द्वारा विकसित एक लोकप्रिय इमेज लोडिंग लाइब्रेरी है जो इमेज डाउनलोडिंग, कैशिंग, रीसाइज़िंग और डिस्प्ले को सरल बनाती है। एक लाइन कोड में इमेज लोड होती है। आवश्यक चरण चरण 1: नया प्रोजेक्ट बनाएं और डिपेंडेंसी जोड़ें Android Studio में File → New Project से नया प्रोजेक्ट बनाएं। bui

  1. Android में ImageView में इमेज को एस्पेक्ट रेशियो बनाए रखते हुए कैसे स्केल करें? Android में ImageView में इमेज को एस्पेक्ट रेशियो बनाए रखते हुए कैसे स्केल करें?

    यह उदाहरण दर्शाता है कि Android में ImageView में इमेज को एस्पेक्ट रेशियो (aspect ratio) बनाए रखते हुए कैसे स्केल किया जाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और सभी आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं। चरण 2: लेआउट XML में कोड जोड़ें res/layout/activ