Skip to content

Commit 32f786a

Browse files
author
ChenRui
committed
博文界面UI调整
1 parent 2dbf2c3 commit 32f786a

File tree

11 files changed

+280
-14
lines changed

11 files changed

+280
-14
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,24 @@
2727
android:name=".activity.LauncherActivity"
2828
android:screenOrientation="portrait"
2929
android:theme="@style/AppTheme.NoActionBar.FullScreen">
30-
<intent-filter>
30+
<!-- <intent-filter>
3131
<action android:name="android.intent.action.MAIN"/>
3232
3333
<category android:name="android.intent.category.LAUNCHER"/>
34-
</intent-filter>
34+
</intent-filter>-->
3535
</activity>
3636

3737
<!--主界面-->
3838
<activity
3939
android:name=".activity.MainActivity"
4040
android:screenOrientation="portrait"
41-
android:theme="@style/AppTheme.Dark"/>
41+
android:theme="@style/AppTheme.Dark">
42+
<intent-filter>
43+
<action android:name="android.intent.action.MAIN"/>
44+
45+
<category android:name="android.intent.category.LAUNCHER"/>
46+
</intent-filter>
47+
</activity>
4248

4349
<activity
4450
android:name=".activity.TestActivity"

app/src/main/java/com/rae/cnblogs/CnblogsApplication.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import com.rae.cnblogs.sdk.db.DbCnblogs;
1313
import com.rae.cnblogs.sdk.db.DbFactory;
1414
import com.rae.swift.session.SessionManager;
15-
import com.tencent.bugly.Bugly;
1615
import com.tencent.tinker.loader.app.TinkerApplication;
1716
import com.tencent.tinker.loader.shareutil.ShareConstants;
1817
import com.umeng.socialize.PlatformConfig;
@@ -38,7 +37,7 @@ public void onCreate() {
3837
// 级别较高的初始化操作
3938
DbCnblogs.init(getApplication());
4039
// 日志上报
41-
Bugly.init(getApplication(), BuildConfig.BUGLY_APP_ID, BuildConfig.DEBUG);
40+
// Bugly.init(getApplication(), BuildConfig.BUGLY_APP_ID, BuildConfig.DEBUG);
4241
// if (!LeakCanary.isInAnalyzerProcess(this)) {
4342
// LeakCanary.install(this);
4443
// }

app/src/main/java/com/rae/cnblogs/activity/BaseActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ protected BaseActivity getContext() {
121121
}
122122

123123
public void onBackClick(View view) {
124-
finish();
124+
onBackPressed();
125125
}
126126

127127
@Override

app/src/main/java/com/rae/cnblogs/fragment/BlogContentFragment.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.os.Bundle;
77
import android.support.annotation.Nullable;
88
import android.support.v4.content.ContextCompat;
9+
import android.util.Log;
910
import android.view.View;
1011
import android.webkit.JavascriptInterface;
1112
import android.webkit.WebSettings;
@@ -28,6 +29,7 @@
2829
import com.rae.cnblogs.sdk.bean.BlogBean;
2930
import com.rae.cnblogs.sdk.bean.BlogType;
3031
import com.rae.cnblogs.sdk.db.model.UserBlogInfo;
32+
import com.rae.cnblogs.utils.ViewCaptureUtils;
3133
import com.rae.cnblogs.widget.ImageLoadingView;
3234
import com.rae.cnblogs.widget.PlaceholderView;
3335
import com.rae.cnblogs.widget.RaeWebView;
@@ -37,7 +39,10 @@
3739
import org.greenrobot.eventbus.EventBus;
3840
import org.greenrobot.eventbus.Subscribe;
3941

42+
import java.io.File;
43+
4044
import butterknife.BindView;
45+
import io.reactivex.functions.Consumer;
4146

4247

4348
/**
@@ -91,9 +96,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
9196
@Override
9297
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
9398
super.onViewCreated(view, savedInstanceState);
94-
// mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
95-
// todo:调试模式
96-
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
99+
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
97100

98101
mPlaceholderView.setOnRetryClickListener(new View.OnClickListener() {
99102
@Override
@@ -124,6 +127,8 @@ public void onScrollChange(int x, int y, int oldX, int oldY) {
124127
mBackView.setImageResource(R.drawable.ic_back_white);
125128
mMoreView.setImageResource(R.drawable.ic_blog_content_more);
126129
}
130+
131+
Log.i("rae", "滚动高度:" + y);
127132
}
128133
});
129134
}
@@ -160,6 +165,22 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
160165

161166
mBackView = getActivity().findViewById(R.id.back);
162167
mMoreView = getActivity().findViewById(R.id.img_action_bar_more);
168+
169+
// 测试一下
170+
final ViewCaptureUtils viewCaptureUtils = new ViewCaptureUtils(getContext());
171+
mBackView.setOnClickListener(new View.OnClickListener() {
172+
173+
@Override
174+
public void onClick(View view) {
175+
viewCaptureUtils.capture(mWebView, "/sdcard/test.jpg")
176+
.subscribe(new Consumer<File>() {
177+
@Override
178+
public void accept(File file) throws Exception {
179+
AppUI.toast(getContext(), "保存成功!");
180+
}
181+
});
182+
}
183+
});
163184
}
164185

165186
@Override

app/src/main/java/com/rae/cnblogs/fragment/WebViewFragment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
8181
View view = super.onCreateView(inflater, container, savedInstanceState);
8282

8383

84+
8485
mWebView = new RaeWebView(getContext().getApplicationContext());
8586
mWebView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
8687
mContentLayout.addView(mWebView);
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package com.rae.cnblogs.utils;
2+
3+
import android.annotation.TargetApi;
4+
import android.content.Context;
5+
import android.graphics.Bitmap;
6+
import android.graphics.Canvas;
7+
import android.graphics.Color;
8+
import android.graphics.Paint;
9+
import android.graphics.Picture;
10+
import android.os.Build;
11+
import android.os.Environment;
12+
import android.support.annotation.NonNull;
13+
import android.support.annotation.Nullable;
14+
import android.text.TextUtils;
15+
import android.util.Log;
16+
import android.webkit.WebView;
17+
18+
import java.io.File;
19+
import java.io.FileOutputStream;
20+
import java.lang.ref.WeakReference;
21+
22+
import io.reactivex.Observable;
23+
import io.reactivex.android.schedulers.AndroidSchedulers;
24+
import io.reactivex.functions.Function;
25+
26+
/**
27+
* 截图工具
28+
* Created by ChenRui on 2017/10/10 0010 15:59.
29+
*/
30+
public final class ViewCaptureUtils {
31+
32+
private WeakReference<Context> mContextWeakReference;
33+
34+
public ViewCaptureUtils(Context context) {
35+
mContextWeakReference = new WeakReference<>(context);
36+
}
37+
38+
public Observable<File> capture(@NonNull WebView view, @Nullable final String path) {
39+
return Observable.just(view)
40+
.map(new Function<WebView, Bitmap>() {
41+
@Override
42+
public Bitmap apply(WebView view) throws Exception {
43+
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
44+
return convertViewToBitmapV19(view);
45+
}
46+
return convertViewToBitmap(view);
47+
}
48+
})
49+
.map(new Function<Bitmap, File>() {
50+
@Override
51+
public File apply(Bitmap bitmap) throws Exception {
52+
File file;
53+
if (TextUtils.isEmpty(path)) {
54+
// 生成临时文件
55+
String fileName = "temp" + System.currentTimeMillis() + ".jpg";
56+
if (mContextWeakReference.get() != null) {
57+
file = new File(mContextWeakReference.get().getExternalCacheDir(), fileName);
58+
} else {
59+
file = new File(Environment.getDataDirectory(), fileName);
60+
}
61+
} else {
62+
file = new File(path);
63+
}
64+
65+
if (file.exists()) {
66+
boolean res = file.delete();
67+
Log.d("rae", "删除文件:" + file.getPath() + ";是否成功:" + res);
68+
}
69+
70+
FileOutputStream os = new FileOutputStream(file);
71+
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, os);
72+
os.flush();
73+
os.close();
74+
return file;
75+
}
76+
}).observeOn(AndroidSchedulers.mainThread());
77+
}
78+
79+
// // 5.0 方式
80+
// private Bitmap convertViewToBitmap(WebView view) {
81+
// int height = (int) (view.getContentHeight() * view.getScale()); // 内容高度
82+
// if (height <= 0) return null;
83+
// Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), height, Bitmap.Config.ARGB_8888);
84+
// Canvas canvas = new Canvas(bitmap);
85+
// view.draw(canvas);
86+
// return bitmap;
87+
// }
88+
89+
/**
90+
* API 19 以下的
91+
*
92+
* @param view
93+
* @return
94+
*/
95+
@TargetApi(Build.VERSION_CODES.KITKAT)
96+
private Bitmap convertViewToBitmapV19(WebView view) {
97+
// 获取Picture对象
98+
Picture picture = view.capturePicture();
99+
// 得到图片的宽和高(没有reflect图片内容)
100+
int width = picture.getWidth();
101+
int height = picture.getHeight();
102+
if (width > 0 && height > 0) {
103+
// 创建位图
104+
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
105+
Canvas canvas = new Canvas(bitmap);
106+
picture.draw(canvas);
107+
return bitmap;
108+
}
109+
return null;
110+
}
111+
112+
private Bitmap convertViewToBitmap(WebView view) {
113+
114+
int height = (int) (view.getContentHeight() * view.getScale()); // 内容高度
115+
if (height <= 0) return null;
116+
117+
// 滚动截图
118+
view.setEnabled(false);
119+
// 记录当前位置
120+
int srcX = view.getScrollX();
121+
int srcY = view.getScrollY();
122+
123+
// 滚动到头部
124+
view.scrollTo(0, 0);
125+
int y = view.getScrollY();
126+
int h = view.getHeight(); // 屏幕高度
127+
128+
// 拼接图像
129+
Bitmap result = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888);
130+
Canvas canvas = new Canvas(result);
131+
canvas.drawColor(Color.WHITE);
132+
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
133+
paint.setColor(Color.WHITE);
134+
135+
Log.w("rae", "截图高度:" + height + "; 屏幕高度:" + h);
136+
do {
137+
view.scrollTo(0, y);
138+
Log.i("rae", "正在截图:" + y);
139+
Bitmap bmp = capturePicture(view);
140+
if (bmp == null) {
141+
Log.w("rae", "没有图像:" + y);
142+
continue;
143+
}
144+
canvas.drawBitmap(bmp, 0, y, paint);
145+
if (y == height) {
146+
break;
147+
}
148+
y += h;
149+
y = Math.min(height, y);
150+
Log.w("rae", "下一个截图---> " + y);
151+
} while (y <= height);
152+
153+
// 还原位置
154+
view.scrollTo(srcX, srcY);
155+
156+
return result;
157+
}
158+
159+
private Bitmap capturePicture(WebView v) {
160+
161+
boolean willNotCache = v.willNotCacheDrawing();
162+
v.setWillNotCacheDrawing(false);
163+
164+
// Reset the drawing cache background color to fully transparent
165+
// for the duration of this operation
166+
int color = v.getDrawingCacheBackgroundColor();
167+
v.setDrawingCacheBackgroundColor(0);
168+
169+
if (color != 0) {
170+
v.destroyDrawingCache();
171+
}
172+
v.buildDrawingCache();
173+
Bitmap cacheBitmap = v.getDrawingCache();
174+
if (cacheBitmap == null) {
175+
return null;
176+
}
177+
178+
// 压缩图片
179+
// Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
180+
181+
// Restore the view
182+
v.destroyDrawingCache();
183+
v.setWillNotCacheDrawing(willNotCache);
184+
v.setDrawingCacheBackgroundColor(color);
185+
186+
return cacheBitmap;
187+
}
188+
}

