Android开发启动页跳过倒计时效果
app启动页都会放些广告图,然后倒计时跳过。
一、思路:
不是很难,就用用个倒计时控制mHandler.sendEmptyMessageDelayed(WHAT_COUNT_DOWN, 100);
二、效果图:
三、关键代码:
public class MainActivity extends AppCompatActivity {
private static final int WHAT_COUNT_DOWN = 1;
private CircleProgress mCircleProgress;
private ImageView mIvAd;
private int mMaxProgressVal;
private int mCurProgressVal;
private List<AdBean> mAdList;
private int mAdIndex;
private int mInterval = 2000;
private Handler mHandler;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case WHAT_COUNT_DOWN:
updateCountDown();
break;
}
}
};
mCircleProgress = findViewById(R.id.progress);
mIvAd = findViewById(R.id.iv_ad);
//数据处理,一般是接口返回的,正常是不会有resImage本地图片的
mAdList = new ArrayList<>();
mAdList.add(new AdBean("","",R.mipmap.head1));
mAdList.add(new AdBean("","",R.mipmap.head2));
mAdList.add(new AdBean("","",R.mipmap.head3));
mAdList.add(new AdBean("","",R.mipmap.head5));
mMaxProgressVal = 4 * mInterval;
if (mCircleProgress != null) {
mCircleProgress.setMaxProgress(mMaxProgressVal);
}
if (mHandler != null) {
mIvAd.setImageResource(mAdList.get(3).getResImage());
mHandler.sendEmptyMessageDelayed(WHAT_COUNT_DOWN, 100);
}
}
private void updateCountDown() {
mCurProgressVal += 100;
if (mCurProgressVal > mMaxProgressVal) {
return;
}
if (mCircleProgress != null) {
mCircleProgress.setCurProgress(mCurProgressVal);
}
int index = mCurProgressVal / mInterval;
if (index < mAdList.size() && mAdIndex != index) {
mIvAd.setImageResource(mAdList.get(mAdIndex).getResImage());
mAdIndex = index;
}
if (mCurProgressVal < mMaxProgressVal) {
if (mHandler != null) {
mHandler.sendEmptyMessageDelayed(WHAT_COUNT_DOWN, 100);
}
} else if (mCurProgressVal == mMaxProgressVal) {
Toast.makeText(mContext,"倒计时完了,做下一步操作",Toast.LENGTH_SHORT).show();
}
}
}
四、项目demo源码结构图:
有问题或者需要完整源码的私信我