Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีตั้งค่า WebView Render Priority ใน Android

ภาพรวม

ตัวอย่างนี้สาธิตวิธีการตั้งค่า Render Priority (ลำดับความสำคัญในการเรนเดอร์) ของ WebView ใน Android โดยใช้ WebSettings.setRenderPriority() เพื่อปรับปรุงประสิทธิภาพการแสดงผล

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio ไปที่ File → New Project กรอกข้อมูลที่จำเป็นทั้งหมดเพื่อสร้างโปรเจกต์ใหม่

ขั้นตอนที่ 2: จัดทำ Layout (res/layout/activity_main.xml)

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

ในโค้ดข้างต้น เราใช้ WebView เต็มหน้าจอเพื่อโหลดเว็บไซต์ tutorialspoint.com

ขั้นตอนที่ 3: เขียนโค้ดหลัก (src/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("Loading Data...");
        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);
        
        // สำคัญ: ตั้งค่า Render Priority เป็น HIGH
        webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        
        webView.loadUrl("https://www.tutorialspoint.com/");

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress < 100) {
                    progressDialog.show();
                } else {
                    progressDialog.dismiss();
                }
            }
        });
    }
}

คำอธิบายสำคัญ:

  • setRenderPriority(WebSettings.RenderPriority.HIGH) - ตั้งค่าให้ WebView เรนเดอร์ด้วยความสำคัญสูงสุด
  • ตัวเลือก RenderPriority:
    • HIGH - ความสำคัญสูง (เรนเดอร์เร็วที่สุด ใช้ทรัพยากรมาก)
    • NORMAL - ความสำคัญปกติ (ค่าเริ่มต้น)
    • LOW - ความสำคัญต่ำ (ประหยัดทรัพยากร เรนเดอร์ช้ากว่า)
  • เหมาะสำหรับ: แอปที่เน้นการแสดงผลเว็บซับซ้อน ต้องการความราบรื่นสูงสุด
⚠ ข้อควรระวัง:
• RenderPriority ถูก Deprecated ตั้งแต่ API 18 (Android 4.3) และไม่มีผลใน WebView สมัยใหม่ (Chromium-based)
• WebView สมัยใหม่จัดการ Rendering เองโดยอัตโนมัติ ไม่ต้องตั้งค่านี้
• โค้ดนี้เหมาะสำหรับการศึกษา/รองรับ Legacy เท่านั้น
⚠ ข้อผิดพลาดในโค้ดต้นฉบับ (已修正):
โค้ดต้นฉบับมีเงื่อนไข if (progress = = 100) ซึ่งเป็น Syntax Error (ต้องเป็น == ไม่มีช่องว่าง) - ได้แก้ไขแล้ว

ขั้นตอนที่ 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>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

การทดสอบแอปพลิเคชัน

เชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์ ใน Android Studio เปิดไฟล์ Activity แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณ แล้วตรวจสอบหน้าจอ - จะแสดงเว็บไซต์ tutorialspoint.com

วิธีตั้งค่า WebView Render Priority ใน Android