kopia lustrzana https://github.com/olgamiller/SSTVEncoder2
Create result Label in EditTextActivity in onStart and update it on any change
rodzic
24c5c1804b
commit
a2911345c5
|
@ -16,10 +16,11 @@ limitations under the License.
|
|||
package om.sstvencoder;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
|
@ -44,10 +45,8 @@ public class EditTextActivity extends AppCompatActivity
|
|||
|
||||
public static final int REQUEST_CODE = 101;
|
||||
public static final String EXTRA = "EDIT_TEXT_EXTRA";
|
||||
private EditText mEditText;
|
||||
private int mColor, mOutlineColor;
|
||||
private Label mLabel;
|
||||
private EditColorMode mEditColor;
|
||||
private float mTextSize, mOutlineSize;
|
||||
private FontFamilySet mFontFamilySet;
|
||||
private FontFamilySet.FontFamily mSelectedFontFamily;
|
||||
private List<String> mFontFamilyNameList;
|
||||
|
@ -58,7 +57,6 @@ public class EditTextActivity extends AppCompatActivity
|
|||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_edit_text);
|
||||
mEditColor = EditColorMode.None;
|
||||
mEditText = (EditText) findViewById(R.id.edit_text);
|
||||
mEditBold = (CheckBox) findViewById(R.id.edit_bold);
|
||||
mEditItalic = (CheckBox) findViewById(R.id.edit_italic);
|
||||
mEditOutline = (CheckBox) findViewById(R.id.edit_outline);
|
||||
|
@ -67,21 +65,36 @@ public class EditTextActivity extends AppCompatActivity
|
|||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
Label label = (Label) getIntent().getSerializableExtra(EXTRA);
|
||||
mEditText.setText(label.getText());
|
||||
initTextSizeSpinner(label.getTextSize());
|
||||
mEditBold.setChecked(label.getBold());
|
||||
mEditItalic.setChecked(label.getItalic());
|
||||
initFontFamilySpinner(label.getFamilyName());
|
||||
mLabel = ((Label) getIntent().getSerializableExtra(EXTRA)).getClone();
|
||||
initText();
|
||||
initTextSizeSpinner(mLabel.getTextSize());
|
||||
mEditBold.setChecked(mLabel.getBold());
|
||||
mEditItalic.setChecked(mLabel.getItalic());
|
||||
initFontFamilySpinner(mLabel.getFamilyName());
|
||||
updateBoldAndItalic();
|
||||
mEditOutline.setChecked(label.getOutline());
|
||||
initOutlineSizeSpinner(label.getOutlineSize());
|
||||
mEditOutline.setChecked(mLabel.getOutline());
|
||||
initOutlineSizeSpinner(mLabel.getOutlineSize());
|
||||
findViewById(R.id.edit_color).setBackgroundColor(mLabel.getForeColor());
|
||||
findViewById(R.id.edit_outline_color).setBackgroundColor(mLabel.getOutlineColor());
|
||||
}
|
||||
|
||||
mColor = label.getForeColor();
|
||||
findViewById(R.id.edit_color).setBackgroundColor(mColor);
|
||||
private void initText() {
|
||||
EditText editText = (EditText) findViewById(R.id.edit_text);
|
||||
editText.setText(mLabel.getText());
|
||||
editText.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
||||
}
|
||||
|
||||
mOutlineColor = label.getOutlineColor();
|
||||
findViewById(R.id.edit_outline_color).setBackgroundColor(mOutlineColor);
|
||||
@Override
|
||||
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
||||
mLabel.setText(charSequence.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable editable) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void initFontFamilySpinner(String familyName) {
|
||||
|
@ -96,7 +109,6 @@ public class EditTextActivity extends AppCompatActivity
|
|||
}
|
||||
|
||||
private void initTextSizeSpinner(float textSize) {
|
||||
mTextSize = textSize;
|
||||
Spinner spinner = (Spinner) findViewById(R.id.edit_text_size);
|
||||
spinner.setOnItemSelectedListener(this);
|
||||
String[] sizeList = new String[]{"Small", "Normal", "Large", "Huge"};
|
||||
|
@ -106,7 +118,6 @@ public class EditTextActivity extends AppCompatActivity
|
|||
}
|
||||
|
||||
private void initOutlineSizeSpinner(float outlineSize) {
|
||||
mOutlineSize = outlineSize;
|
||||
Spinner spinner = (Spinner) findViewById(R.id.edit_outline_size);
|
||||
spinner.setOnItemSelectedListener(this);
|
||||
String[] sizeList = new String[]{"Thin", "Normal", "Thick"};
|
||||
|
@ -119,7 +130,7 @@ public class EditTextActivity extends AppCompatActivity
|
|||
int position = (int) (textSize - 1f);
|
||||
if (0 <= position && position <= 3)
|
||||
return position;
|
||||
mTextSize = Label.TEXT_SIZE_NORMAL;
|
||||
mLabel.setTextSize(Label.TEXT_SIZE_NORMAL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -131,7 +142,7 @@ public class EditTextActivity extends AppCompatActivity
|
|||
int position = (int) (outlineSize * 2f / Label.OUTLINE_SIZE_NORMAL - 1f);
|
||||
if (0 <= position && position <= 2)
|
||||
return position;
|
||||
mOutlineSize = Label.OUTLINE_SIZE_NORMAL;
|
||||
mLabel.setOutlineSize(Label.OUTLINE_SIZE_NORMAL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -143,14 +154,15 @@ public class EditTextActivity extends AppCompatActivity
|
|||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
switch (parent.getId()) {
|
||||
case R.id.edit_text_size:
|
||||
mTextSize = positionToTextSize(position);
|
||||
mLabel.setTextSize(positionToTextSize(position));
|
||||
break;
|
||||
case R.id.edit_outline_size:
|
||||
mOutlineSize = positionToOutlineSize(position);
|
||||
mLabel.setOutlineSize(positionToOutlineSize(position));
|
||||
break;
|
||||
case R.id.edit_font_family:
|
||||
String displayName = mFontFamilyNameList.get(position);
|
||||
mSelectedFontFamily = mFontFamilySet.getFontFamilyFromDisplayName(displayName);
|
||||
mLabel.setFamilyName(mSelectedFontFamily.name);
|
||||
updateBoldAndItalic();
|
||||
break;
|
||||
default:
|
||||
|
@ -160,12 +172,16 @@ public class EditTextActivity extends AppCompatActivity
|
|||
|
||||
private void updateBoldAndItalic() {
|
||||
mEditBold.setEnabled(mSelectedFontFamily.bold);
|
||||
if (!mEditBold.isEnabled())
|
||||
if (!mEditBold.isEnabled()) {
|
||||
mEditBold.setChecked(false);
|
||||
mLabel.setBold(false);
|
||||
}
|
||||
|
||||
mEditItalic.setEnabled(mSelectedFontFamily.italic);
|
||||
if (!mEditItalic.isEnabled())
|
||||
if (!mEditItalic.isEnabled()) {
|
||||
mEditItalic.setChecked(false);
|
||||
mLabel.setItalic(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -188,13 +204,25 @@ public class EditTextActivity extends AppCompatActivity
|
|||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
public void onBoldClick(View view) {
|
||||
mLabel.setBold(mEditBold.isChecked());
|
||||
}
|
||||
|
||||
public void onItalicClick(View view) {
|
||||
mLabel.setItalic(mEditItalic.isChecked());
|
||||
}
|
||||
|
||||
public void onOutlineClick(View view) {
|
||||
mLabel.setOutline(mEditOutline.isChecked());
|
||||
}
|
||||
|
||||
public void onColorClick(View view) {
|
||||
showColorDialog(R.string.color, mColor);
|
||||
showColorDialog(R.string.color, mLabel.getForeColor());
|
||||
mEditColor = EditColorMode.Text;
|
||||
}
|
||||
|
||||
public void onOutlineColorClick(View view) {
|
||||
showColorDialog(R.string.outline_color, mOutlineColor);
|
||||
showColorDialog(R.string.outline_color, mLabel.getOutlineColor());
|
||||
mEditColor = EditColorMode.Outline;
|
||||
}
|
||||
|
||||
|
@ -210,12 +238,12 @@ public class EditTextActivity extends AppCompatActivity
|
|||
public void onColorSelected(DialogFragment fragment, int color) {
|
||||
switch (mEditColor) {
|
||||
case Text:
|
||||
mOutlineColor = color;
|
||||
findViewById(R.id.edit_color).setBackgroundColor(mOutlineColor);
|
||||
mLabel.setForeColor(color);
|
||||
findViewById(R.id.edit_color).setBackgroundColor(color);
|
||||
break;
|
||||
case Outline:
|
||||
mOutlineColor = color;
|
||||
findViewById(R.id.edit_outline_color).setBackgroundColor(mOutlineColor);
|
||||
mLabel.setOutlineColor(color);
|
||||
findViewById(R.id.edit_outline_color).setBackgroundColor(color);
|
||||
break;
|
||||
}
|
||||
mEditColor = EditColorMode.None;
|
||||
|
@ -228,23 +256,8 @@ public class EditTextActivity extends AppCompatActivity
|
|||
|
||||
private void done() {
|
||||
Intent intent = new Intent();
|
||||
intent.putExtra(EXTRA, getLabel());
|
||||
intent.putExtra(EXTRA, mLabel);
|
||||
setResult(RESULT_OK, intent);
|
||||
finish();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Label getLabel() {
|
||||
Label label = new Label();
|
||||
label.setText(mEditText.getText().toString());
|
||||
label.setTextSize(mTextSize);
|
||||
label.setFamilyName(mSelectedFontFamily.name);
|
||||
label.setItalic(mEditItalic.isChecked());
|
||||
label.setBold(mEditBold.isChecked());
|
||||
label.setForeColor(mColor);
|
||||
label.setOutline(mEditOutline.isChecked());
|
||||
label.setOutlineSize(mOutlineSize);
|
||||
label.setOutlineColor(mOutlineColor);
|
||||
return label;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ package om.sstvencoder.TextOverlay;
|
|||
import android.graphics.Color;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public class Label implements Serializable {
|
||||
public static final float TEXT_SIZE_NORMAL = 2f;
|
||||
|
@ -122,4 +123,15 @@ public class Label implements Serializable {
|
|||
public void setOutlineColor(int color) {
|
||||
mOutlineColor = color;
|
||||
}
|
||||
|
||||
public Label getClone() {
|
||||
Label clone = new Label();
|
||||
try {
|
||||
for (Field field : getClass().getDeclaredFields()) {
|
||||
field.set(clone, field.get(this));
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,6 +47,7 @@
|
|||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="0.3"
|
||||
android:onClick="onItalicClick"
|
||||
android:text="@string/italic"/>
|
||||
|
||||
<CheckBox
|
||||
|
@ -54,6 +55,7 @@
|
|||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="0.3"
|
||||
android:onClick="onBoldClick"
|
||||
android:text="@string/bold"/>
|
||||
|
||||
<LinearLayout
|
||||
|
@ -95,6 +97,7 @@
|
|||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="0.3"
|
||||
android:onClick="onOutlineClick"
|
||||
android:text="@string/outline"/>
|
||||
|
||||
<Spinner
|
||||
|
|
Ładowanie…
Reference in New Issue