效果图:
功能介绍
自动轮播指定的图片,并有倒计时,点击倒计时自动跳过(模拟跳过广告功能),倒计时自动计算到0时,实现自动跳转;给ImageView绑定图片使用了background和src两种属性方法。
具体实现方法
在src文件夹下的drawable文件添加几张事先准备好的图片,以备后续使用。
1.transition_drawable:
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/img1"/>
<item android:drawable="@drawable/img2"/>
</transition>
2.activity布局:
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="利用线程在倒计时中无限循环图片切换的次数"
android:textSize="20sp"/>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img_view"
android:scaleType="centerInside"
android:layout_width="381dp"
android:layout_height="216dp" />
<TextView
android:id="@+id/tv_time_count"
android:layout_margin="10dp"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="3"/>
</FrameLayout>
<TextView
android:id="@+id/tv_title_img_view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"/>
<ImageView
android:id="@+id/img_view1"
android:layout_width="381dp"
android:layout_height="216dp"
android:scaleType="centerInside"
android:background="@drawable/transition_drawable" />
<TextView
android:id="@+id/tv_title_img_view2"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"/>
<ImageView
android:id="@+id/img_view2"
android:layout_width="381dp"
android:layout_height="216dp"
android:scaleType="centerInside"
android:src="@drawable/transition_drawable" />
</LinearLayout>
</ScrollView>
</LinearLayout>
3.activity代码:
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
public class ActTransitionDrawable extends AppCompatActivity {
@BindView(R.id.img_view1)
ImageView imgView1;
@BindView(R.id.img_view2)
ImageView imgView2;
@BindView(R.id.tv_title_img_view1)
TextView tvTitleImgView1;
@BindView(R.id.tv_title_img_view2)
TextView tvTitleImgView2;
@BindView(R.id.img_view)
ImageView imgView;
@BindView(R.id.tv_time_count)
TextView tvTimeCount;
private int adTime = 8000;//倒计时秒数
private int timeInterval = 1000;//倒计时间隔
private CountDownTimer mTimer;//计时器
private int change = 0;//记录下标
private int[] ids = new int[]{R.drawable.img1, R.drawable.img2, R.drawable.img3};
private Drawable[] drawables;//图片集合
private Thread mThread;//线程
private boolean mThreadFlag = true;//线程结束标志符
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act_transitiondrawable);
ButterKnife.bind(this);
//************************************/transitionDrawable是切换两张图片淡入淡出效果的一个类/************************************
tvTitleImgView1.setText("在imageView设置的background属性");
TransitionDrawable drawable1 = (TransitionDrawable) imgView1.getBackground();
drawable1.startTransition(3000);
tvTitleImgView2.setText("在imageView设置的src属性");
TransitionDrawable drawable2 = (TransitionDrawable) imgView2.getDrawable();
drawable2.startTransition(3000);
//纯代码实现
// Bitmap bitmap1= BitmapFactory.decodeResource(getResources(), R.drawable.img1);
// Bitmap bitmap2= BitmapFactory.decodeResource(getResources(), R.drawable.img2);
// final TransitionDrawable td = new TransitionDrawable(new Drawable[] { new BitmapDrawable(getResources(), bitmap1),
// new BitmapDrawable(getResources(), bitmap2) });
// imgView1.setImageDrawable(td);
// td.startTransition(3000);
//************************************/transitionDrawable结合线程使图片在指定时间内循环切换/************************************
initView();
initData();
}
private void initView() {
//填充图片
drawables=new Drawable[ids.length];
for (int i = 0; i < ids.length; i++) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawables[i] = getDrawable(ids[i]);
} else {
drawables[i] = getResources().getDrawable(ids[i]);
}
}
tvTimeCount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mTimer.cancel();
jumpActivity();
}
});
}
//定义hander
private Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
int duration = msg.arg1;
TransitionDrawable transitionDrawable = new TransitionDrawable(new Drawable[]{drawables[change % ids.length],
drawables[(change + 1) % ids.length]});
change++;//改变标识位置
imgView.setImageDrawable(transitionDrawable);
transitionDrawable.startTransition(duration);
return false;
}
});
//开启线程发送消息,让transition一直在改变
private class MyRunnable implements Runnable {
@Override
public void run() {
//这个while(true)是做死循环
while (mThreadFlag) {
int duration = 3000;//改变的间隔
Message message = mHandler.obtainMessage();
message.arg1 = duration;
mHandler.sendMessage(message);
try {
Thread.sleep(duration);
//隔duration秒发送一次
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private void initData() {
// 初始化计时器,第一个参数是共要倒计时的秒数,第二个参数是倒计时的间隔
mTimer = new CountDownTimer(adTime, timeInterval) {
// 倒计时开始时要做的事情,参数m是直到完成的时间
@Override
public void onTick(long millisUntilFinished) {
tvTimeCount.setText("" + millisUntilFinished / 1000 + "s跳过广告");
}
// 结束计时后要做的工作
@Override
public void onFinish() {
jumpActivity();
}
};
//开启计时器
mTimer.start();
//开启线程,改变transition,切换图片
mThread= new Thread(new ActTransitionDrawable.MyRunnable());
mThread.start();
}
// 跳转页面
private void jumpActivity() {
//如果还没结束当前的页面,就结束
if (!isFinishing()) {
finish();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//mThread.stop(); 不推荐使用
mThreadFlag=false;//结束线程
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
}