如何在 android 中连续闪烁第 4 张图像
How to blink image in back to back 4 image in android
我想将背对背的图像闪烁最多 4 张图像。这意味着显示 1 张图像 n 消失,当第 1 张图像消失时,显示第 2 张图像,当第 2 张图像消失时,显示第 3 张图像,这个过程继续满足我的要求,图像在 运行 时间内加载。
如果图像是 url 基础的或在线的那么
int pos=0;
String[] urlsOfimages;
private Handler blnkHandler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case STOPSPLASH:
//Set image here form the urlsOfimages[pos]
img.setVisibility(ImageView.GONE);
resumeMessage = new Message();
resumeMessage.what = RESUMESPLASH;
resumeHandler.sendMessageDelayed(resumeMessage, SPLASHTIME);
pos++;
//Logic could be improve by your self by sending What pram, i just give you and idea
if(pos==5)
pos=0;
}
}
};
private Handler resumeHandler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case RESUMESPLASH:
img.setVisibility(ImageView.VISIBLE);
stopMessage.what = STOPSPLASH;
blnkHandler.sendMessageDelayed(stopMessage, SPLASHTIME);
}
}
};
并在创建方法中
blnkHandler.sendMessageDelayed(STOPSPLASH, SPLASHTIME);
如果图像在可绘制文件夹中离线可用,则
AnimationDrawable myAnimationDrawable;
public void loadAnimation()
{
ImageView myAnimation = (ImageView)findViewById(R.id.progress_bar);
myAnimationDrawable= (AnimationDrawable)myAnimation.getDrawable();
myAnimationDrawable.start();
}
在 XML 喜欢 anim_loading.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list android:id="@+id/progress_horizontal" android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/loading_0" android:duration="500" />
<item android:drawable="@drawable/loading_1" android:duration="500" />
<item android:drawable="@drawable/loading_2" android:duration="500" />
<item android:drawable="@drawable/loading_3" android:duration="500" />
<item android:drawable="@drawable/loading_4" android:duration="500" />
</animation-list>
如果您的图像必须在 运行 时选择,则按语法创建动画列表
animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.img1), 100);
animation.addFrame(getResources().getDrawable(R.drawable.img2), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.img3), 1000);
animation.setOneShot(false);
ImageView imageAnim = (ImageView) findViewById(R,id.img);
imageAnim.setBackgroundDrawable(animation);
imageAnim.post(new Runnable() {
@Override
public void run() {
animation.start();
}
});
试试下面的代码:-
private void loadingAmin(final TextView loading) {
final Animation pokeLoadingAnim = new Animation() {
int step = 0;
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
// TODO Auto-generated method stub
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime == 0) {
loading.setText("Loading");
step = 1;
}
if ((interpolatedTime / 0.3) > step) {
loading.setText(loading.getText() + ".");
++step;
}
}
};
pokeLoadingAnim.setDuration(2000);
pokeLoadingAnim.setRepeatCount(Animation.INFINITE);
pokeLoadingAnim.setInterpolator(new LinearInterpolator());
pokeLoadingAnim.setRepeatMode(Animation.RESTART);
loading.startAnimation(pokeLoadingAnim);
}
调用这个方法
TextView layout = (TextView) getView().findViewById(R.id.loading);
loadingAmin(layout);
我想将背对背的图像闪烁最多 4 张图像。这意味着显示 1 张图像 n 消失,当第 1 张图像消失时,显示第 2 张图像,当第 2 张图像消失时,显示第 3 张图像,这个过程继续满足我的要求,图像在 运行 时间内加载。
如果图像是 url 基础的或在线的那么
int pos=0;
String[] urlsOfimages;
private Handler blnkHandler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case STOPSPLASH:
//Set image here form the urlsOfimages[pos]
img.setVisibility(ImageView.GONE);
resumeMessage = new Message();
resumeMessage.what = RESUMESPLASH;
resumeHandler.sendMessageDelayed(resumeMessage, SPLASHTIME);
pos++;
//Logic could be improve by your self by sending What pram, i just give you and idea
if(pos==5)
pos=0;
}
}
};
private Handler resumeHandler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case RESUMESPLASH:
img.setVisibility(ImageView.VISIBLE);
stopMessage.what = STOPSPLASH;
blnkHandler.sendMessageDelayed(stopMessage, SPLASHTIME);
}
}
};
并在创建方法中
blnkHandler.sendMessageDelayed(STOPSPLASH, SPLASHTIME);
如果图像在可绘制文件夹中离线可用,则
AnimationDrawable myAnimationDrawable;
public void loadAnimation()
{
ImageView myAnimation = (ImageView)findViewById(R.id.progress_bar);
myAnimationDrawable= (AnimationDrawable)myAnimation.getDrawable();
myAnimationDrawable.start();
}
在 XML 喜欢 anim_loading.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list android:id="@+id/progress_horizontal" android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/loading_0" android:duration="500" />
<item android:drawable="@drawable/loading_1" android:duration="500" />
<item android:drawable="@drawable/loading_2" android:duration="500" />
<item android:drawable="@drawable/loading_3" android:duration="500" />
<item android:drawable="@drawable/loading_4" android:duration="500" />
</animation-list>
如果您的图像必须在 运行 时选择,则按语法创建动画列表
animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.img1), 100);
animation.addFrame(getResources().getDrawable(R.drawable.img2), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.img3), 1000);
animation.setOneShot(false);
ImageView imageAnim = (ImageView) findViewById(R,id.img);
imageAnim.setBackgroundDrawable(animation);
imageAnim.post(new Runnable() {
@Override
public void run() {
animation.start();
}
});
试试下面的代码:-
private void loadingAmin(final TextView loading) {
final Animation pokeLoadingAnim = new Animation() {
int step = 0;
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
// TODO Auto-generated method stub
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime == 0) {
loading.setText("Loading");
step = 1;
}
if ((interpolatedTime / 0.3) > step) {
loading.setText(loading.getText() + ".");
++step;
}
}
};
pokeLoadingAnim.setDuration(2000);
pokeLoadingAnim.setRepeatCount(Animation.INFINITE);
pokeLoadingAnim.setInterpolator(new LinearInterpolator());
pokeLoadingAnim.setRepeatMode(Animation.RESTART);
loading.startAnimation(pokeLoadingAnim);
}
调用这个方法
TextView layout = (TextView) getView().findViewById(R.id.loading);
loadingAmin(layout);