`
abc20899
  • 浏览: 910105 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

TextView刷入与刷出的效果

阅读更多
//// attrs.xml    自定义属性

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <declare-styleable name="SlidingText">

       <attr name="animationDuration" format="integer" />

    </declare-styleable>

</resources>



///// main.xml  

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="fill_parent"

    xmlns:slidingtext="http://schemas.android.com/apk/res/com.testSildingTextView"

    android:layout_height="fill_parent">





<com.testSildingTextView.SlidingTextView

       android:id="@+id/sliding_textview" android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       slidingtext:animationDuration="500"

       android:layout_gravity="center">

       <TextView android:layout_width="fill_parent" android:gravity="center_horizontal"

           android:layout_height="wrap_content" android:text="sssssss" />

    </com.testSildingTextView.SlidingTextView>



</LinearLayout>



首先自定义名称xmlns:slidingtext=http://schemas.android.com/apk/res/com.testSildingTextView

这里使用了自定义的名称

slidingtext:animationDuration="500"





///// SlidingTextView

private String[] showTexts = new String[] { "ssssss", "aaaaaa", "bbbbbb" };    // 用来记录显示哪个字符串    private int count = 0;    private int mDuration;    private TextView text;    private int textWidth = 200;//获取自定义变量public SlidingTextView(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray a = context.obtainStyledAttributes(attrs,                R.styleable.SlidingText);        mDuration = a                .getInteger(R.styleable.SlidingText_animationDuration, 750);    }//设置要显示的字符串    public void setShowText(String[] showTexts){        this.showTexts=showTexts;    }    // 回调函数 界面初始化快结束时调用protected void onFinishInflate() {        super.onFinishInflate();        text = (TextView) this.getChildAt(0);        mHandler.postDelayed(appear, 1000);    }private Handler mHandler = new Handler() {        @Override        public void handleMessage(Message msg) {            // 1为出现,2为隐藏            switch (msg.arg1) {            case 1:                doAnimationOpen();                break;            case 2:                doAnimationClose();                break;            }        }    };public void doAnimationOpen() {        post(appear);    }    // 出现的效果    Runnable appear = new Runnable() {        public void run() {            TranslateAnimation animation;            int fromXDelta = 0, toXDelta = 0, fromYDelta = 0, toYDelta = 0;            int calculatedDuration = 0;            fromXDelta = textWidth;            toXDelta = 0;            calculatedDuration = mDuration * Math.abs(toXDelta - fromXDelta)                    / textWidth;            animation = new TranslateAnimation(fromXDelta, toXDelta,                    fromYDelta, toYDelta);            animation.setDuration(calculatedDuration);            animation.setAnimationListener(new AnimationListener() {                @Override                public void onAnimationStart(Animation animation) {                    if(showTexts.length!=0){                        count = (count + 1) % showTexts.length;                        text.setText(showTexts[count]);                    }                    text.setVisibility(VISIBLE);                }                @Override                public void onAnimationRepeat(Animation animation) {                }                @Override                public void onAnimationEnd(Animation animation) {                    mHandler.postDelayed(hide, 2500);                }            });            startAnimation(animation);        }    };    public void doAnimationClose() {        post(hide);    }    // 隐藏的效果    Runnable hide = new Runnable() {        public void run() {            TranslateAnimation animation;            int fromXDelta = 0, toXDelta = 0, fromYDelta = 0, toYDelta = 0;            int calculatedDuration = 0;            toXDelta = -1 * textWidth;            calculatedDuration = mDuration * Math.abs(toXDelta - fromXDelta)                    / textWidth;            animation = new TranslateAnimation(fromXDelta, toXDelta,                    fromYDelta, toYDelta);            animation.setDuration(calculatedDuration);            animation.setAnimationListener(new AnimationListener() {                @Override                public void onAnimationStart(Animation animation) {                }                @Override                public void onAnimationRepeat(Animation animation) {                }                //动画结束时  设置textview的状态                @Override                public void onAnimationEnd(Animation animation) {                    mHandler.postDelayed(appear, 500);                    text.setVisibility(INVISIBLE);                }            });            startAnimation(animation);        }    };
这个动画效果主要是每次开出一条线程来运行的,首次运行后等2点5秒,就将textview以动画效果向左运行,等0.5秒后从右边出现,动画结束后隐藏,不断循
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics