PowenKo, Android Tutorial 085-2, Animation,Animation_move

August 30, 2011   ·   0 Comments

package com.powenko.Tutorial_UI_TextView_Marquee_slideshow;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

public class Tutorial_UI_TextView_Marquee_slideshowActivity extends Activity {
	String mtext[]={"Apples","Bananas","Orange","Guava","Pears","Pomegranate"};
	private Handler handler;
    private int mClock=0;
    private TextView mTextView1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mTextView1 = (TextView)findViewById(R.id.textView1);

        handler = new Handler();
        handler.postDelayed(runner, 1);

    }

    final Runnable runner = new Runnable()
    {
        public void run()
        {
            mClock++;
            int t_len=mtext.length;
            if(mClock>=t_len){mClock=0;}
            mTextView1.setText(mtext[mClock]);
            Animation t_Animation=scrollingText(mTextView1, 600);
            mTextView1.clearAnimation();
            mTextView1.startAnimation(t_Animation);

            handler.postDelayed(this, 8000);
        }
    };

    public static Animation scrollingText(View view, float margin){

        Context context = view.getContext(); //gets the context of the view

                // measures the unconstrained size of the view
                // before it is drawn in the layout
        view.measure(View.MeasureSpec.UNSPECIFIED,
                             View.MeasureSpec.UNSPECIFIED);

                // takes the unconstrained wisth of the view
        float width = view.getMeasuredWidth();

                // gets the screen width
        float screenWidth = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();

                // perfrms the calculation
        float toXDelta = width - (screenWidth - margin);

                // sets toXDelta to 0 if the text width is smaller that the screen size
        if (toXDelta < 0) {toXDelta = 0; } else { toXDelta = 0 - toXDelta;}

                // Animation parameters
        Animation mAnimation = new TranslateAnimation(0, toXDelta, 0, 0);
        mAnimation.setDuration(8000);//15000);
        mAnimation.setRepeatMode(Animation.RESTART);
        mAnimation.setRepeatCount(Animation.INFINITE);

        return mAnimation;
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:lines="1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:textColor="#ffffff"
        android:text="Simple application that shows how to use marquee, with a long text" />
</RelativeLayout>

sample code:

Tutorial_UI_TextView_Marquee_slideshow

shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Android7

Amazon

adsbygoogle

Recent Posts

Categories

Amazon