Skip to content

Commit

Permalink
Added Tag Text Styles (NORMAL, BOLD, ITALIC, BOLD_ITALIC) to the Libr…
Browse files Browse the repository at this point in the history
…ary. Added Style Changing Functionality to the Demo too.
  • Loading branch information
Jilberta committed Aug 30, 2016
1 parent 4ba49f9 commit 64d5d3e
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package net.wujingchao.android.view.simpletagimage;

import android.app.Activity;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
Expand Down Expand Up @@ -35,6 +37,7 @@ protected void onCreate(Bundle savedInstanceState) {
setUpTextSizeSeekBar();
setUpCornerDistanceSeekBar();
setUpTextColorSpinner();
setUpTextStyleSpinner();
setUpTextTag();
setUpTagBackgroundColor();
setUpTagWidth();
Expand Down Expand Up @@ -85,15 +88,15 @@ public void onStopTrackingTouch(SeekBar seekBar) {
}

private void setUpTextColorSpinner() {
final Map<String,Integer> colors = new LinkedHashMap<>();
colors.put("WHITE",Color.WHITE);
colors.put("RED",Color.RED);
colors.put("BLUE",Color.BLUE);
colors.put("CYAN",Color.CYAN);
colors.put("YELLOW",Color.YELLOW);
final Map<String, Integer> colors = new LinkedHashMap<>();
colors.put("WHITE", Color.WHITE);
colors.put("RED", Color.RED);
colors.put("BLUE", Color.BLUE);
colors.put("CYAN", Color.CYAN);
colors.put("YELLOW", Color.YELLOW);
Spinner mSpinner = (Spinner) findViewById(R.id.spinner_text_color);
final Object [] keys = colors.keySet().toArray();
final SpinnerAdapter mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,keys);
final Object[] keys = colors.keySet().toArray();
final SpinnerAdapter mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, keys);
mSpinner.setAdapter(mAdapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
Expand All @@ -109,6 +112,30 @@ public void onNothingSelected(AdapterView<?> parent) {
});
}

private void setUpTextStyleSpinner() {
final Map<String, Byte> styles = new LinkedHashMap<>();
styles.put("NORMAL", SimpleTagImageView.TEXT_STYLE_NORMAL);
styles.put("BOLD", SimpleTagImageView.TEXT_STYLE_BOLD);
styles.put("ITALIC", SimpleTagImageView.TEXT_STYLE_ITALIC);
styles.put("BOLD_ITALIC", SimpleTagImageView.TEXT_STYLE_BOLD_ITALIC);
Spinner mSpinner = (Spinner) findViewById(R.id.spinner_tag_text_style);
final Object[] keys = styles.keySet().toArray();
final SpinnerAdapter mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, keys);
mSpinner.setAdapter(mAdapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String key = (String) keys[position];
mSimpleTagImageView.setTagTextStyle(styles.get(key));
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
}

