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

एंड्रॉइड में व्यू की ओपेसिटी (पारदर्शिता) कैसे सेट करें?

एंड्रॉइड में व्यू की पारदर्शिता (Alpha/Opacity) सेट करना

यह उदाहरण दिखाता है कि `View.setAlpha()` और `ObjectAnimator` का उपयोग करके व्यू की पारदर्शिता कैसे बदली जाती है।

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

Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं।

चरण 2: लेआउट XML

res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:background="#011ffd"
        android:gravity="center"
        android:text="TextView"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFFFFF" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:text="पारदर्शिता एनिमेट करें (0.6)" />

</RelativeLayout>

चरण 3: MainActivity में ओपेसिटी लॉजिक

MainActivity.java:

import androidx.appcompat.app.AppCompatActivity;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private Button button;
    private ObjectAnimator alphaAnimator;

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

        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);

        // ObjectAnimator: "alpha" प्रॉपर्टी एनिमेट करता है (0.0f से 1.0f)
        alphaAnimator = ObjectAnimator.ofFloat(textView, "alpha", 1.0f, 0.6f);
        alphaAnimator.setDuration(4000); // 4 सेकंड

        button.setOnClickListener(v -> {
            // टॉगल: यदि पूरी तरह अपारदर्शी है तो पारदर्शी करें, अन्यथा वापस लाएं
            float currentAlpha = textView.getAlpha();
            float targetAlpha = (currentAlpha > 0.8f) ? 0.6f : 1.0f;
            
            alphaAnimator = ObjectAnimator.ofFloat(textView, "alpha", currentAlpha, targetAlpha);
            alphaAnimator.setDuration(1000);
            alphaAnimator.start();
        });
    }
}

🎯 तत्काल ओपेसिटी सेट करना (बिना एनिमेशन)

// XML में:
android:alpha="0.5"

// कोड में:
textView.setAlpha(0.5f); // 0.0f (पूरी तरह पारदर्शी) से 1.0f (पूरी तरह अपारदर्शी)

// बैकग्राउंड कलर में अल्फा (केवल बैकग्राउंड के लिए):
textView.getBackground().setAlpha(128); // 0-255 रेंज

🎨 XML में एनिमेशन परिभाषित करना

res/anim/fade.xml:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="https://schemas.android.com/apk/res/android"
    android:fromAlpha="1.0"
    android:toAlpha="0.3"
    android:duration="2000"
    android:repeatCount="1"
    android:repeatMode="reverse" />

उपयोग:

Animation fadeAnim = AnimationUtils.loadAnimation(this, R.anim.fade);
textView.startAnimation(fadeAnim);

चरण 4: Manifest

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

    <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" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

📋 मुख्य बिंदु

  • alpha = 1.0f: पूरी तरह दृश्यमान (डिफ़ॉल्ट)
  • alpha = 0.0f: पूरी तरह अदृश्य (लेकिन लेआउट स्पेस लेता है)
  • alpha = 0.5f: 50% पारदर्शी
  • View.GONE vs alpha=0: GONE लेआउट स्पेस नहीं लेता, alpha=0 लेता है
  • ObjectAnimator.ofFloat(view, "alpha", ...) - प्रॉपर्टी एनिमेशन के लिए मानक तरीका

ऐप चलाएं

डिवाइस कनेक्ट करें, Run पर क्लिक करें। बटन दबाने पर TextView 4 सेकंड में 60% पारदर्शी हो जाएगी।

  1. Android में DatePicker डायलॉग में दिनांक कैसे सेट करें Android में DatePicker डायलॉग में दिनांक कैसे सेट करें

    परिचय`DatePickerDialog` उपयोगकर्ता को एक डायलॉग से दिनांक चुनने की अनुमति देता है। यह लेख `DialogFragment` का उपयोग करके एक डेट पिकर दिखाने और चयनित दिनांक को TextView में प्रदर्शित करने का तरीका दिखाता है।कार्यान्वयनचरण 1: लेआउट (activity_main.xml)<TextView android:id=@+id/textView android:text=

  1. Android में वॉलपेपर इमेज प्रोग्रामेटिकली कैसे सेट करें? Android में वॉलपेपर इमेज प्रोग्रामेटिकली कैसे सेट करें?

    यह उदाहरण दर्शाता है कि Android ऐप से डिवाइस का वॉलपेपर प्रोग्रामेटिकली कैसे बदला जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं और res/drawable/ में wallpaper नाम की इमेज रखें। चरण 2: लेआउट XML res/layout/activity_main.xml: <LinearLayout xmlns:android=https://schemas.an

  1. Android में AsyncTask के लिए टाइमआउट कैसे सेट करें? Android में AsyncTask के लिए टाइमआउट कैसे सेट करें?

    परिचय यह ट्यूटोरियल दिखाता है कि AsyncTask के लिए टाइमआउट कैसे सेट करें। ध्यान दें: AsyncTask API 30 (Android 11) में डिप्रिकेटेड है। आधुनिक ऐप्स के लिए Coroutines, RxJava, या ExecutorService उपयोग करें। यह उदाहरण केवल लिगेसी कोड समझने के लिए है। आवश्यक चरण चरण 1: नया प्रोजेक्ट बनाएं Android Studi