From ad11e397fed3c9b3bdcb6c75aa039c7a5e9f0bf0 Mon Sep 17 00:00:00 2001 From: Hristo Hristov Date: Tue, 7 Mar 2017 10:35:53 +0200 Subject: [PATCH] Move several types from modules to widgets (#87) Add setBackground method in ViewHelper --- .../widgets/CustomTypefaceSpan.java | 46 +++++++++++++++++++ .../DisableUserInteractionListener.java | 15 ++++++ .../widgets/SegmentedBarColorDrawable.java | 17 +++++++ .../org/nativescript/widgets/ViewHelper.java | 8 ++++ 4 files changed, 86 insertions(+) create mode 100644 android/widgets/src/main/java/org/nativescript/widgets/CustomTypefaceSpan.java create mode 100644 android/widgets/src/main/java/org/nativescript/widgets/DisableUserInteractionListener.java create mode 100644 android/widgets/src/main/java/org/nativescript/widgets/SegmentedBarColorDrawable.java diff --git a/android/widgets/src/main/java/org/nativescript/widgets/CustomTypefaceSpan.java b/android/widgets/src/main/java/org/nativescript/widgets/CustomTypefaceSpan.java new file mode 100644 index 000000000..a2ec9c1e1 --- /dev/null +++ b/android/widgets/src/main/java/org/nativescript/widgets/CustomTypefaceSpan.java @@ -0,0 +1,46 @@ +package org.nativescript.widgets; + +import android.annotation.SuppressLint; +import android.graphics.Paint; +import android.graphics.Typeface; +import android.text.TextPaint; +import android.text.style.TypefaceSpan; + +/** + * Created by hhristov on 2/27/17. + */ + +@SuppressLint("ParcelCreator") +public class CustomTypefaceSpan extends TypefaceSpan { + private Typeface typeface; + + public CustomTypefaceSpan(String family, Typeface typeface) { + super(family); + this.typeface = typeface; + } + + public void updateDrawState(TextPaint ds) { + this.applyCustomTypeFace(ds); + } + + public void updateMeasureState(TextPaint paint) { + this.applyCustomTypeFace(paint); + } + + private void applyCustomTypeFace(TextPaint paint) { + final Typeface old = paint.getTypeface(); + final int oldStyle = (old == null) ? 0 : old.getStyle(); + + Typeface typeface = this.typeface; + int fake = oldStyle & ~typeface.getStyle(); + if ((fake & android.graphics.Typeface.BOLD) != 0) { + paint.setFakeBoldText(true); + } + + if ((fake & android.graphics.Typeface.ITALIC) != 0) { + paint.setTextSkewX(-0.25f); + } + + paint.setTypeface(typeface); + } +} diff --git a/android/widgets/src/main/java/org/nativescript/widgets/DisableUserInteractionListener.java b/android/widgets/src/main/java/org/nativescript/widgets/DisableUserInteractionListener.java new file mode 100644 index 000000000..e59aef48f --- /dev/null +++ b/android/widgets/src/main/java/org/nativescript/widgets/DisableUserInteractionListener.java @@ -0,0 +1,15 @@ +package org.nativescript.widgets; + +import android.view.MotionEvent; +import android.view.View; + +/** + * Created by hhristov on 2/22/17. + */ + +public final class DisableUserInteractionListener extends Object implements View.OnTouchListener { + @Override + public boolean onTouch(View view, MotionEvent motionEvent) { + return true; + } +} diff --git a/android/widgets/src/main/java/org/nativescript/widgets/SegmentedBarColorDrawable.java b/android/widgets/src/main/java/org/nativescript/widgets/SegmentedBarColorDrawable.java new file mode 100644 index 000000000..7ae4fec8d --- /dev/null +++ b/android/widgets/src/main/java/org/nativescript/widgets/SegmentedBarColorDrawable.java @@ -0,0 +1,17 @@ +package org.nativescript.widgets; + +import android.graphics.Paint; +import android.graphics.drawable.ColorDrawable; + +/** + * Created by hhristov on 2/23/17. + */ + +public class SegmentedBarColorDrawable extends ColorDrawable { + public void draw(android.graphics.Canvas canvas) { + Paint p = new Paint(); + p.setColor(this.getColor()); + p.setStyle(android.graphics.Paint.Style.FILL); + canvas.drawRect(0, this.getBounds().height() - 15, this.getBounds().width(), this.getBounds().height(), p); + } +} \ No newline at end of file diff --git a/android/widgets/src/main/java/org/nativescript/widgets/ViewHelper.java b/android/widgets/src/main/java/org/nativescript/widgets/ViewHelper.java index d1695f057..34b375b73 100644 --- a/android/widgets/src/main/java/org/nativescript/widgets/ViewHelper.java +++ b/android/widgets/src/main/java/org/nativescript/widgets/ViewHelper.java @@ -528,5 +528,13 @@ public class ViewHelper { textView.setLetterSpacing(value); } } + + public static void setBackground(android.view.View view, android.graphics.drawable.Drawable background) { + if (version >= 16) { + view.setBackground(background); + } else { + view.setBackgroundDrawable(background); + } + } } \ No newline at end of file