Skip to content

Commit

Permalink
enable setting for image render mode: mixed or grid
Browse files Browse the repository at this point in the history
  • Loading branch information
zfdang committed Jun 18, 2024
1 parent 1a45aaa commit def9990
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class MyPreferenceFragment extends PreferenceFragmentCompat {
CheckBoxPreference image_quality_control;
CheckBoxPreference login_with_verification;
CheckBoxPreference image_source_cdn;
CheckBoxPreference image_grid_mode;

CheckBoxPreference board_master_only;

CheckBoxPreference notification_control_mail;
Expand Down Expand Up @@ -189,6 +191,20 @@ public class MyPreferenceFragment extends PreferenceFragmentCompat {
}
});

image_grid_mode = (CheckBoxPreference) findPreference("setting_image_grid_mode");
image_grid_mode.setChecked(Settings.getInstance().isImageGridMode());
image_grid_mode.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean bImageGridMode = Settings.getInstance().isImageGridMode();
if (newValue instanceof Boolean) {
Boolean boolVal = (Boolean) newValue;
bImageGridMode = boolVal;
}
Settings.getInstance().setImageGridMode(bImageGridMode);
return true;
}
});

board_master_only = (CheckBoxPreference) findPreference("setting_board_master_only");
board_master_only.setChecked(Settings.getInstance().isBoardMasterOnly());
board_master_only.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,43 +77,68 @@ public void inflateContentViewGroup(ViewGroup viewGroup, TextView contentView, f
if (content.getType() == ContentSegment.SEGMENT_IMAGE) {
// Log.d("CreateView", "Image: " + content.getUrl());

// // Add the text layout to the parent layout
// WrapContentDraweeView image = (WrapContentDraweeView) inflater.inflate(R.layout.post_item_imageview, viewGroup, false);
// image.setImageFromStringURL(content.getUrl());

// use grid view to replace original imageview
ImageView image = (ImageView) inflater.inflate(R.layout.post_item_image, viewGroup, false);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
image.setPadding(5, 5, 5, 5);
Glide.with(mListener).load(content.getUrl()).into(image);
image.setTag(R.id.image_tag, content.getImgIndex());

// set onclicklistener
image.setTag(R.id.image_tag, content.getImgIndex());
image.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
int position = (int) v.getTag(R.id.image_tag);

Intent intent = new Intent(mListener, FSImageViewerActivity.class);

ArrayList<String> urls = new ArrayList<>();
List<Attachment> attaches = post.getAttachFiles();
for (Attachment attach : attaches) {
// load original image in FS image viewer
urls.add(attach.getOriginalImageSource());
if(Settings.getInstance().isImageGridMode()){
// use grid view to replace original imageview
ImageView image = (ImageView) inflater.inflate(R.layout.post_item_image, viewGroup, false);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
image.setPadding(5, 5, 5, 5);
Glide.with(mListener).load(content.getUrl()).into(image);
image.setTag(R.id.image_tag, content.getImgIndex());

// set onclicklistener
image.setTag(R.id.image_tag, content.getImgIndex());
image.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
int position = (int) v.getTag(R.id.image_tag);

Intent intent = new Intent(mListener, FSImageViewerActivity.class);

ArrayList<String> urls = new ArrayList<>();
List<Attachment> attaches = post.getAttachFiles();
for (Attachment attach : attaches) {
// load original image in FS image viewer
urls.add(attach.getOriginalImageSource());
}

intent.putStringArrayListExtra(SMTHApplication.ATTACHMENT_URLS, urls);
intent.putExtra(SMTHApplication.ATTACHMENT_CURRENT_POS, position);
mListener.startActivity(intent);
}
});

// add image to list, instead of adding to viewgroup directly
imgList.add(image);
} else {
// Add the text layout to the parent layout
// 采用混排模式,文字和图片混在一起
WrapContentDraweeView image = (WrapContentDraweeView) inflater.inflate(R.layout.post_item_imageview, viewGroup, false);
image.setImageFromStringURL(content.getUrl());

// set onclicklistener
image.setTag(R.id.image_tag, content.getImgIndex());
image.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
int position = (int) v.getTag(R.id.image_tag);

Intent intent = new Intent(mListener, FSImageViewerActivity.class);

ArrayList<String> urls = new ArrayList<>();
List<Attachment> attaches = post.getAttachFiles();
for (Attachment attach : attaches) {
// load original image in FS image viewer
urls.add(attach.getOriginalImageSource());
}

intent.putStringArrayListExtra(SMTHApplication.ATTACHMENT_URLS, urls);
intent.putExtra(SMTHApplication.ATTACHMENT_CURRENT_POS, position);
mListener.startActivity(intent);
}
});

intent.putStringArrayListExtra(SMTHApplication.ATTACHMENT_URLS, urls);
intent.putExtra(SMTHApplication.ATTACHMENT_CURRENT_POS, position);
mListener.startActivity(intent);
}
});

// Add the text view to the parent layout
// viewGroup.addView(image);
// Add the text view to the parent layout
viewGroup.addView(image);
}

