एंड्रॉइड में व्यू की पारदर्शिता (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% पारदर्शी हो जाएगी।