Animating a text in Android
Sidibe Ali Broma issues in jahbromo@yahoo.fr
In what follows we will make a scrolling text ticker in Android as Java ME.
Process: First create a project, our project contains two classes one of which extends Activity and another TextView.
Android integration of animation allows to animate the View. In this case we will lead a textview.
The idea is to have the news broken by Android: the text scrolls at a speed that veut.Contrairement to tag marked with a speed can not be controlled. Is just the same concept as in Java Ticker ME.
First we will create a class that extends TextView, just to inherit from this TextView. Here
this class:
The idea is to have the news broken by Android: the text scrolls at a speed that veut.Contrairement to tag marked with a speed can not be controlled. Is just the same concept as in Java Ticker ME.
First we will create a class that extends TextView, just to inherit from this TextView. Here
this class:
| class TextDefile |
|---|
| android.content.Context import, import android.content.res.ColorStateList; import android.content.res.TypedArray ; android.graphics.Canvas import, import android.graphics.Paint; android.graphics.Rect import, import android.graphics.drawable.Drawable; android.net.Uri import, import android.os.Handler ; import android.os.Message; import android.os.SystemClock; import android.util.AttributeSet; import android.widget.TextView; public class MTextDefile extends TextView { private static final int MSG_UPDATE = 1; private Paint mPaint; private long mOffset; private float[] mWidths; private String[] mTexts; private int mTotalWidth; private Drawable mDivider; private float mSpaceWidth; private Rect mDividerBounds; private long mStartTime; private int mDelay; private int mStep; public MTextDefile(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Ticker); mDivider = a.getDrawable(R.styleable.Ticker_divider); if (mDivider != null) { mDivider.setBounds(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight()); } mDelay = a.getInt(R.styleable.Ticker_delay, 150); mStep = a.getInt(R.styleable.Ticker_step, 2); a.recycle(); mPaint = new Paint(getPaint()); ColorStateList csl = getTextColors(); mPaint.setColor(csl.getDefaultColor()); mOffset = 0; if (mDivider != null) { mSpaceWidth = mPaint.measureText(" "); mDividerBounds = mDivider.getBounds(); } } public void setText(String[] as, boolean animate) { mTexts = new String[as.length]; mWidths = new float[as.length]; int i = 0; mTotalWidth = 0; for (String s : as) { mTexts[i] = s; if (mDivider != null) { mWidths[i] = (int) mPaint.measureText(s + " "); mWidths[i] += mDividerBounds.width(); } else { mWidths[i] = (int) mPaint.measureText(s + " "); } mTotalWidth += mWidths[i]; i++; } stopAnimating(); if (animate) { startAnimating(); } } public void setTickerText(String s, boolean animate) { String[] arr = { s }; setText(arr, animate); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); stopAnimating(); } @Override protected void onDraw(Canvas canvas) { if (mTotalWidth == 0) { return; } long time = SystemClock.uptimeMillis() - mStartTime; mOffset = mStep * time / mDelay; mOffset %= mTotalWidth; float y = -mPaint.ascent(); float x = -mOffset; int w = getWidth(); int h = getHeight (); while (x \u0026lt;w) {int i = 0; for (String s: mTexts) {if (x + mWidths [i]> 0) { canvas.drawText (s, x, y , mPaint); if (mDivider! = null) { canvas.save (); canvas.translate (x + mWidths [i] - mSpaceWidth - mDividerBounds.width (), (h - mDividerBounds.height ()) / 2); mDivider.draw (canvas); canvas.restore ();} } x + = mWidths [i]; i + +; }}} Handler mHandler = new Handler () { @ Override public void dispatchMessage (Message msg) { if (msg.what == MSG_UPDATE) { mHandler.sendEmptyMessageDelayed (MSG_UPDATE, mDelay) invalidate ();} }} public void startAnimating () { stopAnimating (); mStartTime SystemClock.uptimeMillis = (); mHandler. sendEmptyMessage (MSG_UPDATE);} stopAnimating public void () { mHandler.removeCallbacksAndMessages (null);} setlien public void (int a, Uri uri) { }} |
You see that this class uses to activities which are defined in the xml file in
Here are the contents of xml:
Firstly we have in res / anim animation that apply to our component: cycle.xml
| cycle.xml |
|---|
| xml version = "1.0" encoding = "utf-8"? cycleInterpolator xmlns: android = "http://schemas.android.com/apk/res/android" android: cycles = "17" |
attributes that define a kind speed that is the style, the delay between the movement of characters. It is used in class Builder as attributes when we declare our TextDefile in the layout.
etaptes After all this we will declare our TextDefile objects in the layout: I'll put three with different speeds: A top to bottom and another one in the middle, can be of different colors and also different behavior when the click .
| class TextDefile |
|---|
| ? Xml version = "1.0" encoding = "utf-8"? resources styleable declare-name = "ticker" attr name = "divider" format = "reference" attr name = "delay" format = "integer" attr name = "step" size = "integer" declare-styleable resources |
Finally here as used in our Ticker activity.
| The class TestActivity |
|---|
| package org.sidibe.blog.ticker; android.app.Activity import, import android.os.Bundle; public class TestActivity extends Activity { String [] text ={"Android Developper par jahbromo","SIDIBE Ali-Broma","http://jahbromo.blogspot.com"}; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Ticker ticker= (Ticker)findViewById(R.id.bas); Ticker tickers= (Ticker)findViewById(R.id.haut); ticker.setText(text, true); tickers.setText(text, true); } } |
Le resultat :
0 comments:
Post a Comment