android 自定义ViewAnimator文字轮播广告控件——自定义控件学习(三)

android 自定义ViewAnimator文字轮播广告控件——自定义控件学习(三)

经过上一篇文章自定义图片轮播,应该能够感受到自定义控件的好处了。下面在做一个ViewAnimator实现文字轮播广告的控件。直接上货:
1、主要的ViewAnimatorWordComponent.java
package com.example.administrator.wordanimationcomponent;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.GetChars;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewAnimator;

/**
 * Created by Administrator on 2016-09-26.
 */
public class ViewAnimatorWordComponent extends RelativeLayout {
    private ViewAnimator viewAnimator;
    private final int MSG_CODE = 0x667;
    private final int TIMER_INTERVAL = 3000;
    private String[] strings;

    public void setStrings(String[] strings) {
        this.strings = strings;
        if(strings != null){
            for (int i = 0; i < strings.length; i++) {
                TextView textView = new TextView(getContext());
                textView.setText(strings[i]);
                //任意设置你的文字样式,在这里
                textView.setTextColor(getResources().getColor(android.R.color.holo_purple));
                textView.setTextSize(30);
                viewAnimator.addView(textView,i);
            }
        }
    }

    public ViewAnimatorWordComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
        viewAnimator = new ViewAnimator(getContext());
        viewAnimator.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        addView(viewAnimator);
        Message message = handler.obtainMessage(MSG_CODE);
        handler.sendMessageDelayed(message,TIMER_INTERVAL);
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == MSG_CODE){
                viewAnimator.setOutAnimation(getContext(),R.anim.slide_out_up);
                viewAnimator.setInAnimation(getContext(),R.anim.slide_in_down);
                viewAnimator.showNext();
                Message message = handler.obtainMessage(MSG_CODE);
                handler.sendMessageDelayed(message,TIMER_INTERVAL);
            }
        }
    };

}

2、需要使用的资源:
android 自定义ViewAnimator文字轮播广告控件——自定义控件学习(三)_第1张图片

slide_in_down.xml


    

slide_in_up.xml


    

slide_out_down.xml


    

slide_out_up.xml


    

3、调用,布局文件:



    

    

4、调用,MainActivity.java
package com.example.administrator.wordanimationcomponent;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    private ViewAnimatorWordComponent viewAnimatorWordComponent;
    private String[] strings = new String[]{
            "如果你真想得到一些知识",
            "最好过滤一下你的信息",
            "否则你只是在别人的思考中",
            "得意着——《暗时间》"
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewAnimatorWordComponent = (ViewAnimatorWordComponent)this.findViewById(R.id.wordAnimator);
        viewAnimatorWordComponent.setStrings(strings);
    }
}

5、下面是运行结果:

android 自定义ViewAnimator文字轮播广告控件——自定义控件学习(三)_第2张图片
最后,附上源码:点我下载,不要你的积分哦!!!

你可能感兴趣的:(Android)