Android仿淘宝最新向上滚动广告条,flutter登录验证码图片

这篇博客介绍了如何在Android应用中实现仿淘宝的向上滚动广告条,使用了Glide图片加载库,并展示了相关代码实现。同时,文章提到了移动开发面试的相关知识。
摘要由CSDN通过智能技术生成

this.tipList = tipList;

curTipIndex = index;

updateTip(tv_tip_out);

updateTipAndPlayAnimation();

}

private void setNowPosition(int position) {

this.nowPosition = position;

}

private int getNowPosition() {

return nowPosition;

}

//获得当前滚动的位置

public int getNowTip() {

if (isListEmpty(tipList)) return -1;

return getNowPosition();

}

}

  • 滚动的文字布局
<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent”>

<TextView

android:id="@+id/text_View"

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_gravity=“center_vertical”

android:ellipsize=“end”

android:gravity=“center_vertical”

android:lines=“2”

android:text="@string/app_name"

android:textColor="#2F4F4F"

android:textSize=“16sp” />

  • 创建自动滚动的图片控件

引入图片加载框架:

compile ‘com.github.bumptech.glide:glide:3.7.0’

package tsou.cn.loopview.view;

import android.content.Context;

import android.util.AttributeSet;

import android.view.LayoutInflater;

import android.view.View;

import android.view.animation.Animation;

import android.view.animation.DecelerateInterpolator;

import android.view.animation.TranslateAnimation;

import android.widget.FrameLayout;

import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.List;

import tsou.cn.loopview.R;

import tsou.cn.loopview.bean.LooperBean;

public class LooperImageView extends FrameLayout {

private List tipList;

private int curTipIndex = 0;

private long lastTimeMillis;

private static final int ANIM_DELAYED_MILLIONS = 3 * 1000;

/**

  • 动画持续时长

*/

private static final int ANIM_DURATION = 1 * 1000;

private View tv_tip_out, tv_tip_in;

private Animation anim_out, anim_in;

private int nowPosition;

public LooperImageView(Context context) {

super(context);

initTipFrame();

initAnimation();

}

public LooperImageView(Context context, AttributeSet attrs) {

super(context, attrs);

initTipFrame();

initAnimation();

}

public LooperImageView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

initTipFrame();

initAnimation();

}

private void initTipFrame() {

tv_tip_out = newView();

tv_tip_in = newView();

addView(tv_tip_in);

addView(tv_tip_out);

}

private View newView() {

View inflate = LayoutInflater.from(getContext()).inflate(R.layout.item_looper_image, null);

return inflate;

}

private void initAnimation() {

anim_out = newAnimation(0, -1);

anim_in = newAnimation(1, 0);

anim_in.setAnimationListener(new Animation.AnimationListener() {

@Override

public void onAnimationStart(Animation animation) {

}

@Override

public void onAnimationRepeat(Animation animation) {

}

@Override

public void onAnimationEnd(Animation animation) {

updateTipAndPlayAnimationWithCheck();

}

});

}

private Animation newAnimation(float fromYValue, float toYValue) {

Animation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,

Animation.RELATIVE_TO_SELF, fromYValue, Animation.RELATIVE_TO_SELF, toYValue);

anim.setDuration(ANIM_DURATION);

anim.setStartOffset(ANIM_DELAYED_MILLIONS);

anim.setInterpolator(new DecelerateInterpolator());

return anim;

}

private void updateTipAndPlayAnimationWithCheck() {

if (System.currentTimeMillis() - lastTimeMillis < 1000) {

return;

}

lastTimeMillis = System.currentTimeMillis();

updateTipAndPlayAnimation();

}

private void updateTipAndPlayAnimation() {

if (curTipIndex % 2 == 0) {

updateTip(tv_tip_out);

tv_tip_in.startAnimation(anim_out);

tv_tip_out.startAnimation(anim_in);

this.bringChildToFront(tv_tip_in);

} else {

updateTip(tv_tip_in);

tv_tip_out.startAnimation(anim_out);

tv_tip_in.startAnimation(anim_in);

this.bringChildToFront(tv_tip_out);

}

}

private void updateTip(View tipView) {

final ImageView imageView = (ImageView) tipView.findViewById(R.id.image_view);

Glide.with(tipView.getContext())

.load(tipList.get(curTipIndex % tipList.size()).getUrl())

.placeholder(R.mipmap.app_loading_pic)

.error(R.mipmap.app_loading_pic)

.into(imageView);

getNextTip();

}

/**

  • 获取下一条消息

  • @return

*/

