android自动切换图片,android-自动切换背景图片
因为好用,所以值得收藏。
》》》》 》》》》》
一、创建切换背景布局类
public class LaunchAppAnimatedBgRelativeLayout extends
RelativeLayout {
private Random random;
private final float MAX_ALPHA = 1F;
private final float MIN_ALPHA = 0F;
private final int ANIMATION_DURATION =
800;
private final int DISPLAY_DURATION = (2000 +
ANIMATION_DURATION);
private Handler handler;
private ImageView animationImageView;
private ImageView staticImageView;
private List imageIdList;
private int currentImageIndex = -1;
private boolean isContinueLoop = true;
public
LaunchAppAnimatedBgRelativeLayout(Context context,
AttributeSet
attrs, int defStyle) {
super(context, attrs,
defStyle);
init();
}
public
LaunchAppAnimatedBgRelativeLayout(Context context, AttributeSet
attrs) {
super(context, attrs);
init();
}
public
LaunchAppAnimatedBgRelativeLayout(Context context) {
super(context);
init();
}
private void init() {
random = new Random();
handler = new Handler();
staticImageView =
makeNewImageView();
animationImageView =
makeNewImageView();
this.setBackgroundColor(Color.BLACK);
}
private ImageView makeNewImageView() {
ImageView imageView = new
ImageView(getContext());
imageView.setScaleType(ScaleType.CENTER_CROP);
this.addView(imageView, new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return imageView;
}
private int calNextImageIdIndex() {
currentImageIndex++;
if (currentImageIndex >
imageIdList.size() - 1) {
currentImageIndex
= 0;
}
return currentImageIndex;
}
private void
setAlphaAnimatedImageViewFadeOut() {
calNextImageIdIndex();
float fromAlpha =
MAX_ALPHA;
float toAlpha =
MIN_ALPHA;
AlphaAnimation alphaAnimation =
new AlphaAnimation(fromAlpha, toAlpha);
alphaAnimation.setDuration(ANIMATION_DURATION);
alphaAnimation.setAnimationListener(new
AnimationListener() {
@Override
public void
onAnimationStart(Animation animation) {
staticImageView.setImageResource(imageIdList
.get(currentImageIndex));
}
@Override
public void
onAnimationRepeat(Animation animation) {
}
@Override
public void
onAnimationEnd(Animation animation) {
animationImageView.setImageResource(imageIdList
.get(currentImageIndex));
if
(isContinueLoop) {
handler.postDelayed(new
Runnable() {
@Override
public
void run() {
setAlphaAnimatedImageViewFadeOut();
}
},
DISPLAY_DURATION);
}
}
});
animationImageView.startAnimation(alphaAnimation);
}
public boolean isContinueLoop() {
return isContinueLoop;
}
public void setContinueLoop(boolean
isContinueLoop) {
if ((!(this.isContinueLoop))
&& isContinueLoop) {
this.isContinueLoop
= isContinueLoop;
setAlphaAnimatedImageViewFadeOut();
} else {
this.isContinueLoop
= isContinueLoop;
}
}
public void setImageIdList(List imageIdList)
{
this.imageIdList =
imageIdList;
currentImageIndex = 0;
isContinueLoop = true;
animationImageView.setImageResource(imageIdList.get(currentImageIndex));
handler.postDelayed(new
Runnable() {
@Override
public void
run() {
setAlphaAnimatedImageViewFadeOut();
}
}, DISPLAY_DURATION);
}
}
二、在mainActivity内加载该布局,并向该布局传递需要进行切换的图片list即可,可根据项目需求修改布局参数,以实现自己需要的功能。
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout layout =
(LinearLayout) findViewById(R.id.mlinear);
launch = new
LaunchAppAnimatedBgRelativeLayout(this);
layout.addView(launch);
List imageIdList = new
ArrayList();
imageIdList.add(R.drawable.car2);
imageIdList.add(R.drawable.car3);
imageIdList.add(R.drawable.car4);
imageIdList.add(R.drawable.car7);
imageIdList.add(R.drawable.car8);
imageIdList.add(R.drawable.car9);
launch.setImageIdList(imageIdList);
}
android自动切换图片,android-自动切换背景图片相关推荐
- table vue 背景图片_table中background背景图片自动拉伸
解决android:background背景图片被拉伸问题 ImageView中XML属性src和background的区别: background会根据ImageView组件给定的长宽进行拉伸,而s ...
- java添加背景图片_Java怎么添加背景图片
首先,导入相关的包: import java.awt.BorderLayout; import java.awt.Container; import javax.swing.ImageIcon; im ...
- java框体怎么添加背景图,文本框添加背景图片,文本框背景图片,public class
文本框添加背景图片,文本框背景图片,public classpublic class WatermarkTextField extends JTextField { BufferedImage img ...
- java为文本框添加背景颜色,文本框添加背景图片,文本框背景图片,文本框添加水印[Java...
文本框添加背景图片,文本框背景图片,文本框添加水印[Java 文本框添加水印 [Java]代码public class WatermarkTextField extends JTextField { ...
- OpenCV技巧 | 常用格式图片保存为透明背景图片(附Python源码)-教你轻松制作Logo
导读 本文主要介绍使用OpenCV将常用格式图片保存为透明背景图片的方法与实现代码. 实现目标 本文的目标有如下两个: ① 将常见格式[jpg/png/bmp]白色背景图片转换保存为透明背景图片: ② ...
- 在html 背景图片的大小,网页背景图片的大小的代码是什么
background-size 设置背景图片大小. /* 关键字 */ background-size: cover background-size: contain /* 一个值: 这个值指定图片的 ...
- win10下载后的背景图片在哪等背景图片位置的问题
问题: 1.Win10下载后的背景图片在哪? 当使用一个下载的图片设置为window背景图片时,这个图片存在哪里了?答案是: C:\Users\Administrator\AppData\Roamin ...
- html内容被背景图片遮住怎么办_css背景图片显示不完怎么解决?
css背景图片显示不完是什么原因?怎么解决?下面本篇文章就来给大家介绍一下css背景图片显示不完的解决方法.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. css设置背景图片显示不全 ...
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解...
2019独角兽企业重金招聘Python工程师标准>>> 今天继续给大家分享下View的相关知识,重点有一下两点: 1.View的几种不同状态属性 2.如何根 ...
- Android中View绘制各种状态的背景图片原理深入分析以及StateListDrawable使用
/* Call this to force a view to update its drawable state. This will cause drawableStateChanged to b ...
最新文章
- 计算机二级执行程序,计算机二级VFP程序文件的建立与执行
- 【BZOJ4417】: [Shoi2013]超级跳马
- 安装SQL2005提示“SQL Server 2005 COM+ 目录要求”警告 解决方法
- JDK下载地址、SecureCRT中JDK安装和环境配置、SecureCRT窗口编程、linux下命令运行小程序
- npm的镜像替换淘宝
- 【蓝桥杯单片机】实战之基础停表
- 数据库设计时设计标识字段的一些思考
- 谷歌软件工程师_这是我曾经在Google担任软件工程师的简历。
- 解决WORD输出Html图片Jpg变模糊的问题
- 大话2正在连接登录服务器,大话西游手游服务器连接失败进不去解决办法
- Liunx教程超详细(完整)
- 凸函数(convex)
- 幅频特性曲线protues_幅频特性曲线Matlab编程
- 特别详细的数据中心运维管理方案
- 电脑硬盘分区不见了怎么恢复数据?方法来啦
- Python-实现trim函数
- 学习记录-钓鱼网站总结
- 江西2022农民丰收节开幕式 国稻种芯:活动主场浮梁县举办
- 无线网络中PPK-MIC和CKIP-CMIC的学习资料
- 初中英语多词性单词怎么办_初中英语词性转换大全: