Android 自定义跑马灯view

在这里插入图片描述

1.xml布局文件

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MarqueeActivity">
        <LinearLayout
            android:id="@+id/martian_tips_view"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:gravity="center_vertical"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp"
            android:orientation="horizontal">
    
            <ImageView
                android:id="@+id/fr_user_header"
                android:layout_width="15dp"
                android:layout_height="15dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="4dp"
                android:layout_gravity="center_vertical"
                android:src="@drawable/icon_laba" />
    
            <com.example.down.TipsTextSwitcher<!--替换成自己的包名-->
                xmlns:tipsTextSwitcher="http://schemas.android.com/apk/res-auto"
                android:id="@+id/tipsTextSwitcher"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_height="wrap_content"
                tipsTextSwitcher:textSize="14sp"/>
        </LinearLayout>
    </LinearLayout>

2.自定义属性attrs.xml

 <?xml version="1.0" encoding="utf-8"?>
        <resources>
            <declare-styleable name="tipsTextSwitcher">
                <attr name="textSize" format="dimension" />
                <attr name="gravity">
                    <flag name="right" value="0x01" />
                    <flag name="left" value="0x02" />
                    <flag name="center" value="0x03" />
                </attr>
            </declare-styleable>
        </resources>

3.动画文件anim:
fade_in_slide_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="350"
        android:fromYDelta="100%"
        android:toYDelta="0" />
    <alpha
        android:duration="350"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>

fade_out_slide_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="350"
        android:fromYDelta="0"
        android:toYDelta="-100%" />
    <alpha
        android:duration="350"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>

4.Switcher类文件:添加控件,动画时间,开始

   public class Switcher {
        private TipsTextSwitcher advTsView;
        private boolean isPaused;
        private int mDuration=3000;
    
        public Switcher()
        {}
    
        public Switcher(TipsTextSwitcher view, int duration)//控件,时长
        {
            this.advTsView = view;
            this.mDuration = duration;
        }
    
        public Switcher setDuration(int duration)
        {
            this.mDuration = duration;
            return this;
        }
    
        public Switcher attach(TipsTextSwitcher view)
        {
            this.pause();
            this.advTsView = view;
            return this;
        }
    
        public void start()
        {
            isPaused = false;
            if (this.advTsView != null)
            {
                hlUpdt.postDelayed(rbUpdt, mDuration);
            }
        }
    
        public void pause()
        {
            isPaused = true;
        }
    
        public Handler hlUpdt = new Handler();
    
        public Runnable rbUpdt = new Runnable(){
            @Override
            public void run()
            {
                if (!isPaused && advTsView != null)
                {
                    advTsView.next();
                    hlUpdt.postDelayed(this, mDuration);
                }
            }
        };
    }

5.TipsTextSwitcher类文件:自定义控件

  public class TipsTextSwitcher extends TextSwitcher {
        private Context mContext;
        private String[] mTexts = {};
        private int currentPos;
        private Callback mCallback = new Callback(){
            @Override
            public void onItemClick(int position){}
        };
    
        public TipsTextSwitcher(Context context, AttributeSet attrs)
        {
            super(context, attrs);
            this.mContext = context;
            int[] attrsArray = new int[] {
                    android.R.attr.textColor,
                    R.attr.textSize,
                    android.R.attr.inAnimation,
                    android.R.attr.outAnimation,
                    R.attr.gravity
            };
            TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray);
            final int textColor = ta.getColor(0, Color.BLACK);
            final float textSize = ta.getDimensionPixelSize(1, 20);
            final int animInRes = ta.getResourceId(2, R.anim.fade_in_slide_in);
            final int animOutRes = ta.getResourceId(3, R.anim.fade_out_slide_out);
            boolean right = (ta.getInt(4, 0) & 0x01) == 0x01;
            boolean left = (ta.getInt(4, 0) & 0x02) == 0x02;
            boolean center = (ta.getInt(4, 0) & 0x03) == 0x03;
            final int gravity = center? Gravity.CENTER:
                    (right?Gravity.RIGHT|Gravity.CENTER_VERTICAL:
                            (left?(Gravity.LEFT|Gravity.CENTER_VERTICAL)
                                    :Gravity.NO_GRAVITY)
                    );
            ta.recycle();
            this.setFactory(new ViewFactory() {
                public View makeView()
                {
                    TextView innerText = new TextView(mContext);
                    innerText.setGravity(gravity);
                    innerText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
                    innerText.setTextColor(textColor);
                    innerText.setSingleLine();
                    innerText.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View p1)
                        {
                            TipsTextSwitcher.this.onClick();
                        }
                    });
                    return innerText;
                }
            });
    
            Animation animIn = AnimationUtils.loadAnimation(mContext, animInRes);
            Animation animOut = AnimationUtils.loadAnimation(mContext, animOutRes);
    
            this.setInAnimation(animIn);
            this.setOutAnimation(animOut);
        }
    
        public void overrideText(String text){
            this.setText(text);
        }
    
        public void setAnim(int animInRes, int animOutRes){
            Animation animIn = AnimationUtils.loadAnimation(mContext, animInRes);
            Animation animOut = AnimationUtils.loadAnimation(mContext, animOutRes);
            this.setInAnimation(animIn);
            this.setOutAnimation(animOut);
        }
    
        public void setAnim(Animation animIn, Animation animOut){
            this.setInAnimation(animIn);
            this.setOutAnimation(animOut);
        }
    
        public void setTexts(String[] texts)
        {
            if (texts.length > 0)
            {
                this.mTexts = texts;
                this.currentPos = 0;
            }
            updateDisp();
        }
    
        public void setCallback(Callback callback){
            this.mCallback = callback;
        }
    
        public void next()
        {
            if (mTexts.length > 0)
            {
                if (currentPos < mTexts.length - 1)
                {
                    currentPos++;
                }
                else
                {
                    currentPos = 0;
                }
                updateDisp();
            }
        }
    
        public void previous()
        {
            if (mTexts.length > 0)
            {
                if (currentPos > 0)
                {
                    currentPos--;
                }
                else
                {
                    currentPos = mTexts.length - 1;
                }
                updateDisp();
            }
        }
    
        public interface Callback{//点击回调
            public void onItemClick(int position);
        }
    
        private void updateDisp()
        {
            this.setText(mTexts[ currentPos ]);
        }
    
        private void onClick(){
            mCallback.onItemClick(currentPos);
        }
    }

6.MarqueeActivity页面

 public class MarqueeActivity extends AppCompatActivity {
    
        private TipsTextSwitcher tipsTextSwitcher;
        private String[] marquees = new String[5];
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_marquee);
            tipsTextSwitcher = findViewById(R.id.tipsTextSwitcher);
            tipsTextSwitcher.setCallback(new TipsTextSwitcher.Callback() {
                @Override
                public void onItemClick(int position) {
                    Toast.makeText(MarqueeActivity.this,"点击",Toast.LENGTH_SHORT).show();
                }
            });
    
            for (int i = 0; i < marquees.length; i++) {
                marquees[i] = "第"+i+"条";
            }
            tipsTextSwitcher.setTexts(marquees);//设置数据,是数组类型
            Switcher switcher = new Switcher(tipsTextSwitcher,3000);
            switcher.start();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值