创建一个文本并向下设置动画,直到它离开屏幕

时间:2014-03-22 10:08:49

标签: java android animation

我正在制作一个应用程序,我需要每秒创建一行文本,然后向下设置动画,直到它离开屏幕。这将永远这样下去。我可以使用哪种算法?

我使用canvas.drawtext来创建文本。

2 个答案:

答案 0 :(得分:0)

protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    canvas.drawText(text, x, y, paintObject);

            if(y< canvas.getheight()){
                y += 10;
            }else{
                y = 0;  
            }
            invalidate();
}

Invalidate使方法反复运行。不知道这是否有效,因为我自己是初学者。但是它会做什么,当它将离开屏幕时,文本将出现在上面,并将重复这样做。

答案 1 :(得分:0)

    try this..


    package com.example.animationsample;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.view.animation.Animation.AnimationListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity implements AnimationListener {

        TextView txtMessage;
        Button btnBlink, btnfadeout, btnfadein, btnzoomin, btnzoomout, btnmove,
                btnslideup, btnslidedown, btnroate, btnbounce, btnsequential,
                btntogether;

        // Animation
        Animation animFadein, animBlink, animfadeout, animzoomin, animzoomout,
                animmove, animslideup, animslidedown, animroate, animbounce,
                animsequential, animtogether;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            txtMessage = (TextView) findViewById(R.id.txt1);
            btnBlink = (Button) findViewById(R.id.btnblink);
            btnfadeout = (Button) findViewById(R.id.btnfadeout);
            btnfadein = (Button) findViewById(R.id.btnfadein);
            btnmove = (Button) findViewById(R.id.btnmove);
            btnzoomin = (Button) findViewById(R.id.btnzoomin);
            btnzoomout = (Button) findViewById(R.id.btnzoomout);
            btnroate = (Button) findViewById(R.id.btnrotate);
            btnslidedown = (Button) findViewById(R.id.btnsliddown);
            btnslideup = (Button) findViewById(R.id.btnslideup);
            btnsequential = (Button) findViewById(R.id.btnsquential);
            btntogether = (Button) findViewById(R.id.btntogether);
            btnbounce = (Button) findViewById(R.id.btnbounce);

            // load the animation
            animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.fade_in);
            animBlink = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.blink);
            animbounce = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.bounce);
            animfadeout = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.fade_out);
            animmove = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.move);
            animroate = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.rotate);
            animslideup = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.slide_up);
            animslidedown = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.slide_down);
            animsequential = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.squential);
            animtogether = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.together);
            animzoomin = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.zoom_in);
            animzoomout = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.zoom_out);

            // set animation listener
            animFadein.setAnimationListener(this);
            animfadeout.setAnimationListener(this);
            animzoomout.setAnimationListener(this);
            animsequential.setAnimationListener(this);
            animslidedown.setAnimationListener(this);
            animslideup.setAnimationListener(this);
            animbounce.setAnimationListener(this);
            animmove.setAnimationListener(this);
            animtogether.setAnimationListener(this);
            animroate.setAnimationListener(this);
            animBlink.setAnimationListener(this);

            // button click event
            btnBlink.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    txtMessage.setVisibility(View.VISIBLE);

                    txtMessage.startAnimation(animBlink);
                }
            });
            btnbounce.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    txtMessage.setVisibility(View.VISIBLE);

                    txtMessage.startAnimation(animbounce);
                }
            });
            btnfadein.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    txtMessage.setVisibility(View.VISIBLE);

                    txtMessage.startAnimation(animFadein);
                }
            });
            btnfadeout.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    txtMessage.setVisibility(View.VISIBLE);

                    txtMessage.startAnimation(animfadeout);
                }
            });
            btnsequential.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    txtMessage.setVisibility(View.VISIBLE);

                    txtMessage.startAnimation(animsequential);
                }
            });
            btnslidedown.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    txtMessage.setVisibility(View.VISIBLE);

                    txtMessage.startAnimation(animslidedown);
                }
            });
            btnslideup.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    txtMessage.setVisibility(View.VISIBLE);

                    txtMessage.startAnimation(animslideup);
                }
            });
            btnzoomin.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    txtMessage.setVisibility(View.VISIBLE);

                    txtMessage.startAnimation(animzoomin);
                }
            });
            btnzoomout.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    txtMessage.setVisibility(View.VISIBLE);

                    txtMessage.startAnimation(animzoomout);
                }
            });
            btnmove.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    txtMessage.setVisibility(View.VISIBLE);

                    txtMessage.startAnimation(animmove);
                }
            });
            btnroate.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    txtMessage.setVisibility(View.VISIBLE);

                    txtMessage.startAnimation(animroate);
                }
            });

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // Take any action after completing the animation

            // check for fade in animation
            if (animation == animFadein) {
                Toast.makeText(getApplicationContext(), "Animation Stopped",
                        Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

    }


    layout.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/btnrotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="37dp"
        android:text="rotate" />

    <Button
        android:id="@+id/btnsliddown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnrotate"
        android:layout_toLeftOf="@+id/btnmove"
        android:text="slidedown" />

    <Button
        android:id="@+id/btnsquential"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnsliddown"
        android:layout_alignLeft="@+id/btnsliddown"
        android:text="squential" />

    <Button
        android:id="@+id/btnfadein"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnsquential"
        android:layout_alignRight="@+id/btnzoomin"
        android:layout_marginBottom="26dp"
        android:text="fadein" />

    <Button
        android:id="@+id/btnmove"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnslideup"
        android:layout_marginLeft="53dp"
        android:layout_toRightOf="@+id/btnfadeout"
        android:text="move" />

    <Button
        android:id="@+id/btnzoomout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnmove"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="19dp"
        android:text="zoomout" />

    <Button
        android:id="@+id/btnslideup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnzoomout"
        android:layout_alignRight="@+id/btnmove"
        android:text="slideup" />

    <Button
        android:id="@+id/btnzoomin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnmove"
        android:layout_alignRight="@+id/btnmove"
        android:text="zoomin" />

    <Button
        android:id="@+id/btnbounce"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnsquential"
        android:layout_centerVertical="true"
        android:text="bounce" />

    <Button
        android:id="@+id/btnfadeout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnbounce"
        android:layout_alignLeft="@+id/btnblink"
        android:text="fadeout" />

    <Button
        android:id="@+id/btnblink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnfadeout"
        android:layout_alignRight="@+id/txt1"
        android:text="blink" />

    <Button
        android:id="@+id/btntogether"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnfadeout"
        android:layout_alignRight="@+id/btnfadein"
        android:text="together" />

</RelativeLayout>