private LooperBean getNextTip() {

if (isListEmpty(tipList)) return null;

int nextPostion = curTipIndex++ % tipList.size();

if (tipList.size() == 1) {

setNowPosition(0);

} else if (nextPostion == 0) {

setNowPosition(tipList.size() - 1);

} else {

setNowPosition(nextPostion - 1);

}

return tipList.get(nextPostion);

}

public boolean isListEmpty(List list) {

return list == null || list.isEmpty();

}

public void setTipList(List tipList, int index) {

this.tipList = tipList;

curTipIndex = index;

updateTip(tv_tip_out);

updateTipAndPlayAnimation();

}

private void setNowPosition(int position) {

this.nowPosition = position;

}

private int getNowPosition() {

return nowPosition;

}

public int getNowTip() {

if (isListEmpty(tipList)) return -1;

return getNowPosition();

}

}

  • 滚动的图片布局
<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:orientation=“horizontal”>

<ImageView

android:id="@+id/image_view"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center_vertical”

android:scaleType=“fitXY”

android:src="@mipmap/app_loading_pic" />

  • 主页面布局
<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

tools:context=“tsou.cn.loopview.MainActivity”>

<RelativeLayout

android:id="@+id/rl_marquee_view"

android:layout_width=“match_parent”

android:layout_height=“80dp”

android:background="#ffffff"

android:padding=“5dp”>

<ImageView

android:id="@+id/image"

android:layout_width=“80dp”

android:layout_height=“80dp”

android:src="@mipmap/home_message" />

<tsou.cn.loopview.view.LooperImageView

android:id="@+id/marquee_image_view"

android:layout_width=“80dp”

android:layout_height=“match_parent”

android:layout_alignParentRight=“true”

android:layout_marginLeft=“5dp”

android:background="#fff" />

<tsou.cn.loopview.view.LooperTextView

android:id="@+id/marquee_text_view"

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:layout_marginLeft=“5dp”

android:layout_toLeftOf="@id/marquee_image_view"

android:layout_toRightOf="@id/image"

android:background="#fff" />

  • 实现滚动效果

package tsou.cn.loopview;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.RelativeLayout;

import com.squareup.otto.Subscribe;

import java.util.ArrayList;

import java.util.List;

import tsou.cn.loopview.bean.LooperBean;

import tsou.cn.loopview.otto.AppBus;

import tsou

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

.cn.loopview.otto.bean.Looper;

import tsou.cn.loopview.util.UIUtils;

import tsou.cn.loopview.view.LooperImageView;

import tsou.cn.loopview.view.LooperTextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private List looperBeenList;

private LooperImageView mMarqueeImageView;

private LooperTextView mMarqueeTextView;

private RelativeLayout mRlMarqueeView;

private int position;

@Override

protected void onDestroy() {

super.onDestroy();

AppBus.getInstance().unregister(this);

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

AppBus.getInstance().register(this);

initView();

looperBeenList = generateTips();

}

@Override

public void onStart() {

super.onStart();

mMarqueeImageView.setTipList(looperBeenList, position);

mMarqueeTextView.setTipList(looperBeenList, position);

}

@Subscribe

public void setPostion(Looper looper) {

this.position = looper.getPosition();

}

private void initView() {

mMarqueeImageView = (LooperImageView) findViewById(R.id.marquee_image_view);

mMarqueeTextView = (LooperTextView) findViewById(R.id.marquee_text_view);

mRlMarqueeView = (RelativeLayout) findViewById(R.id.rl_marquee_view);

mRlMarqueeView.setOnClickListener(this);

}

private List generateTips() {

List tips = new ArrayList<>();

for (int i = 0; i < 10; i++) {

LooperBean looperBean = new LooperBean();

looperBean.setId(i);

looperBean.setName(“huangxiaoguo” + i);

looperBean.setUrl(“https://unsplash.it/200/200?random&” + i);

looperBean.setPath(“http://blog.csdn.net/huangxiaoguo1”);

tips.add(looperBean);

}

return tips;

}

@Override

public void onClick(View v) {

switch (v.getId()) {

default:

break;

case R.id.rl_marquee_view:

position = mMarqueeTextView.getNowTip();

UIUtils.showToast(String.valueOf(position));

if (position > -1) {

WebViewActivity.actionStart(this, looperBeenList.get(position).getPath());

}

break;

}

}

}


  • 使用otto进行消息传递

引入otto

compile ‘com.squareup:otto:1.3.8’

初始化otto

package tsou.cn.loopview.otto;

import com.squareup.otto.Bus;

public class AppBus extends Bus {

private static AppBus bus;

private AppBus() {

}

public static AppBus getInstance() {

if (bus == null) {

bus = new AppBus();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值