短视频带货源码, 文本上下滚动和图片闪烁
短视频带货源码, 文本上下滚动和图片闪烁
先创建一个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>
以上就是 短视频带货源码, 文本上下滚动和图片闪烁,更多内容欢迎关注之后的文章
浙公网安备 33010602011771号