private void setUpTextTag() {
EditText mEt = (EditText) findViewById(R.id.tv);
mEt.addTextChangedListener(new TextWatcher() {
Expand All @@ -129,16 +156,16 @@ public void afterTextChanged(Editable s) {
});
}

private void setUpTagBackgroundColor(){
final Map<String,Integer> colors = new LinkedHashMap<>();
colors.put("Green",0xaf27CDC0);
colors.put("RED",Color.RED);
colors.put("BLUE",Color.BLUE);
colors.put("CYAN",Color.CYAN);
colors.put("YELLOW",Color.YELLOW);
private void setUpTagBackgroundColor() {
final Map<String, Integer> colors = new LinkedHashMap<>();
colors.put("Green", 0xaf27CDC0);
colors.put("RED", Color.RED);
colors.put("BLUE", Color.BLUE);
colors.put("CYAN", Color.CYAN);
colors.put("YELLOW", Color.YELLOW);
Spinner mSpinner = (Spinner) findViewById(R.id.spinner_background_color);
final Object [] keys = colors.keySet().toArray();
final SpinnerAdapter mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,keys);
final Object[] keys = colors.keySet().toArray();
final SpinnerAdapter mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, keys);
mSpinner.setAdapter(mAdapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
Expand Down Expand Up @@ -175,14 +202,14 @@ public void onStopTrackingTouch(SeekBar seekBar) {
}

private void setUpTagOrientation() {
final Map<String,Byte> orientations = new LinkedHashMap<>();
orientations.put("LEFT_TOP",SimpleTagImageView.LEFT_TOP);
orientations.put("RIGHT_TOP",SimpleTagImageView.RIGHT_TOP);
orientations.put("LEFT_BOTTOM",SimpleTagImageView.LEFT_BOTTOM);
orientations.put("RIGHT_BOTTOM",SimpleTagImageView.RIGHT_BOTTOM);
final Map<String, Byte> orientations = new LinkedHashMap<>();
orientations.put("LEFT_TOP", SimpleTagImageView.LEFT_TOP);
orientations.put("RIGHT_TOP", SimpleTagImageView.RIGHT_TOP);
orientations.put("LEFT_BOTTOM", SimpleTagImageView.LEFT_BOTTOM);
orientations.put("RIGHT_BOTTOM", SimpleTagImageView.RIGHT_BOTTOM);
Spinner mSpinner = (Spinner) findViewById(R.id.spinner_tag_orientation);
final Object [] keys = orientations.keySet().toArray();
final SpinnerAdapter mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,keys);
final Object[] keys = orientations.keySet().toArray();
final SpinnerAdapter mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, keys);
mSpinner.setAdapter(mAdapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
Expand All @@ -201,15 +228,15 @@ public void onNothingSelected(AdapterView<?> parent) {
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if(outState != null) {
outState.putString("tag_text",mSimpleTagImageView.getTagText());
if (outState != null) {
outState.putString("tag_text", mSimpleTagImageView.getTagText());
}
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if(savedInstanceState != null) {
if (savedInstanceState != null) {
mSimpleTagImageView.setTagText(savedInstanceState.getString("tag_text"));
}
}
Expand Down Expand Up @@ -237,13 +264,13 @@ public void onStopTrackingTouch(SeekBar seekBar) {
private void setUpScaleType() {
Spinner mSpinner = (Spinner) findViewById(R.id.spinner_tag_scale_type);
// final ImageView.ScaleType [] types = ImageView.ScaleType.values();
final ImageView.ScaleType [] types = {ImageView.ScaleType.FIT_XY};
final ImageView.ScaleType[] types = {ImageView.ScaleType.FIT_XY};
final SpinnerAdapter mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, types);
mSpinner.setAdapter(mAdapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG,types[position].toString());
Log.d(TAG, types[position].toString());
mSimpleTagImageView.setScaleType(types[position]);
}

Expand Down
15 changes: 14 additions & 1 deletion demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
simple:simple_tag_width="20dip"
simple:simple_tag_textColor="@android:color/white"
simple:simple_tag_enable="true"
simple:simple_tag_round_radius="20dp"/>
simple:simple_tag_round_radius="20dp"
simple:simple_tag_textStyle="NORMAL"/>

<ScrollView
android:layout_width="match_parent"
Expand Down Expand Up @@ -60,6 +61,18 @@
/>
</TableRow>

<TableRow>
<Spinner
android:layout_height="30dip"
android:layout_margin="5dp"
android:background="@android:color/darker_gray"
android:id="@+id/spinner_tag_text_style"/>
<TextView
android:layout_gravity="center_vertical"
android:text="TextStyle"
/>
</TableRow>

<TableRow>
<EditText
android:hint="Tag Content..."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
Expand All @@ -34,6 +35,14 @@ public class SimpleTagImageView extends ImageView {

public static final byte RIGHT_BOTTOM = 0x03;

public static final byte TEXT_STYLE_NORMAL = 0x00;

public static final byte TEXT_STYLE_BOLD = 0x01;

public static final byte TEXT_STYLE_ITALIC = 0x02;

public static final byte TEXT_STYLE_BOLD_ITALIC = 0x03;

private static final float THE_SQUARE_ROOT_OF_2 = (float) Math.sqrt(2);

private static final int DEFAULT_TAG_WIDTH = 20;
Expand All @@ -46,6 +55,8 @@ public class SimpleTagImageView extends ImageView {

private static final int DEFAULT_TAG_TEXT_COLOR = 0xFFFFFFFF;

private static final int DEFAULT_TAG_TEXT_STYLE = 0;

private float mCornerDistance;

private float mTagWidth;
Expand All @@ -60,6 +71,8 @@ public class SimpleTagImageView extends ImageView {

private int mTagTextSize;

private int mTagTextStyle;

private Paint mTextPaint;

private Rect mTagTextBound;
Expand Down Expand Up @@ -100,6 +113,7 @@ public SimpleTagImageView(Context context, AttributeSet attrs, int defStyleAttr)
mTagBackgroundColor = a.getColor(R.styleable.SimpleTagImageView_simple_tag_background_color,DEFAULT_TAG_BACKGROUND_COLOR);
mTagText = a.getString(R.styleable.SimpleTagImageView_simple_tag_text);
mTagTextSize = a.getDimensionPixelSize(R.styleable.SimpleTagImageView_simple_tag_textSize, dip2px(DEFAULT_TAG_TEXT_SIZE));
mTagTextStyle = a.getInteger(R.styleable.SimpleTagImageView_simple_tag_textStyle, DEFAULT_TAG_TEXT_STYLE);
mTagTextColor = a.getColor(R.styleable.SimpleTagImageView_simple_tag_textColor, DEFAULT_TAG_TEXT_COLOR);
mTagEnable = a.getBoolean(R.styleable.SimpleTagImageView_simple_tag_enable,true);
mRoundRadius = a.getDimensionPixelSize(R.styleable.SimpleTagImageView_simple_tag_round_radius,0);
Expand Down Expand Up @@ -174,6 +188,16 @@ public void setTagText(String tagText){
invalidate();
}

public int getTagTextStyle() {
return this.mTagTextStyle;
}

public void setTagTextStyle (int tagStyle){
if(this.mTagTextStyle == tagStyle)return;
this.mTagTextStyle = tagStyle;
invalidate();
}

public void setTagBackgroundColor(int tagBackgroundColor) {
if(this.mTagBackgroundColor == tagBackgroundColor)return;
this.mTagBackgroundColor = tagBackgroundColor;
Expand Down Expand Up @@ -274,6 +298,7 @@ protected void onDraw(@SuppressWarnings("NullableProblems") Canvas mCanvas) {
mCanvas.drawPath(mPath, mPaint);
mTextPaint.setColor(mTagTextColor);
mTextPaint.setTextSize(mTagTextSize);
mTextPaint.setTypeface(Typeface.defaultFromStyle(mTagTextStyle));
mTextPaint.setAntiAlias(true);
// 斜边长度
float hypotenuse = THE_SQUARE_ROOT_OF_2 * rDistance;
Expand Down
26 changes: 16 additions & 10 deletions simple-tag-imageview/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@
<resources>
<declare-styleable name="SimpleTagImageView">
<attr name="simple_corner_distance" format="dimension" />
<attr name="simple_tag_width" format="dimension"/>
<attr name="simple_tag_background_color" format="color"/>
<attr name="simple_tag_text" format="string"/>
<attr name="simple_tag_width" format="dimension" />
<attr name="simple_tag_background_color" format="color" />
<attr name="simple_tag_text" format="string" />
<attr name="simple_tag_textSize" format="dimension" />
<attr name="simple_tag_textColor" format="color"/>
<attr name="simple_tag_textColor" format="color" />
<attr name="simple_tag_textStyle" format="enum">
<enum name="NORMAL" value="0"/>
<enum name="BOLD" value="1"/>
<enum name="ITALIC" value="2"/>
<enum name="BOLD_ITALIC" value="3"/>
</attr>
<attr name="simple_tag_orientation" format="enum">
<enum name="left_top" value="0"/>
<enum name="right_top" value="1"/>
<enum name="left_bottom" value="2"/>
<enum name="right_bottom" value="3"/>
<enum name="left_top" value="0" />
<enum name="right_top" value="1" />
<enum name="left_bottom" value="2" />
<enum name="right_bottom" value="3" />
</attr>
<attr name="simple_tag_enable" format="boolean"/>
<attr name="simple_tag_round_radius" format="dimension"/>
<attr name="simple_tag_enable" format="boolean" />
<attr name="simple_tag_round_radius" format="dimension" />
</declare-styleable>
</resources>

0 comments on commit 64d5d3e

Please sign in to comment.