Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

How to create a marquee effect for a text of smaller length not exceeding the screen size in Android?

I have been trying to give the marquee effect for the word HELLO in my application but android does not allow the same unless the length of the text exceeds the screen size. Is there a solution around it?

PS: As simple as this seems I haven't got my hands on ANY solution yet.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I used simple light weight of ticker like animation which I developed in my early Android days.

below is complete code.

Hope this helps.

Works for all API levels of Android

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setticker("Hello", this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public  void setticker(String text, Context contx) {
        if (text != "") {
            LinearLayout parent_layout = (LinearLayout) ((Activity) contx)
                    .findViewById(R.id.ticker_area);

            TextView view = new TextView(contx);
            view.setText(text);

            view.setTextColor(Color.BLACK);
            view.setTextSize(25.0F);
            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 width of the view
            float width = view.getMeasuredWidth();
            float height = view.getMeasuredHeight();

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

            view.setLayoutParams(new LinearLayout.LayoutParams((int) width,
                    (int) height, 1f));

            System.out.println("width and screenwidth are" + width + "/"
                    + screenWidth + "///" + view.getMeasuredWidth());

            // performs the calculation
            float toXDelta = width - (screenWidth - 0);

            // sets toXDelta to -300 if the text width is smaller that the
            // screen size
            if (toXDelta < 0) {
                toXDelta = 0 - screenWidth;// -300;
            } else {
                toXDelta = 0 - screenWidth - toXDelta;// -300 - toXDelta;
            }
            // Animation parameters
            Animation mAnimation = new TranslateAnimation(screenWidth,
                    toXDelta, 0, 0);
            mAnimation.setDuration(15000);
            mAnimation.setRepeatMode(Animation.RESTART);
            mAnimation.setRepeatCount(Animation.INFINITE);
            view.setAnimation(mAnimation);
            parent_layout.addView(view);
        }
    }


}

in activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/ticker_area"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#9CB1DD"
        android:orientation="horizontal" >
    </LinearLayout>
</RelativeLayout>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...