2 Commits
Author SHA1 Message Date
amos b86fa7586d feat: 添加轮播图预加载指示器,完善三篇博客占位内容
Android Release / release (push) Successful in 46s
2026-06-03 00:52:57 +08:00
amos 462c889964 feat: 使用 WebView 加载网络轮播图,加深搜索图标,添加网络权限
Android Release / release (push) Successful in 50s
2026-06-03 00:46:44 +08:00
4 changed files with 99 additions and 14 deletions
+6 -1
View File
@@ -1,11 +1,16 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 网络权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application <application
android:allowBackup="true" android:allowBackup="true"
android:label="@string/app_name" android:label="@string/app_name"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar"> android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar"
android:usesCleartextTraffic="true">
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
@@ -1,16 +1,34 @@
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.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
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();
// 设置功能按钮点击事件 // 设置功能按钮点击事件
setupFunctionButtons(); setupFunctionButtons();
@@ -18,6 +36,61 @@ public class MainActivity extends Activity {
setupBottomNavigation(); setupBottomNavigation();
} }
private void loadCarousel() {
// 显示加载指示器
if (loadingView != null) {
loadingView.setVisibility(View.VISIBLE);
}
// 隐藏 WebView 直到加载完成
carouselWebView.setVisibility(View.INVISIBLE);
WebSettings settings = carouselWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setLoadWithOverviewMode(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;background:#f3f4f6;'>" +
"<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>";
carouselWebView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
}
private void setupFunctionButtons() { private void setupFunctionButtons() {
// 备忘录 // 备忘录
findViewById(R.id.btnMemo).setOnClickListener(v -> findViewById(R.id.btnMemo).setOnClickListener(v ->
+2 -2
View File
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- 搜索图标 - 加深颜--> <!-- 搜索图标 - -->
<vector xmlns:android="http://schemas.android.com/apk/res/android" <vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="20dp" android:width="20dp"
android:height="20dp" android:height="20dp"
android:viewportWidth="24" android:viewportWidth="24"
android:viewportHeight="24"> android:viewportHeight="24">
<path <path
android:fillColor="#6B7280" android:fillColor="#1F2937"
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/> android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
</vector> </vector>
+18 -11
View File
@@ -76,23 +76,30 @@
android:focusable="true"/> android:focusable="true"/>
</LinearLayout> </LinearLayout>
<!-- 轮播图 --> <!-- 轮播图区域(含加载指示器) -->
<LinearLayout <FrameLayout
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">
android:orientation="vertical"
android:background="@drawable/bg_carousel"
android:elevation="4dp">
<ImageView <!-- 加载指示器 -->
<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_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:src="@drawable/carousel_image" android:background="@drawable/bg_carousel"
android:scaleType="centerCrop" android:elevation="4dp"
android:contentDescription="轮播图"/> android:visibility="invisible"/>
</LinearLayout> </FrameLayout>
<!-- 功能网格标题 --> <!-- 功能网格标题 -->
<TextView <TextView