Commit 16605465 authored by Sendtion's avatar Sendtion Committed by GitHub

Merge pull request #2 from bravekingzhang/master

增加动态计算插入图片的高度
parents 23536c25 71be65e9
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MarkdownProjectSettings">
<PreviewSettings splitEditorLayout="SPLIT" splitEditorPreview="PREVIEW" useGrayscaleRendering="false" zoomFactor="1.0" maxImageWidth="0" showGitHubPageIfSynced="false" allowBrowsingInPreview="false" synchronizePreviewPosition="true" highlightPreviewType="NONE" highlightFadeOut="5" highlightOnTyping="true" synchronizeSourcePosition="true">
<PanelProvider>
<provider providerId="com.vladsch.idea.multimarkdown.editor.swing.html.panel" providerName="Default - Swing" />
</PanelProvider>
</PreviewSettings>
<ParserSettings>
<PegdownExtensions>
<option name="ABBREVIATIONS" value="false" />
<option name="ANCHORLINKS" value="true" />
<option name="ATXHEADERSPACE" value="true" />
<option name="AUTOLINKS" value="true" />
<option name="DEFINITIONS" value="false" />
<option name="FENCED_CODE_BLOCKS" value="true" />
<option name="FOOTNOTES" value="false" />
<option name="HARDWRAPS" value="false" />
<option name="INSERTED" value="false" />
<option name="QUOTES" value="false" />
<option name="RELAXEDHRULES" value="true" />
<option name="SMARTS" value="false" />
<option name="STRIKETHROUGH" value="true" />
<option name="SUBSCRIPT" value="false" />
<option name="SUPERSCRIPT" value="false" />
<option name="SUPPRESS_HTML_BLOCKS" value="false" />
<option name="SUPPRESS_INLINE_HTML" value="false" />
<option name="TABLES" value="true" />
<option name="TASKLISTITEMS" value="true" />
<option name="TOC" value="false" />
<option name="WIKILINKS" value="true" />
</PegdownExtensions>
<ParserOptions>
<option name="COMMONMARK_LISTS" value="false" />
<option name="DUMMY" value="false" />
<option name="EMOJI_SHORTCUTS" value="true" />
<option name="FLEXMARK_FRONT_MATTER" value="false" />
<option name="GFM_TABLE_RENDERING" value="true" />
<option name="GITBOOK_URL_ENCODING" value="false" />
<option name="GITHUB_EMOJI_URL" value="false" />
<option name="GITHUB_LISTS" value="true" />
<option name="GITHUB_WIKI_LINKS" value="true" />
<option name="JEKYLL_FRONT_MATTER" value="false" />
<option name="SIM_TOC_BLANK_LINE_SPACER" value="true" />
</ParserOptions>
</ParserSettings>
<HtmlSettings headerTopEnabled="false" headerBottomEnabled="false" bodyTopEnabled="false" bodyBottomEnabled="false" embedUrlContent="false" addPageHeader="true">
<GeneratorProvider>
<provider providerId="com.vladsch.idea.multimarkdown.editor.swing.html.generator" providerName="Default Swing HTML Generator" />
</GeneratorProvider>
<headerTop />
<headerBottom />
<bodyTop />
<bodyBottom />
</HtmlSettings>
<CssSettings previewScheme="UI_SCHEME" cssUri="" isCssUriEnabled="false" isCssTextEnabled="false" isDynamicPageWidth="true">
<StylesheetProvider>
<provider providerId="com.vladsch.idea.multimarkdown.editor.swing.html.css" providerName="Default Swing Stylesheet" />
</StylesheetProvider>
<ScriptProviders />
<cssText />
</CssSettings>
<HtmlExportSettings updateOnSave="false" parentDir="$ProjectFileDir$" targetDir="$ProjectFileDir$" cssDir="" scriptDir="" plainHtml="false" imageDir="" copyLinkedImages="false" imageUniquifyType="0" targetExt="" useTargetExt="false" noCssNoScripts="false" linkToExportedHtml="true" exportOnSettingsChange="true" regenerateOnProjectOpen="false" />
<LinkMapSettings>
<textMaps />
</LinkMapSettings>
</component>
</project>
\ No newline at end of file
......@@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="JDK" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
......@@ -3,10 +3,7 @@
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/XRichText.iml" filepath="$PROJECT_DIR$/XRichText.iml" />
<module fileurl="file://$PROJECT_DIR$/XRichTextDemo.iml" filepath="$PROJECT_DIR$/XRichTextDemo.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
<module fileurl="file://$PROJECT_DIR$/xrichtext/xrichtext.iml" filepath="$PROJECT_DIR$/xrichtext/xrichtext.iml" />
<module fileurl="file://$PROJECT_DIR$/xrichtext/xrichtext.iml" filepath="$PROJECT_DIR$/xrichtext/xrichtext.iml" />
</modules>
</component>
......
package com.sendtion.xrichtext;
import android.animation.LayoutTransition;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
......@@ -7,6 +8,7 @@ import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
......@@ -32,6 +34,7 @@ public class RichTextEditor extends ScrollView {
private OnClickListener btnListener; // 图片右上角红叉按钮监听器
private OnFocusChangeListener focusListener; // 所有EditText的焦点监听listener
private EditText lastFocusEdit; // 最近被聚焦的EditText
private LayoutTransition mTransitioner; // 只在图片View添加或remove时,触发transition动画
private int editNormalPadding = 0; //
private int disappearingImageIndex = 0;
......@@ -51,9 +54,10 @@ public class RichTextEditor extends ScrollView {
allLayout = new LinearLayout(context);
allLayout.setOrientation(LinearLayout.VERTICAL);
//allLayout.setBackgroundColor(Color.WHITE);
setupLayoutTransitions();
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
allLayout.setPadding(50,15,50,15);//设置间距,防止生成图片时文字太靠边,不能用margin,否则有黑边
allLayout.setPadding(50, 15, 50, 15);//设置间距,防止生成图片时文字太靠边,不能用margin,否则有黑边
addView(allLayout, layoutParams);
// 2. 初始化键盘退格监听
......@@ -99,6 +103,33 @@ public class RichTextEditor extends ScrollView {
lastFocusEdit = firstEdit;
}
/**
* 初始化transition动画
*/
private void setupLayoutTransitions() {
mTransitioner = new LayoutTransition();
allLayout.setLayoutTransition(mTransitioner);
mTransitioner.addTransitionListener(new LayoutTransition.TransitionListener() {
@Override
public void startTransition(LayoutTransition transition,
ViewGroup container, View view, int transitionType) {
}
@Override
public void endTransition(LayoutTransition transition,
ViewGroup container, View view, int transitionType) {
if (!transition.isRunning()
&& transitionType == LayoutTransition.CHANGE_DISAPPEARING) {
// transition动画结束,合并EditText
// mergeEditText();
}
}
});
mTransitioner.setDuration(300);
}
public int dip2px(Context context, float dipValue) {
float m = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * m + 0.5f);
......@@ -107,8 +138,7 @@ public class RichTextEditor extends ScrollView {
/**
* 处理软键盘backSpace回退事件
*
* @param editTxt
* 光标所在的文本输入框
* @param editTxt 光标所在的文本输入框
*/
private void onBackspacePress(EditText editTxt) {
int startSelection = editTxt.getSelectionStart();
......@@ -142,8 +172,7 @@ public class RichTextEditor extends ScrollView {
/**
* 处理图片叉掉的点击事件
*
* @param view
* 整个image对应的relativeLayout view
* @param view 整个image对应的relativeLayout view
* @type 删除类型 0代表backspace删除 1代表按红叉按钮删除
*/
private void onImageCloseClick(View view) {
......@@ -152,17 +181,17 @@ public class RichTextEditor extends ScrollView {
List<EditData> dataList = buildEditData();
EditData editData = dataList.get(disappearingImageIndex);
//Log.i("", "editData: "+editData);
if (editData.imagePath != null){
if (editData.imagePath != null) {
SDCardUtil.deleteFile(editData.imagePath);
}
allLayout.removeView(view);
}
public void clearAllLayout(){
public void clearAllLayout() {
allLayout.removeAllViews();
}
public int getLastIndex(){
public int getLastIndex() {
int lastEditIndex = allLayout.getChildCount();
return lastEditIndex;
}
......@@ -220,10 +249,10 @@ public class RichTextEditor extends ScrollView {
// 如果EditText非空且光标不在最顶端,则需要添加新的imageView和EditText
lastFocusEdit.setText(editStr1);
String editStr2 = lastEditStr.substring(cursorIndex).trim();
if (editStr2.length() == 0){
if (editStr2.length() == 0) {
editStr2 = " ";
}
if (allLayout.getChildCount() - 1 == lastEditIndex ) {
if (allLayout.getChildCount() - 1 == lastEditIndex) {
addEditTextAtIndex(lastEditIndex + 1, editStr2);
}
......@@ -246,10 +275,8 @@ public class RichTextEditor extends ScrollView {
/**
* 在特定位置插入EditText
*
* @param index
* 位置
* @param editStr
* EditText显示的文字
* @param index 位置
* @param editStr EditText显示的文字
*/
public void addEditTextAtIndex(final int index, CharSequence editStr) {
EditText editText2 = createEditText("", EDIT_PADDING);
......@@ -266,15 +293,19 @@ public class RichTextEditor extends ScrollView {
final RelativeLayout imageLayout = createImageLayout();
DataImageView imageView = (DataImageView) imageLayout.findViewById(R.id.edit_imageView);
Glide.with(getContext()).load(imagePath).crossFade().centerCrop().into(imageView);
//imageView.setImageBitmap(bmp);//这里改用Glide加载图片
//imageView.setBitmap(bmp);//这句去掉,保留下面的图片地址即可,优化图片占用
imageView.setAbsolutePath(imagePath);//保留这句,后面保存数据会用
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);//裁剪剧中
// 调整imageView的高度,根据宽度来调整高度
//int imageHeight = allLayout.getWidth() * bmp.getHeight() / bmp.getWidth();
Bitmap bmp = BitmapFactory.decodeFile(imagePath);
int imageHeight = 500;
if (bmp != null) {
imageHeight = allLayout.getWidth() * bmp.getHeight() / bmp.getWidth();
bmp.recycle();
}
// TODO: 17/3/1 调整图片高度,这里是否有必要,如果出现微博长图,可能会很难看
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, 500);//设置图片固定高度
LayoutParams.MATCH_PARENT, imageHeight);//设置图片固定高度
lp.bottomMargin = 10;
imageView.setLayoutParams(lp);
......@@ -284,8 +315,7 @@ public class RichTextEditor extends ScrollView {
/**
* 根据view的宽度,动态缩放bitmap尺寸
*
* @param width
* view的宽度
* @param width view的宽度
*/
public Bitmap getScaledBitmap(String filePath, int width) {
BitmapFactory.Options options = new BitmapFactory.Options();
......@@ -313,7 +343,6 @@ public class RichTextEditor extends ScrollView {
} else if (itemView instanceof RelativeLayout) {
DataImageView item = (DataImageView) itemView.findViewById(R.id.edit_imageView);
itemData.imagePath = item.getAbsolutePath();
//itemData.bitmap = item.getBitmap();//去掉这个防止bitmap一直被占用,导致内存溢出
}
dataList.add(itemData);
}
......@@ -324,6 +353,5 @@ public class RichTextEditor extends ScrollView {
public class EditData {
public String inputStr;
public String imagePath;
public Bitmap bitmap;
}
}
......@@ -43,7 +43,7 @@ public class RichTextView extends ScrollView {
//allLayout.setBackgroundColor(Color.WHITE);//去掉背景
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
allLayout.setPadding(50,15,50,15);//设置间距,防止生成图片时文字太靠边
allLayout.setPadding(50, 15, 50, 15);//设置间距,防止生成图片时文字太靠边
addView(allLayout, layoutParams);
LinearLayout.LayoutParams firstEditParam = new LinearLayout.LayoutParams(
......@@ -61,15 +61,16 @@ public class RichTextView extends ScrollView {
/**
* 清除所有的view
*/
public void clearAllLayout(){
public void clearAllLayout() {
allLayout.removeAllViews();
}
/**
* 获得最后一个子view的位置
*
* @return
*/
public int getLastIndex(){
public int getLastIndex() {
int lastEditIndex = allLayout.getChildCount();
return lastEditIndex;
}
......@@ -100,10 +101,8 @@ public class RichTextView extends ScrollView {
/**
* 在特定位置插入EditText
*
* @param index
* 位置
* @param editStr
* EditText显示的文字
* @param index 位置
* @param editStr EditText显示的文字
*/
public void addTextViewAtIndex(final int index, CharSequence editStr) {
TextView textView = createTextView("", EDIT_PADDING);
......@@ -126,7 +125,12 @@ public class RichTextView extends ScrollView {
imageView.setAbsolutePath(imagePath);
// 调整imageView的高度
int imageHeight = allLayout.getWidth() * bmp.getHeight() / bmp.getWidth();
int imageHeight = 500;
if (bmp != null) {
imageHeight = allLayout.getWidth() * bmp.getHeight() / bmp.getWidth();
// 使用之后,还是回收掉吧
bmp.recycle();
}
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, imageHeight);
lp.bottomMargin = 10;
......@@ -138,8 +142,7 @@ public class RichTextView extends ScrollView {
/**
* 根据view的宽度,动态缩放bitmap尺寸
*
* @param width
* view的宽度
* @param width view的宽度
*/
public Bitmap getScaledBitmap(String filePath, int width) {
BitmapFactory.Options options = new BitmapFactory.Options();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment