Android 文本上下滚动和图片闪烁


前言

这个是我前几天刚学的,文本上下滚动,外加图片闪烁效果
先来看下效果吧(gif图还没去了解…)

效果


提示:以下是本篇文章正文内容,下面案例可供参考

一、第一种方式

先在res目录下创建一个anim文件,里面用来存放动画效果

图2
先创建一个text_in.xml文件,代码为以下内容:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    //设置不保存播放完毕之后的画面
    android:fillAfter="true"
    //去掉好像没影响
    android:shareInterpolator="false"
    //允许在动画播放期间,调整播放内容在Z轴方向的顺序,
    //normal(0):正在播放的动画内容保持当前的Z轴顺序,
    //top(1):在动画播放期间,强制把当前播放的内容放到其他内容的上面;
    //bottom(-1):在动画播放期间,强制把当前播放的内容放到其他内容之下
    android:zAdjustment="top">

    <translate
        //动画执行时间
        android:duration="1000"
         //属性为动画起始时 Y坐标上的位置
        android:fromYDelta="100%p"
        //属性为动画结束时 Y坐标上的位置
        android:toYDelta="0" />
</set>

text_out.xml代码如下:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:shareInterpolator="false"
    android:zAdjustment="top"
    >
    <translate
        android:duration="1000"
        android:fromYDelta="0"
        android:toYDelta="-100%p" />
</set> 

具体的代码:

public class Fragment_Home extends Fragment {
    View mView;
    private ImageView img;
    private TextSwitcher text;
    //设置数组
    private String[] mAdvertisements;
    //相当于代号
    private final int HOME_AD_RESULT = 1;
    //控制循环次数
    private int mSwitcherCount = 0;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                // 设置滚动数据
                case HOME_AD_RESULT:
                    //进行除余
                    text.setText(mAdvertisements[mSwitcherCount % mAdvertisements.length]);
                    mSwitcherCount++;
                    //指定多少毫秒后发送消息。  sendMessage :立即发送消息
                    mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3000);
                    break;
            }

        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment__home, container, false);
        initView();
        initData();
        //设置图片效果
        initImg();
        return mView;
    }

    private void initImg() {
        //设置透明度从0-1
        AlphaAnimation alphaAnimation1 = new AlphaAnimation(0.0f, 1.0f);
        //设置多少秒执行一次
        alphaAnimation1.setDuration(1000);
        //设置闪烁的次数(INFINITE为无数次)
        alphaAnimation1.setRepeatCount(Animation.INFINITE);
        //动画结束后从头开始或从末尾开始
        // Animation.REVERSE(从末尾开始) Animation.RESTART(从头开始)
        alphaAnimation1.setRepeatMode(Animation.RESTART);
        //将动画设置在View上
        img.setAnimation(alphaAnimation1);
        //启动
        alphaAnimation1.start();
    }

    private void initData() {
        // 指 定 TextSwitcher 的 ViewFactory。
        text.setFactory(() -> {
            TextView tv = new TextView(getContext());
            //设置字体颜色
            tv.setTextColor(getResources().getColor(R.color.black));
            return tv;
        });
        //设置text的进场和出场动画
        text.setInAnimation(getContext(), R.anim.text_in);
        text.setOutAnimation(getContext(), R.anim.text_out);
        //设置滚动的数据
        mAdvertisements = new String[]{"第一次循环......", "第二次循环......", "第三次循环......", "第四次循环......",};
        //发送信息并启动Handler
        mHandler.sendEmptyMessage(HOME_AD_RESULT);
    }

    private void initView() {
        img = mView.findViewById(R.id.home_img);
        text = mView.findViewById(R.id.home_text);
    }
}

XML如下:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    tools:context=".home.Fragment_Home">
    <ImageView
        android:id="@+id/home_img"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/welcome" />

    <TextSwitcher
        android:id="@+id/home_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:textSize="12sp" />
</LinearLayout>

二、第二种方式

这种方式用到的控件是ViewFlipper(这种方式我也是无意之间看到的,我就试了一下,结果效果和第一种方式差不多)

xml布局代码:

<RelativeLayout 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"
    android:gravity="center"
    tools:context=".home.Fragment_Home">
    <ViewFlipper
        android:id="@+id/vf"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoStart="true"
        android:background="@drawable/back"
        android:flipInterval="3000"
        android:inAnimation="@anim/text_in"
        android:outAnimation="@anim/text_out" />
</RelativeLayout>

ViewFlipper的主要属性:

属性含义
autoStart自动开始播放
flipInterval间隔时间
inAnimation新条目进入的动画
outAnimation旧条目离开的动画

java代码部分就比较简单了

private void initData() {
    ViewFlipper vf= mView.findViewById(R.id.vf);
    //这里的listview_item布局里面就一个TextView,加上文字就可以了,我就不贴出来了
    View item1 = View.inflate(getContext(), R.layout.listview_item, null);
    View item2 = View.inflate(getContext(), R.layout.listview_item, null);
    View item3 = View.inflate(getContext(), R.layout.listview_item, null);
    vf.addView(item1);
    vf.addView(item2);
    vf.addView(item3);
}

总结

好了,这就是我今天想要分享的内容。有错误的地方请多多包涵

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值