使用viewpager android单击图像时延迟幻灯片操作
我已经创建了一个幻灯片,在其中我禁用了滑动屏幕的默认滑动操作。相反,当我单击图像时,幻灯片会执行。但是我想在点击图像后将幻灯片延迟t毫秒。单击操作的方法是onClickSlideDown。 viewpager类如下所示:使用viewpager android单击图像时延迟幻灯片操作,android,android-viewpager,android-animation,Android,Android Viewpager,Android Animation,我已经创建了一个幻灯片,在其中我禁用了滑动屏幕的默认滑动操作。相反,当我单击图像时,幻灯片会执行。但是我想在点击图像后将幻灯片延迟t毫秒。单击操作的方法是onClickSlideDown。 viewpager类如下所示: public class slidescreen extends ActionBarActivity implements Animation.AnimationListener { //Declare variables ViewPager viewPager; Pager
public class slidescreen extends ActionBarActivity implements Animation.AnimationListener {
//Declare variables
ViewPager viewPager;
PagerAdapter adapter;
int[] background;
int[] icon;
String[] title;
String[] title_2;
String[] description;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slidescreenmain);
//Generate sample data
background = new int[]{R.mipmap.bg1, R.mipmap.bg2, R.mipmap.bg3, R.mipmap.bg4, R.mipmap.bg5, R.mipmap.bg6,
R.mipmap.bg7, R.mipmap.bg8, R.mipmap.bg9, R.mipmap.bg10, R.mipmap.bg11, R.mipmap.bg12};
icon = new int[]{R.mipmap.im1, R.mipmap.im2, R.mipmap.im3, R.mipmap.im4, R.mipmap.im5,
R.mipmap.im6, R.mipmap.im7, R.mipmap.im8, R.mipmap.im9, R.mipmap.im10, R.mipmap.im11, R.mipmap.im12};
title = new String[]{"ALTA RESISTENCIA A", "ALTA RESISTENCIA", "ALTAMENTE", "RESISTENCIA A", "MATERIAL", "ALTA RESISTENCIA",
"RESISTENCIA AL", "RESISTENCIA", "ESTABILIDAD", "ESTABILIDAD", "RESISTENCIA A", "NULA ABSORCIÓN"};
title_2 = new String[]{"LOS RAYOS UV", "AL FUEGO Y AL CALOR", "RESISTENTE AL RAYADO", "LAS MANCHAS", "INCOMBUSTIBLE", "A LA HIDRÓLISIS",
"HIELO Y DESHIELO", "MECÁNICA", "DIMENSIONAL", "DEL COLOR", "LA ABRASIÓN", "DEL AGUA"};
description = new String[]{"Por naturaleza, es capaz del repeler\n" + "líquidos y gases para que no penetren en\n" +
"la superficie. De este modo, el\n" + "mantenimiento de la superficie es mínimo\n" +
"y más fácil de limpiar."};
// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) findViewById(R.id.pager);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(slidescreen.this, background, icon, title, title_2, description);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
getSupportActionBar().hide();
}
public void onClickSlideDown(View view) {
Animation slideback;
ImageView iconimage, whitebox;
TextView titletext, title_2text, descriptiontext;
titletext = (TextView)findViewById(R.id.title);
title_2text = (TextView)findViewById(R.id.title_2);
descriptiontext = (TextView)findViewById(R.id.description);
iconimage = (ImageView)findViewById(R.id.icon);
whitebox = (ImageView)findViewById(R.id.whitebox);
slideback = AnimationUtils.loadAnimation(this, R.anim.whiteboxanimback);
slideback.setAnimationListener(this);
whitebox.startAnimation(slideback);
iconimage.startAnimation(slideback);
titletext.startAnimation(slideback);
title_2text.startAnimation(slideback);
descriptiontext.startAnimation(slideback);
if (viewPager.getCurrentItem() < viewPager.getAdapter().getCount()) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
} else {
Intent i1 = new Intent(this, glass_3.class);
startActivity(i1);
}
公共类slidescreen扩展ActionBarActivity实现Animation.AnimationListener{
//声明变量
查看页面查看页面;
PagerAdapter适配器;
int[]背景;
int[]图标;
字符串[]标题;
字符串[]title_2;
字符串[]说明;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.slidescreenmain);
//生成样本数据
背景=新建int[]{R.mipmap.bg1、R.mipmap.bg2、R.mipmap.bg3、R.mipmap.bg4、R.mipmap.bg5、R.mipmap.bg6、,
R.mipmap.bg7、R.mipmap.bg8、R.mipmap.bg9、R.mipmap.bg10、R.mipmap.bg11、R.mipmap.bg12};
icon=newint[]{R.mipmap.im1,R.mipmap.im2,R.mipmap.im3,R.mipmap.im4,R.mipmap.im5,
R.mipmap.im6、R.mipmap.im7、R.mipmap.im8、R.mipmap.im9、R.mipmap.im10、R.mipmap.im11、R.mipmap.im12};
title=新字符串[]{“ALTA RESISTENCIA A”、“ALTA RESISTENCIA”、“ALTAMENTE”、“RESISTENCIA A”、“MATERIAL”、“ALTA RESISTENCIA”,
“RESISTENCIA AL”、“RESISTENCIA”、“ESTABILIDAD”、“ESTABILIDAD”、“RESISTENCIA A”、“NULA ABSORCIÓN”};
title_2=新字符串[]{“LOS RAYOS UV”、“AL FUEGO Y AL CALOR”、“RESISTENTE AL RAYADO”、“LAS MANCHAS”、“不可燃”、“A LA HIDRÓLISIS”,
“HIELO Y DESHIELO”、“MECÁNICA”、“DIMENSIONAL”、“DEL COLOR”、“LA ABRASIÓN”、“DEL AGUA”};
description=新字符串[]{“Por naturaleza,es capaz del repeler\n”+“líquidos y gas para que no penetren en\n”+
“超级建筑。现代建筑,el\n”+“超级建筑的设计”+
“y más fácil de limpiar.”;
//在ViewPager_main.xml中找到ViewPager
viewPager=(viewPager)findViewById(R.id.pager);
//将结果传递给ViewPagerAdapter类
适配器=新的ViewPagerAdapter(slidescreen.this、背景、图标、标题、标题2、说明);
//将适配器绑定到ViewPager
viewPager.setAdapter(适配器);
getSupportActionBar().hide();
}
单击SlideDown(视图)时的公共void{
动画滑回;
图像视图图像,白盒;
text查看titletext、title_2text、descriptiontext;
titletext=(TextView)findViewById(R.id.title);
title_2text=(TextView)findViewById(R.id.title_2);
descriptionContext=(TextView)findViewById(R.id.description);
iconimage=(ImageView)findViewById(R.id.icon);
whitebox=(ImageView)findViewById(R.id.whitebox);
slideback=AnimationUtils.loadAnimation(this,R.anim.whiteboxanimback);
slideback.setAnimationListener(这个);
白盒。启动动画(滑回);
i动画。开始动画(滑回);
titletext.startAnimation(滑回);
title_2文本开始动画(幻灯片回退);
DescriptionContext.startAnimation(滑回);
if(viewPager.getCurrentItem()
谢谢:)尝试一下。在onClickSlideDown方法中添加此代码
Thread timer = new Thread(){
public void run(){
try{
sleep(2000);
}catch(InterruptedException e){
e.printStackTrace();
}finally
{
//Your entire code of onClickSlideDown method
}
}
};
timer.start();
希望这能起作用,并将滑动延迟2000毫秒
更新:如果intent中的关键字“this”导致错误,请尝试在intent中使用getApplicationContext()而不是this关键字。感谢@Ish的回答,但您不能在任何其他线程上执行ui操作 所以处理程序来拯救,默认构造函数使用主线程消息队列循环器对象
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//TODO: Write your code here
}
}, delayinMiils);
但当您添加延迟时,您正在处理主线程的上下文泄漏…为了避免任何类型的上下文泄漏,请参阅下面的实现
private final MyHandler mHandler = new MyHandler(this);
/**
* Instances of anonymous classes do not hold an implicit
* reference to their outer class when they are "static".
*/
private static final Runnable sRunnable = new Runnable() {
@Override
public void run() { /* ... */ }
};
// Post a message and delay its execution for 10 minutes.
mHandler.postDelayed(sRunnable, 1000 * 60 * 10);
我有一个问题。在下面的代码中,我应该写什么来代替“this”:Intent i1=newintent(this,glass_3.class);很抱歉,我对java不太了解。谢谢@lshThis这是一个非常复杂的错误。使用postdayed()