Android Open Source - TweeningTextView Tweening Text View






From Project

Back to project page TweeningTextView.

License

The source code is released under:

Apache License

If you think the Android project TweeningTextView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.kokalabs.tweening.textview;
/* w w w  . jav  a  2  s. c o  m*/
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Property;
import android.view.View;

import com.kokalabs.svg.CubicBezierCurve;
import com.kokalabs.svg.PointD;
import com.kokalabs.svg.SvgGlyph;
import com.kokalabs.svg.SvgGlyphTweenViaInterpolation;

public class TweeningTextView extends View {
    private static final double SCALE = 0.5;
    private static final Paint PAINT = paint();

    private static final Property<TweeningTextView, SvgGlyph> PATH_POINTS =
            new Property<TweeningTextView, SvgGlyph>(SvgGlyph.class, "path") {
                @Override
                public SvgGlyph get(TweeningTextView view) {
                    return view.path;
                }

                @Override
                public void set(TweeningTextView view, SvgGlyph value) {
                    view.path = value;
                    view.invalidate();
                }
            };

    private SvgGlyph path;

    private static Paint paint() {
        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setColor(Color.BLACK);
        p.setStrokeWidth(5.0f);
        p.setStyle(Paint.Style.FILL_AND_STROKE);
        return p;
    }

    public TweeningTextView(Context context) {
        super(context);
    }

    public TweeningTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TweeningTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawTweenedText(canvas);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        int widthWithoutPadding = width - getPaddingLeft() - getPaddingRight();
        int heightWithoutPadding = height - getPaddingTop() - getPaddingBottom();
        int maxWidth = (int) (heightWithoutPadding * SCALE);
        int maxHeight = (int) (widthWithoutPadding / SCALE);
        if (widthWithoutPadding > maxWidth) {
            width = maxWidth + getPaddingLeft() + getPaddingRight();
        } else {
            height = maxHeight + getPaddingTop() + getPaddingBottom();
        }
        setMeasuredDimension(width, height);
    }

    public ObjectAnimator animate(SvgGlyph start, SvgGlyph end) {
        return ObjectAnimator.ofObject(this, PATH_POINTS, new SvgGlyphTweenViaInterpolation(), start, end);
    }

    private void drawTweenedText(Canvas canvas) {
        if (path == null) {
            return;
        }
        int height = getMeasuredHeight();
        int width = getMeasuredWidth();
        Adjustment adjust = new Adjustment((float) (height > width ? width : height));

        Path toDraw = new Path();
        toDraw.reset();
        toDraw.setFillType(Path.FillType.EVEN_ODD);
        PointD last = new PointD(-1, -1);
        for (CubicBezierCurve c : path.getPath()) {
            if (last.x != c.startX || last.y != c.startY) {
                toDraw.close();
                toDraw.moveTo(adjust.d(c.startX), adjust.d(c.startY));
            }
            toDraw.cubicTo(
                    adjust.d(c.control1X), adjust.d(c.control1Y),
                    adjust.d(c.control2X), adjust.d(c.control2Y),
                    adjust.d(c.endX), adjust.d(c.endY));
            last = new PointD(c.endX, c.endY);
        }
        toDraw.close();
        canvas.drawPath(toDraw, PAINT);
    }

    private static class Adjustment {
        private final double minDimension;

        private Adjustment(double minDimension) {
            this.minDimension = minDimension;
        }

        private float d(double toAdjust) {
            return (float) (minDimension * toAdjust);
        }
    }
}




Java Source Code List

com.kokalabs.svg.CubicBezierCurve.java
com.kokalabs.svg.PointD.java
com.kokalabs.svg.SvgCommandAsCubicHandler.java
com.kokalabs.svg.SvgCommandHandler.java
com.kokalabs.svg.SvgGlyphParser.java
com.kokalabs.svg.SvgGlyphTweenViaInterpolation.java
com.kokalabs.svg.SvgGlyph.java
com.kokalabs.tweening.textview.TweeningTextView.java
com.kokalabs.tweening.textview.example.ApplicationTest.java
com.kokalabs.tweening.textview.example.Char.java
com.kokalabs.tweening.textview.example.TweeningActivity.java