公告動(dòng)畫

notice.gif

廢話區(qū):每次寫動(dòng)畫,都是奇葩的需求。這次是一個(gè)公告的動(dòng)畫,按理說就是一條條的往上走唄,但是,我們的公告“長”著呢,屏幕展示不完,咋辦,要先往左滾動(dòng),展示完了這一條信息之后再往上滾動(dòng)。
需求:一條信息,如果超出屏幕,就要是跑馬燈的效果,這個(gè)直接上原生的,沒有超出不滾動(dòng)。對(duì)于多條信息,如果超出屏幕,就要先往左移動(dòng),直到展示完畢,在往上面移動(dòng)。
這邊加上一個(gè)代碼里面獲取焦點(diǎn)的代碼,這個(gè)頁面已經(jīng)非常復(fù)雜了,然后你無法獲取到焦點(diǎn),那這個(gè)跑馬燈的效果是實(shí)現(xiàn)不了的,我這邊就是在代碼里面手動(dòng)加上獲取焦點(diǎn)的代碼:

tv.setText(mNoticeBeanList.get(0).getTitle());
tv.setFocusable(true);
tv.setFocusableInTouchMode(true);
tv.requestFocus();
package com.example.retrofit.studyretrofit;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

/**
 * Created by on 2017/3/16.
 */

public class MyNoticeLayout extends LinearLayout {

    private ViewFlipper viewSwitcher;
    private int indexSel = 0;
    private Display mDisplay;
    private WindowManager mWindowManager;
    private String[] tags;
    private int mMW;
    private TextView mTv;
    private Context mContext;
    private TextView mStudy_tv;

    public MyNoticeLayout(@NonNull Context context) {
        this(context,null);
    }

    public MyNoticeLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initView(mContext);
    }

    private void initView(Context context) {
        mTv = (TextView) findViewById(R.id.tv);
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        mDisplay = mWindowManager.getDefaultDisplay();
        viewSwitcher = (ViewFlipper) findViewById(R.id.viewswitcher);

        tags = new String[3];
        tags[0] = "公告信息:xxx門店開業(yè),滿5000元減500元,熱烈歡迎welcome to hahahha";
        tags[1] = "公告信息:xxx門店開業(yè),滿5000元減500元";
        tags[2] = "公告信息:xxx門店開業(yè),滿5000元減500元,熱烈歡迎1111222333";
        mTv.setVisibility(tags.length > 1 ? View.GONE: View.VISIBLE);
        viewSwitcher.setVisibility(tags.length > 1 ? View.VISIBLE : View.GONE);

        if(tags.length > 1){
            for(int i= 0; i<tags.length; i++){
                View view = View.inflate(mContext, R.layout.item_viewswitch,null);
                viewSwitcher.addView(view);
            }

            limitTiem();
        }else{
            mTv.setText(tags[0]);
        }
    }


    private void limitTiem() {
        //設(shè)置切入動(dòng)畫
        TranslateAnimation mAnimationTop = new TranslateAnimation(0,0,200,0);
        mAnimationTop.setDuration(2000);
        mAnimationTop.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                width();
                ObjectAnimator objectAnimatorTranslate = ObjectAnimator.ofFloat(mStudy_tv, "translationX", -mMW, 0);
                objectAnimatorTranslate.setDuration(1);
                objectAnimatorTranslate.start();

                Message message = Message.obtain();
                message.what = 1;
                mHandler.sendMessageDelayed(message, 3000);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });


        //設(shè)置切出動(dòng)畫
        TranslateAnimation mAnimationBottom = new TranslateAnimation(0,0,0,-200);
        mAnimationBottom.setDuration(2000);

        viewSwitcher.setInAnimation(mAnimationTop);
        viewSwitcher.setOutAnimation(mAnimationBottom);

        width();

        if(mMW<0){
            animation();
        }else{
            Message message = Message.obtain();
            message.what = 2;
            mHandler.sendMessageDelayed(message, 2000);
        }
    }

    public void width(){
        mStudy_tv = (TextView)viewSwitcher.getCurrentView().findViewById(R.id.viewswitcher_tv_one);
        String tag=tags[indexSel%tags.length];
        mStudy_tv.setText(tag);

        TextPaint paint = mStudy_tv.getPaint();
        int width = computeMaxStringWidth(tag, paint);
        mMW = mDisplay.getWidth()- width - UICommonUtil.dip2px(mContext, 20);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(UICommonUtil.dip2px(mContext,width+200),
                UICommonUtil.dip2px(mContext, 40));
        mStudy_tv.setLayoutParams(layoutParams);
    }


    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(1 == msg.what){
                if(mMW<0){
                    animation();
                }else{
                    viewSwitcher.showNext();
                    indexSel = indexSel+1;
                }
            }else if(2 == msg.what){
                viewSwitcher.showNext();
                indexSel = indexSel+1;
            }

        }
    };


    /**
     * 當(dāng)公告信息超出屏幕的時(shí)候,需要先往左移動(dòng)
     */
    public void animation(){

        TextView mStudy_tv = (TextView)viewSwitcher.getCurrentView().findViewById(R.id.viewswitcher_tv_one);
        //超出屏幕向左移動(dòng)
        ObjectAnimator objectAnimatorTranslate = ObjectAnimator.ofFloat(mStudy_tv, "translationX", 0, mMW);
        objectAnimatorTranslate.setDuration(2000);
        objectAnimatorTranslate.start();

        objectAnimatorTranslate.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                viewSwitcher.showNext();
                indexSel = indexSel+1;

            }
        });
    }


    /**
     * 計(jì)算textview的寬度
     * @param strings
     * @param p  這個(gè)paint 必須是 textview.getPaint(), 不能自己new出來,才能保證準(zhǔn)確性
     * @return
     */
    private int computeMaxStringWidth(String strings, Paint p) {
        float maxWidthF = 0.0f;
        float width = p.measureText(strings);
        maxWidthF = Math.max(width, maxWidthF);
        int maxWidth = (int) (maxWidthF + 0.5);
        return maxWidth;
    }
}

<?xml version="1.0" encoding="utf-8"?>
<com.example.retrofit.studyretrofit.MyNoticeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:singleLine="true"

    />

    <ViewFlipper
        android:id="@+id/viewswitcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none"/>

</com.example.retrofit.studyretrofit.MyNoticeLayout>

布局:item_viewswitch

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/viewswitcher_ll"
              android:layout_width="wrap_content"
              android:layout_height="40dp">
    <TextView
        android:id="@+id/viewswitcher_tv_one"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textSize="14sp"
        android:gravity="center_vertical"

    />
</LinearLayout>

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"

>

    <include
        android:id="@+id/notice_layout"
        layout="@layout/notice_layout"/>

</LinearLayout>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,172評(píng)論 25 708
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,360評(píng)論 0 17
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 47,166評(píng)論 22 665
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,424評(píng)論 4 61
  • 放假在家,我家老太太總會(huì)習(xí)慣性地問我,中午吃什么?晚上吃什么?大部分時(shí)候我的回答是,隨便。昨天我這么說的時(shí)候,老太...
    A陳胖子閱讀 746評(píng)論 0 2

友情鏈接更多精彩內(nèi)容