// add image to list, instead of adding to viewgroup directly
imgList.add(image);
} else if (content.getType() == ContentSegment.SEGMENT_TEXT) {
// Log.d("CreateView", "Text: " + content.getSpanned().toString());

Expand All @@ -127,43 +152,46 @@ public void inflateContentViewGroup(ViewGroup viewGroup, TextView contentView, f
}
}

int index = imgList.size() - imgList.size() > 1 ? 2 : 1;
if (imgList.size() > 0) {
int size, width;

Display display = mListener.getWindowManager().getDefaultDisplay();
Point point = new Point();
display.getSize(point);
width = point.x;
size = imgList.size();

gridLayout = new GridLayout(mListener);
GridLayout.Spec rowSpec, columnSpec;

GridLayout.LayoutParams gridParams = null;
int remainder, i;
remainder = i = size % 3;
for (int k = 0; k < remainder; ++k) {
rowSpec = GridLayout.spec(0);
columnSpec = GridLayout.spec(k);
gridParams = new GridLayout.LayoutParams(rowSpec, columnSpec);
gridParams.setGravity(Gravity.LEFT);
gridParams.width = width / i;
gridParams.height = width / 2;
gridLayout.addView(imgList.get(k), gridParams);
}
viewGroup.addView(gridLayout, index++);

gridLayout = new GridLayout(mListener);
for (; i < size; ++i) {
rowSpec = GridLayout.spec((i - remainder) / 3);
columnSpec = GridLayout.spec((i - remainder) % 3);
gridParams = new GridLayout.LayoutParams(rowSpec, columnSpec);
gridParams.width = width / 3;
gridParams.height = width / 3;
gridLayout.addView(imgList.get(i), gridParams);
if(Settings.getInstance().isImageGridMode()) {
// 将图片统一以网格形式添加进去
int index = imgList.size() - imgList.size() > 1 ? 2 : 1;
if (imgList.size() > 0) {
int size, width;

Display display = mListener.getWindowManager().getDefaultDisplay();
Point point = new Point();
display.getSize(point);
width = point.x;
size = imgList.size();

gridLayout = new GridLayout(mListener);
GridLayout.Spec rowSpec, columnSpec;

GridLayout.LayoutParams gridParams = null;
int remainder, i;
remainder = i = size % 3;
for (int k = 0; k < remainder; ++k) {
rowSpec = GridLayout.spec(0);
columnSpec = GridLayout.spec(k);
gridParams = new GridLayout.LayoutParams(rowSpec, columnSpec);
gridParams.setGravity(Gravity.LEFT);
gridParams.width = width / i;
gridParams.height = width / 2;
gridLayout.addView(imgList.get(k), gridParams);
}
viewGroup.addView(gridLayout, index++);

gridLayout = new GridLayout(mListener);
for (; i < size; ++i) {
rowSpec = GridLayout.spec((i - remainder) / 3);
columnSpec = GridLayout.spec((i - remainder) % 3);
gridParams = new GridLayout.LayoutParams(rowSpec, columnSpec);
gridParams.width = width / 3;
gridParams.height = width / 3;
gridLayout.addView(imgList.get(i), gridParams);
}
viewGroup.addView(gridLayout, index);
}
viewGroup.addView(gridLayout, index);
}
}

Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/com/zfdang/zsmth_android/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,22 @@ public void setLoadOriginalImage(boolean bLoadOriginalImage) {
}
}

// render image in grid mode
private static final String IMAGE_GRID_MODE = "IMAGE_GRID_MODE";
private boolean bImageGridMode;

public boolean isImageGridMode() {
return bImageGridMode;
}

public void setImageGridMode(boolean bImageGridMode) {
if (this.bImageGridMode != bImageGridMode) {
this.bImageGridMode = bImageGridMode;
mEditor.putBoolean(IMAGE_GRID_MODE, this.bImageGridMode);
mEditor.commit();
}
}

// use normal login, or with verification
private static final String LOGIN_WITH_VERIFICATION = "LOGIN_WITH_VERIFICATION";
private boolean bLoginWithVerification;
Expand Down Expand Up @@ -485,6 +501,7 @@ private void initSettings() {
bLoadOriginalImage = mPreference.getBoolean(LOAD_ORIGINAL_IMAGE, true);
bLoginWithVerification = mPreference.getBoolean(LOGIN_WITH_VERIFICATION, true);
bImageSourceCDN = mPreference.getBoolean(IMAGE_SOURCE_CDN, true);
bImageGridMode = mPreference.getBoolean(IMAGE_GRID_MODE, true);

bBoardMasterOnly = mPreference.getBoolean(BOARD_MASTER_ONLY, false);

Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@
android:summaryOn="从CDN加载图片(推荐)"
android:title="图片来源"/>

<CheckBoxPreference
android:defaultValue="on"
android:key="setting_image_grid_mode"
android:summaryOff="图文混合"
android:summaryOn="图片以网格形式显示"
android:title="图文显示模式"/>

<CheckBoxPreference
android:defaultValue="on"
android:key="setting_board_master_only"
Expand Down

0 comments on commit def9990

Please sign in to comment.