LineThroughTextView


Source link: https://gist.github.com/TheGreat0004/b0c0c5d80004b2d40eec

A simple line through text view. ie. ----------------- Hello, World --------------

Java file (5 KB): LineThroughTextView.java

package views;  import android.annotation.TargetApi; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Build; import android.text.TextUtils; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.util.TypedValue; import android.view.View;
/**  * A simple line through text view. ie.  ----------------- Hello, World --------------  * <p/>  * NOTE: only support one line text(not that long), support multiple lines do it by yourself.  *  * @author longkai  */ public class LineThroughTextView extends View {

  private String text;
  private int textColor;
  private float textSize;
  private int textPadding;
  private int lineHeight;
  private int lineColor;

private Paint mTextPaint;

public LineThroughTextView(Context context) {

this(context, null);

  
}

public LineThroughTextView(Context context, AttributeSet attrs) {

super(context, attrs);

bootstrap(context, attrs, 0, 0);

  
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
  public LineThroughTextView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

bootstrap(context, attrs, defStyleAttr, 0);

  
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public LineThroughTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

super(context, attrs, defStyleAttr, defStyleRes);

bootstrap(context, attrs, defStyleAttr, defStyleRes);

  
}

private void bootstrap(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LineThroughTextView, defStyleAttr, defStyleRes);

text = a.getString(R.styleable.LineThroughTextView_android_text);

textColor = a.getColor(R.styleable.LineThroughTextView_android_textColor, Color.BLACK);

DisplayMetrics metrics = getResources().getDisplayMetrics();

textSize = a.getDimension(R.styleable.LineThroughTextView_android_textSize,

  TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, metrics) / metrics.density);

textPadding = a.getDimensionPixelSize(R.styleable.LineThroughTextView_textPadding, 0);

lineHeight = a.getDimensionPixelSize(R.styleable.LineThroughTextView_lineHeight, 2);
 // default to one pixel

lineColor = a.getColor(R.styleable.LineThroughTextView_lineColor, Color.DKGRAY);

a.recycle();

 mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

  
}

@Override
  protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

if (!TextUtils.isEmpty(text)) {

 canvas.save();

  mTextPaint.setTextSize(textSize);

 mTextPaint.setColor(textColor);

 float textWidth = mTextPaint.measureText(text);

  int x = (int) ((getWidth() - textWidth) / 2);

 int y = (int) ((getHeight() - (mTextPaint.descent() + mTextPaint.ascent())) / 2);

  // draw text

 canvas.drawText(text, x, y, mTextPaint);

  // line y

 mTextPaint.setColor(lineColor);

 y = getHeight() / 2;

  // draw lines

 canvas.drawRect(getPaddingLeft(), y - lineHeight / 2, x - textPadding, y + lineHeight / 2, mTextPaint);

 canvas.drawRect(

x + textWidth + textPadding,

y - lineHeight / 2,

getWidth() - getPaddingRight(),

y + lineHeight / 2,

mTextPaint

 );

  canvas.restore();

}

  
}

@Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

// if text is empty, skip it

if (TextUtils.isEmpty(text)) {

 setMeasuredDimension(0, 0);

 return;

}

 // we only need to measure the text height

int height = (int) (textSize + getPaddingTop() + getPaddingBottom());

 setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), resolveSize(height, heightMeasureSpec));

  
}

public int getLineColor() {

return lineColor;
  
}

public void setLineColor(int lineColor) {

this.lineColor = lineColor;

invalidate();

  
}

public int getLineHeight() {

return lineHeight;
  
}

public void setLineHeight(int lineHeight) {

this.lineHeight = lineHeight;

invalidate();

  
}

public String getText() {

return text;
  
}

public void setText(String text) {

this.text = text;

invalidate();

  
}

public int getTextColor() {

return textColor;
  
}

public void setTextColor(int textColor) {

this.textColor = textColor;

invalidate();

  
}

public int getTextPadding() {

return textPadding;
  
}

public void setTextPadding(int textPadding) {

this.textPadding = textPadding;

invalidate();

  
}

public float getTextSize() {

return textSize;
  
}

public void setTextSize(float textSize) {

this.textSize = textSize;

requestLayout();

  
}
 
}

XML file (467 bytes): attrs.xml

<?xml version="1.0" encoding="utf-8"?> <resources>
  <declare-styleable name="LineThroughTextView">

<attr name="lineColor" format="color" />

<attr name="android_text" format="string" />

<attr name="android_textSize" format="dimension" />

<attr name="android_textColor" format="color" />

<attr name="textPadding" format="dimension" />

<attr name="lineHeight" format="dimension" />
  </declare-styleable> </resources>

XML file (471 bytes): layout.xml

<views.LineThroughTextView xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="wrap_context"

android:layout_weight="13"

android:text="@string/str_or"

android:textSize="35sp"

app:lineHeight="4dp"

app:android_text="---Data---"

app:textPadding="4dp"

app:android_textSize="30sp" />

Resources

This is library to easily use connection between android wear and handheld device.

A simple android library that lets user select a directory.

Android library to backport Material design and allow changing colors at run-time.

Small library that provides... bouncing dots. This feature is used in number of messaging apps (such as Hangouts or Messenger), and lately in Android TV (for example when connecting to Wifi).

A small library including an example app which uses the "floating label" pattern to show form validation.

RecyclerView adapter classes for managing multiple view types.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes