This commit is contained in:
@@ -1,22 +1,32 @@
|
|||||||
package com.arkj.app;
|
package com.arkj.app;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.webkit.WebSettings;
|
import android.webkit.WebSettings;
|
||||||
import android.webkit.WebView;
|
import android.webkit.WebView;
|
||||||
|
import android.webkit.WebViewClient;
|
||||||
|
import android.widget.ProgressBar;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
public class MainActivity extends Activity {
|
public class MainActivity extends Activity {
|
||||||
// 轮播图 URL
|
// 轮播图 URL
|
||||||
private static final String CAROUSEL_URL = "https://alist.kg8.net/d/蓝奏云/IMG_20190602_082558.webp";
|
private static final String CAROUSEL_URL = "https://alist.kg8.net/d/蓝奏云/IMG_20190602_082558.webp";
|
||||||
|
|
||||||
|
private WebView carouselWebView;
|
||||||
|
private View loadingView;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
// 加载轮播图
|
// 初始化视图
|
||||||
|
carouselWebView = findViewById(R.id.carouselWebView);
|
||||||
|
loadingView = findViewById(R.id.carouselLoading);
|
||||||
|
|
||||||
|
// 加载轮播图(预加载,完成后显示)
|
||||||
loadCarousel();
|
loadCarousel();
|
||||||
|
|
||||||
// 设置功能按钮点击事件
|
// 设置功能按钮点击事件
|
||||||
@@ -27,15 +37,56 @@ public class MainActivity extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void loadCarousel() {
|
private void loadCarousel() {
|
||||||
WebView carouselWebView = findViewById(R.id.carouselWebView);
|
// 显示加载指示器
|
||||||
|
if (loadingView != null) {
|
||||||
|
loadingView.setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 隐藏 WebView 直到加载完成
|
||||||
|
carouselWebView.setVisibility(View.INVISIBLE);
|
||||||
|
|
||||||
WebSettings settings = carouselWebView.getSettings();
|
WebSettings settings = carouselWebView.getSettings();
|
||||||
settings.setJavaScriptEnabled(true);
|
settings.setJavaScriptEnabled(true);
|
||||||
settings.setLoadWithOverviewMode(true);
|
settings.setLoadWithOverviewMode(true);
|
||||||
settings.setUseWideViewPort(true);
|
settings.setUseWideViewPort(true);
|
||||||
|
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
|
||||||
|
|
||||||
|
// 设置 WebViewClient 监听加载完成
|
||||||
|
carouselWebView.setWebViewClient(new WebViewClient() {
|
||||||
|
@Override
|
||||||
|
public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
||||||
|
super.onPageStarted(view, url, favicon);
|
||||||
|
// 开始加载
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPageFinished(WebView view, String url) {
|
||||||
|
super.onPageFinished(view, url);
|
||||||
|
// 加载完成,显示 WebView,隐藏加载指示器
|
||||||
|
carouselWebView.setVisibility(View.VISIBLE);
|
||||||
|
if (loadingView != null) {
|
||||||
|
loadingView.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
|
||||||
|
super.onReceivedError(view, errorCode, description, failingUrl);
|
||||||
|
// 加载失败,显示错误提示
|
||||||
|
if (loadingView != null) {
|
||||||
|
loadingView.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
Toast.makeText(MainActivity.this, "轮播图加载失败", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// 加载网络图片
|
// 加载网络图片
|
||||||
String html = "<html><body style='margin:0;padding:0;'>" +
|
String html = "<html><body style='margin:0;padding:0;background:#f3f4f6;'>" +
|
||||||
"<img src='" + CAROUSEL_URL + "' style='width:100%;height:100%;object-fit:cover;' />" +
|
"<div style='display:flex;align-items:center;justify-content:center;height:100vh;margin:0;'>" +
|
||||||
|
"<img src='" + CAROUSEL_URL + "' style='width:100%;height:100%;object-fit:cover;' " +
|
||||||
|
"onload='document.body.style.height=\"auto\"' " +
|
||||||
|
"onerror='document.body.innerHTML=\"<div style=\\\"color:#999;text-align:center;padding:20px;\\\">图片加载失败</div>\"' />" +
|
||||||
|
"</div>" +
|
||||||
"</body></html>";
|
"</body></html>";
|
||||||
carouselWebView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
|
carouselWebView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,15 +76,30 @@
|
|||||||
android:focusable="true"/>
|
android:focusable="true"/>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<!-- 轮播图 - 使用 WebView 加载网络图片 -->
|
<!-- 轮播图区域(含加载指示器) -->
|
||||||
<WebView
|
<FrameLayout
|
||||||
android:id="@+id/carouselWebView"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="180dp"
|
android:layout_height="180dp"
|
||||||
android:layout_marginHorizontal="24dp"
|
android:layout_marginHorizontal="24dp"
|
||||||
android:layout_marginBottom="20dp"
|
android:layout_marginBottom="20dp">
|
||||||
|
|
||||||
|
<!-- 加载指示器 -->
|
||||||
|
<ProgressBar
|
||||||
|
android:id="@+id/carouselLoading"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:indeterminateTint="#6366F1"/>
|
||||||
|
|
||||||
|
<!-- WebView 初始隐藏,加载完成后显示 -->
|
||||||
|
<WebView
|
||||||
|
android:id="@+id/carouselWebView"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
android:background="@drawable/bg_carousel"
|
android:background="@drawable/bg_carousel"
|
||||||
android:elevation="4dp"/>
|
android:elevation="4dp"
|
||||||
|
android:visibility="invisible"/>
|
||||||
|
</FrameLayout>
|
||||||
|
|
||||||
<!-- 功能网格标题 -->
|
<!-- 功能网格标题 -->
|
||||||
<TextView
|
<TextView
|
||||||
|
|||||||
Reference in New Issue
Block a user