hi, i'm trying to animate text, but strange behavior happened when i
tried to rotate and scale.
When rotating, the text width becoming wider and narrower randomly
When scaling, the text y position is going upper.
i've test with image, but image works perfectly. don't know why text
is not.
Here's my code, please take a look or run it (only one file is enough
to run the demo)
thanks
public class TestDraw extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout flContent = new FrameLayout(this);
flContent.setLayoutParams(new
LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
setContentView(flContent);
SurfaceView svPreviewer = new SurfaceDrawer(this);
svPreviewer.setLayoutParams(new LayoutParams
(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
flContent.addView(svPreviewer);
}
private class SurfaceDrawer extends SurfaceView implements
SurfaceHolder.Callback, Runnable {
private SurfaceHolder mSurfaceHolder;
private int mLayoutWidth;
private int mLayoutHeight;
private String mText = "Hello World! Hello Android!";
private Paint mFontPaint = new Paint();
private Paint mBackgroundPaint = new Paint();
private Rect mBackgroundRect = new Rect();
// thread
private Thread mThread;
private boolean mIsStopped = false;
private boolean mIsRunningMode = false;
private Lock mLock = new ReentrantLock();
private Condition mCond = mLock.newCondition();
public SurfaceDrawer(Context context) {
super(context);
mSurfaceHolder = getHolder();
mSurfaceHolder.addCallback(this);
mFontPaint.setColor(Color.WHITE);
mBackgroundPaint.setColor(Color.MAGENTA);
// set the rect for background
mFontPaint.getTextBounds(mText, 0, mText.length(),
mBackgroundRect);
mThread = new Thread(this);
mThread.start();
}
public void surfaceChanged(SurfaceHolder holder, int format, int
width,
int height) {
mLayoutWidth = width;
mLayoutHeight = height;
mLock.lock();
mIsRunningMode = true;
mCond.signal();
mLock.unlock();
}
public void surfaceCreated(SurfaceHolder holder) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
mLock.lock();
mIsStopped = true;
mIsRunningMode = false;
mCond.signal();
mLock.unlock();
try {
mThread.join();
} catch (Exception e) {
}
}
public void run() {
while (mIsStopped == false) {
mLock.lock();
try {
while (mIsRunningMode == false) {
mCond.await();
}
if (mIsStopped == true) {
mLock.unlock();
break;
}
DrawRotationAnimation();
DrawScalingAnimation();
} catch (InterruptedException e) {
e.printStackTrace();
}
mLock.unlock();
}
}
public void DrawRotationAnimation() {
Canvas c;
int centerX = mLayoutWidth / 2;
int centerY = mLayoutHeight / 2;
for (int i = 0; i < 360; i += 20) {
c = mSurfaceHolder.lockCanvas();
c.save();
c.rotate(i, centerX, centerY);
c.translate(centerX - (mBackgroundRect.width()
/ 2), centerY
- (mBackgroundRect.height() /
2));
c.drawColor(Color.BLACK);
c.drawRect(mBackgroundRect, mBackgroundPaint);
c.drawText(mText, 0, 0, mFontPaint);
c.restore();
mSurfaceHolder.unlockCanvasAndPost(c);
SystemClock.sleep(300);
}
}
public void DrawScalingAnimation() {
Canvas c;
int centerX = mLayoutWidth / 2;
int centerY = mLayoutHeight / 2;
for (float scale = 1; scale < 3; scale += 0.2) {
c = mSurfaceHolder.lockCanvas();
c.save();
c.scale(scale, scale, centerX, centerY);
c.translate(centerX - (mBackgroundRect.width()
/ 2), centerY
- (mBackgroundRect.height() /
2));
c.drawColor(Color.BLACK);
c.drawRect(mBackgroundRect, mBackgroundPaint);
c.drawText(mText, 0, 0, mFontPaint);
c.restore();
mSurfaceHolder.unlockCanvasAndPost(c);
SystemClock.sleep(300);
}
}
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---