gpt4 book ai didi

java - 同时运行动画和图像切换器

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:52:25 26 4
gpt4 key购买 nike

我试图在一定时间后从动画列表中运行图像,但我还在屏幕顶部添加了图库,用户可以从中选择任何图像并将其显示,并且我创建了线程将在五秒后一次又一次地启动动画。我的代码是:

 int imgs[] =  
{
R.drawable.mashad_one,
R.drawable.mashad_two,
R.drawable.mashad_three,
R.drawable.mashad_four,
R.drawable.mashad_five,
R.drawable.mashad_six,
R.drawable.mashad_seven,
};
int i=0,flag=0;
ImageSwitcher imgBus;
AnimationDrawable animation;
Handler mHandler = new Handler();
void func()
{
imgBus.setFactory(this);
}


@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.dutyfree);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FF6600"))); // Declare object of Utils class;
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));

gallery.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

if(i==0)

{
i++;
func();
}

flag=1;
imgBus.setImageResource(imgs[arg2]);
}
});

imgBus = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
imgBus.setBackgroundResource(R.drawable.anim);

imgBus.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imgBus.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));

animation = (AnimationDrawable) imgBus.getBackground();


animation.start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(5000);
mHandler.post(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
// Write your code here to update the UI.
if(flag==1){
animation.start();
}
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();


}
public class ImageAdapter extends BaseAdapter {

private Context ctx;

public ImageAdapter(Context c) {
ctx = c;
}

public int getCount() {

return imgs.length;
}


public long getItemId(int arg0) {

return arg0;
}

public View getView(int arg0, View arg1, ViewGroup arg2) {

ImageView iView = new ImageView(ctx);
iView.setImageResource(imgs[arg0]);
iView.setScaleType(ImageView.ScaleType.FIT_XY);
iView.setLayoutParams(new Gallery.LayoutParams(200, 150));
return iView;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
}

public View makeView() {
ImageView iView = new ImageView(this);
iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
iView.setLayoutParams(new ImageSwitcher.LayoutParams
(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
iView.setBackgroundColor(0xFF000000);
return iView;
}

}

问题是当 Activity 开始时动画开始运行并且图像不断变化,但是一旦我按下图库中的任何图像,动画就会停止并且不会再次开始,请帮我找到问题所在。

最佳答案

我得到了我的问题的解决方案,我只是使用了imageswitcher的重置功能,并在点击方法中使用了它。

int imgs[] =  
{
R.drawable.mashad_one,
R.drawable.mashad_two,
R.drawable.mashad_three,
R.drawable.mashad_four,
R.drawable.mashad_five,
R.drawable.mashad_six,
R.drawable.mashad_seven,
};
int i=0,flag=0;
ImageSwitcher imgBus;
AnimationDrawable animation;
Handler mHandler = new Handler();
void func()
{
imgBus.setFactory(this);
}


@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.dutyfree);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FF6600"))); // Declare object of Utils class;
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));

gallery.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

if(i==0)

{
i++;
func();
}

flag=1;
animation.stop();
imgBus.setImageResource(imgs[arg2]);
}
});

imgBus = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
imgBus.setBackgroundResource(R.drawable.anim);

imgBus.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imgBus.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));

animation = (AnimationDrawable) imgBus.getBackground();


animation.start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(5000);
mHandler.post(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub


// Write your code here to update the UI.
if(flag==1){
imgBus.reset();
animation.start();
}
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();


}

public class ImageAdapter extends BaseAdapter {
private Context ctx;

public ImageAdapter(Context c) {
ctx = c;
}

public int getCount() {

return imgs.length;
}


public long getItemId(int arg0) {

return arg0;
}

public View getView(int arg0, View arg1, ViewGroup arg2) {

ImageView iView = new ImageView(ctx);
iView.setImageResource(imgs[arg0]);
iView.setScaleType(ImageView.ScaleType.FIT_XY);
iView.setLayoutParams(new Gallery.LayoutParams(200, 150));
return iView;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
}

public View makeView() {
ImageView iView = new ImageView(this);
iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
iView.setLayoutParams(new ImageSwitcher.LayoutParams
(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
iView.setBackgroundColor(0xFF000000);
return iView;
}

}

关于java - 同时运行动画和图像切换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27426073/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com