एंड्रॉइड WebView में रेंडर प्रायोरिटी सेट करना
यह उदाहरण दर्शाता है कि setRenderPriority() का उपयोग करके WebView की रेंडरिंग प्राथमिकता कैसे सेट करें।
⚠️ महत्वपूर्ण नोट
setRenderPriority() API 18 (Android 4.3) में डिप्रिकेटेड है और आधुनिक WebView (Chrome-आधारित) पर इसका कोई प्रभाव नहीं पड़ता। यह केवल पुराने WebKit-आधारित WebView (Android 4.4 से पहले) के लिए था। आधुनिक एंड्रॉइड में इसे इग्नोर किया जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
चरण 3: MainActivity.java (ऐतिहासिक संदर्भ के लिए)
package com.example.myapplication;
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("लोड हो रहा है...");
progressDialog.setCancelable(false);
WebView webView = findViewById(R.id.web_view);
webView.requestFocus();
webView.getSettings().setLightTouchEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setGeolocationEnabled(true);
webView.setSoundEffectsEnabled(true);
webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
webView.getSettings().setUseWideViewPort(true);
// डिप्रिकेटेड - केवल पुराने WebKit WebView के लिए काम करता था
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.loadUrl("https://www.tutorialspoint.com/");
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
if (progress < 100) {
progressDialog.show();
}
if (progress == 100) {
progressDialog.dismiss();
}
}
});
}
}
चरण 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<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>
</intent-filter>
</activity>
</application>
</manifest>
RenderPriority मान (डिप्रिकेटेड)
| कॉन्स्टेंट | विवरण |
|---|---|
| RenderPriority.HIGH | उच्च प्राथमिकता - रेंडरिंग को CPU प्राथमिकता देता था |
| RenderPriority.NORMAL | सामान्य प्राथमिकता (डिफॉल्ट) |
| RenderPriority.LOW | कम प्राथमिकता - बैकग्राउंड टैब्स के लिए |
आधुनिक विकल्प (प्रदर्शन ऑप्टिमाइज़ेशन के लिए)
WebSettings settings = webView.getSettings();
// आधुनिक सेटिंग्स जो वास्तव में काम करती हैं:
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING); // API 19+
// हार्डवेयर एक्सेलेरेशन (मैनिफेस्ट में android:hardwareAccelerated="true")
// या कोड में:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
// ऑफस्क्रीन प्रीरेंडरिंग (API 23+)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
webView.setOffscreenPreRaster(true);
}
ऐप चलाएं
ट्यूटोरियलस्पॉइंट लोड होगा - setRenderPriority का कोई दृश्य प्रभाव नहीं होगा (डिप्रिकेटेड)।
