Tuesday 11 October 2016

Text curve clockwise and anticlockwise in android


 



import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;

import com.ob.virtualproject.R;


public class AndroidDrawActivity extends Activity implements SeekBar.OnSeekBarChangeListener {

    // http://stackoverflow.com/questions/19043452/how-to-alter-the-output-of-android-canvas-drawtextonpath

    private static final String TAG = AndroidDrawActivity.class.getName();

    int mRadius;
    private LinearLayout llMainFrame;
    private MyView myView;

    private boolean mIsClock = true;
    private int rotation_degree = 180;
    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.text_curver_layout);

        SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(this);
        mRadius = seekBar.getProgress();

        llMainFrame = (LinearLayout) findViewById(R.id.tcl_llMainFrame);
        myView = new MyView(this);
        myView.setDrawParams();
        llMainFrame.addView(myView);

    }

    @SuppressLint("LongLogTag")
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        Log.d(TAG, "onProgressChanged:progress= "+progress);
      //  mRadius = seekBar.getProgress();
        mRadius = seekBar.getProgress() - 360;
        if(mRadius<=0 && mRadius>=-8){
            mRadius = -8;
        }

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        llMainFrame.removeAllViews();
        myView.setDrawParams();
        llMainFrame.addView(myView);

    }


    public class MyView extends View {


        private int left;
        private int top;
        private int right;
        private int bottom;
        private RectF oval;
        private Path mArc;
        private Paint mPaintText;
//        private String message ="Hemant Vitthalbhai Patel Android Developer at Office beacon, Vadodara";
        private String message ="Hemant Vitthalbhai Patel Android ";
        private Path textPath;

        public MyView(Context context) {
            super(context);

        }
        public void setDrawParams(){

            Log.d(TAG, "mRadius: "+ mRadius);
            Log.d(TAG, "right: "+right);
            Log.d(TAG, "bottom: "+bottom);

            mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaintText.setStyle(Paint.Style.FILL_AND_STROKE);
            mPaintText.setColor(Color.RED);
            mPaintText.setTextSize(50f);


        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override

        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            textPath = new Path();
            Paint tmpPaint = new Paint();
            Paint tmpStrokePaint = new Paint();


            int offsetX = 0;
            int offsetY = 0;

            int x = offsetX;
            int y = offsetY;
            String t;
            float kerning = 1.0f;
            float scale = 1.0f;
            float kerningValue;
            int textOffsetX = x;
            int textOffsetY = y;
            float innerOffsetX = 0.0f;
            float innerOffsetY = 0.0f;

            float totalWidth = ((float) getWidth()) * scale;

            tmpPaint.setAntiAlias(true);
            tmpPaint.setColor(Color.BLACK);
            tmpPaint.setTextSize(50 * scale);
            tmpPaint.setStyle(Paint.Style.FILL);
            tmpPaint.setShader(null);


            tmpStrokePaint.setAntiAlias(true);
            tmpStrokePaint.setTextSize(tmpPaint.getTextSize());
            tmpStrokePaint.setTypeface(tmpPaint.getTypeface());
            tmpStrokePaint.setTextAlign(tmpPaint.getTextAlign());
            tmpStrokePaint.setColor(Color.RED);
            tmpStrokePaint.setStrokeWidth(2 * scale);


                int startAngle;
                t = message.replace("\n", " ");
                int aCurvingAngle = mRadius + 6;
                if (mRadius >= 360) {
                    aCurvingAngle = 359;
                } else if (mRadius <= -360) {
                    aCurvingAngle = -359;
                }
                kerningValue = 0.0f;
                if (kerning != 0.0f) {
                    kerningValue = ((kerning * 20.0f) * scale) / ((float) (t.length() - 1));
                }
                float hOffset = 0.0f;
                float lineWidth = tmpPaint.measureText(t) + (((float) (t.length() - 1)) * kerningValue);
                if (tmpStrokePaint.getStrokeWidth() > 0.0f) {
                    lineWidth += tmpStrokePaint.getStrokeWidth() * 2.0f;
                    hOffset = 0.0f + tmpStrokePaint.getStrokeWidth();
                }
                float diameter = (float) (((double) ((360.0f * lineWidth) / ((float) Math.abs(aCurvingAngle)))) / 3.141592653589793d);
                textOffsetX = offsetX;
                textOffsetY = offsetY;


                    int baseWidth = 1;
                    int baseHeight = 1;

                    innerOffsetX = (((float) (getWidth() - baseWidth)) * 0.5f) * scale;
                    innerOffsetY = (((float) (getHeight() - baseHeight)) * 0.5f) * scale;

                    textOffsetX += (int) innerOffsetX;
                    textOffsetY += (int) innerOffsetY;
                
                float left = ((float) textOffsetX) + (((totalWidth - (2.0f * innerOffsetX)) - diameter) / 2.0f);
                float top = (float) textOffsetY;
           //    aCurvingAngle = aCurvingAngle * (-1); // Reverse curve
                if (aCurvingAngle > 0) {
                    startAngle = 270;
                } else {
                    top += (((((float) getHeight()) * scale) - (2.0f * innerOffsetY)) - diameter) - 10;
                    if (tmpStrokePaint.getStrokeWidth() > 0.0f) {
                        top -= tmpStrokePaint.getStrokeWidth();
                    }
                    startAngle = 90;
                }
                this.textPath.reset();
                Paint paint = new Paint();
                paint.setColor(Color.YELLOW);
                canvas.drawRect(new RectF(left, top, left + diameter, top + diameter),paint);
                RectF rectf = new RectF(left, top, left + diameter, top + diameter);
                this.textPath.addArc(rectf, (float) (startAngle - (aCurvingAngle / 2)), (float) aCurvingAngle);
                canvas.drawTextOnPath(t, this.textPath, hOffset, 0.0f, tmpPaint);
                /*int j;
                for (j = 0; j < t.length(); j++) {
                    String c = t.substring(j, j + 1);
                    if (tmpStrokePaint.getStrokeWidth() > 0.0f) {
                      //  canvas.drawTextOnPath(c, this.textPath, hOffset, 0.0f, tmpStrokePaint);
                    }

                    canvas.drawTextOnPath(c, this.textPath, hOffset, 0.0f, tmpPaint);
                    hOffset += tmpPaint.measureText(c, 0, 1) + kerningValue;

                }*/




            }
        }

}

// Text curve xml file 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_margin="20dp"
        android:id="@+id/tcl_llMainFrame"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:orientation="horizontal">


    </LinearLayout>

    <!--<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="startReverse"
        android:text="Reverse"
        />-->
    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar"
        android:max="720"
        android:progress="50"
        android:background="@color/blue_semi_transparent"
        />

</LinearLayout>


3 comments: