Handler+动画实现

新闻类app中文字上下轮播效果实现:借助handler和动画

package com.longyue.customtextview;

import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {
    private TextView tv_change;
    private boolean isloop = true;
    private String [] ss  = {"你好","我好","大家好","还有谁"};
    private int item = 0;
    private AnimationSet set = new AnimationSet(true);
    private AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f);
    private TranslateAnimation ta = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
            0, Animation.RELATIVE_TO_SELF, 2.0f,
            Animation.RELATIVE_TO_SELF, 0);
    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case 1:
                if(item<4){
                    if(isloop){
                     tv_change.setText(ss[item]);
                     tv_change.setAnimation(set);
                     tv_change.startAnimation(set);
                     item+=1;
                    }
                }else{
                    item = 0;
                    tv_change.setText(ss[item]);
                     tv_change.setAnimation(set);
                     tv_change.startAnimation(set);
                    item+=1;
                }
                break;
            }
        };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        tv_change.setAnimation(set);
        tv_change.startAnimation(set);
        //实现自动切换界面
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(isloop){
                  //系统时钟的睡眠方法---->电量的消耗很少。
                  SystemClock.sleep(2000);
                  handler.sendEmptyMessage(1);  
                }               
            }
        }).start();
    }
    private void initView() {
        tv_change  = (TextView) findViewById(R.id.tv_change);
        tv_change.setText(ss[0]);
        set.addAnimation(animation);
        set.addAnimation(ta);
        set.setDuration(1000);
        set.setRepeatMode(Animation.REVERSE);
    }
}

Demo下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值