Back to project page ScienceQuiz.
The source code is released under:
GNU General Public License
If you think the Android project ScienceQuiz listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.finger.sciencequiz; //ww w. j a va2s.com import android.content.Context; import android.graphics.Canvas; import android.util.Log; import android.view.Gravity; import android.widget.TextView; public class RotationTextView extends TextView { public RotationTextView (Context context) { super (context); } public RotationTextView (Context context, int[] gravity) { super (context); int l = gravity.length; for (int i = 1; i < l; i++) { if (Gravity.isVertical (gravity[i])) { vertical_grav = gravity[i]; } } } protected float rotation = 0f; protected float height; protected float width; protected int vertical_grav = Gravity.TOP; protected int[] padding; protected String[] sPadding; public static String LOG_TAG = "RotationTextView"; @Override public void onLayout (boolean changed, int l, int t, int r, int b) { if (changed) { calcPadding (sPadding); setPadding (padding); width = getWidth () / 2; switch (vertical_grav) { case Gravity.TOP: height = getPaddingTop () + (getLineCount () * getLineHeight () / 2); break; case Gravity.CENTER_VERTICAL: case Gravity.CENTER: height = getHeight () / 2; break; case Gravity.BOTTOM: height = getHeight () - getPaddingBottom () + (getLineCount () * getLineHeight () / 2); break; } } } @Override public void onDraw (Canvas canvas) { float angle = rotation; // Save the current matrix canvas.save (); // Rotate this View at its center canvas.rotate ( angle, width, height); // Draw it super.onDraw (canvas); // Restore to the previous matrix canvas.restore (); } public void setRot (float r) { rotation = r; } public void setPadding (String[] s) { sPadding = s; } protected void calcPadding (String[] s) { padding = new int[4]; try { for (int i = 0; i < 4; i++) { if (s[i].charAt (0) == 37) { padding[i] = Integer.parseInt (s[i].substring (1)); padding[i] *= getHeight () / 100; } else { padding[i] = Integer.parseInt (s[i]); } } } catch (NumberFormatException e) { Log.d (LOG_TAG, "Failed to set padding"); } } public void setPadding (int[] p) { setPadding (p[0], p[1], p[2], p[3]); } }