app/src/main/java/com/rae/cnblogs/widget/PlaceholderView.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class PlaceholderView extends FrameLayout implements SkinCompatSupportabl
3636
private View mRetryView;
3737
private View mContentView;
3838
private TextView mLoadingTextView;
39+
private String mLoadingText;
3940

4041
public PlaceholderView(Context context) {
4142
super(context);
@@ -97,6 +98,9 @@ private void initAttr(AttributeSet attrs) {
9798
mDefaultEmptyIcon = a.getDrawable(index);
9899
setEmptyIcon(mDefaultEmptyIcon);
99100
break;
101+
case R.styleable.PlaceholderView_loading_message:
102+
mLoadingText = a.getString(index);
103+
break;
100104
}
101105
}
102106
a.recycle();
@@ -120,7 +124,9 @@ private void switchMode(int mode) {
120124
*/
121125
public void loading() {
122126
show();
123-
mLoadingTextView.setText(R.string.loading);
127+
if (mLoadingText == null)
128+
mLoadingText = getContext().getString(R.string.loading);
129+
mLoadingTextView.setText(mLoadingText);
124130
mEmptyView.setVisibility(GONE);
125131
mLoadingView.setVisibility(VISIBLE);
126132
}

app/src/main/res/layout/activity_blog_content.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout
33
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
45
android:layout_width="match_parent"
56
android:layout_height="match_parent"
67
android:background="@color/white"
@@ -15,7 +16,8 @@
1516
<com.rae.cnblogs.widget.PlaceholderView
1617
android:id="@+id/placeholder"
1718
android:layout_width="match_parent"
18-
android:layout_height="match_parent">
19+
android:layout_height="match_parent"
20+
app:loading_message="">
1921

2022
<!--内容区域-->
2123
<FrameLayout

app/src/main/res/layout/fm_blog_comment.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
<com.rae.cnblogs.widget.RaeRecyclerView
1919
android:id="@+id/rec_blog_comment_list"
2020
android:layout_width="match_parent"
21-
android:layout_height="match_parent"/>
21+
android:layout_height="match_parent"
22+
android:layout_marginTop="@dimen/action_bar_height"/>
23+
2224
</com.rae.cnblogs.widget.PlaceholderView>
2325

2426

0 commit comments

Comments
 (0)