ImageSwitcher is;
...
is.setInAnimation(this, android.R.anim.slide_in_left);
is.setOutAnimation(this, android.R.anim.slide_out_right);
上面实现了从左向右滑动的效果,可是反过来,从右向左滑动怎么实现呢?
默认情况下 android.R.anim.slide_in_right 和 android.R.anim.slide_out_left是私有的,也就是说通过.属性是找不到的.
解决办法:slide_in_right和slide_out_left这两个xml都可以在sdk里找到的,修改后作为自己的资源调用就好了
platforms\android-10\data\res\anim\下找到下面两个xml文件.
slide_in_right
slide_out_left
最后放到项目 res->anim下面
imgSwitcher.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
downX = (int) event.getX(); // 取得按下时的坐标
Log.i("event.getX()", " downX " + downX);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
upX = (int) event.getX(); // 取得松开时的坐标
Log.i("event.getX()", " upX " + downX);
// 从左拖到右,即看前一张
if (upX - downX > 100) {
imgSwitcher.setInAnimation(AnimationUtils
.loadAnimation(firstActivity.this,
android.R.anim.slide_in_left));
imgSwitcher.setOutAnimation(AnimationUtils
.loadAnimation(firstActivity.this,
android.R.anim.slide_out_right));
curIndex--;
if (curIndex < 0) {
curIndex = 5;
}
imgSwitcher.setImageResource(IMAGE_LIST[curIndex]);
firstActivity.this.switchTitle(curIndex);
} else if (downX - upX > 100) { // 从右拖到左,即看后一张
imgSwitcher.setInAnimation(AnimationUtils
.loadAnimation(firstActivity.this,
R.anim.slide_out_left));
imgSwitcher.setOutAnimation(AnimationUtils
.loadAnimation(firstActivity.this,
R.anim.slide_in_right));
curIndex++;
if (curIndex > 5) {
curIndex = 0;
}
imgSwitcher.setImageResource(IMAGE_LIST[curIndex]);
firstActivity.this.switchTitle(curIndex);
}
return true;
}
return false;
}
});
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
downX = (int) event.getX(); // 取得按下时的坐标
Log.i("event.getX()", " downX " + downX);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
upX = (int) event.getX(); // 取得松开时的坐标
Log.i("event.getX()", " upX " + downX);
// 从左拖到右,即看前一张
if (upX - downX > 100) {
imgSwitcher.setInAnimation(AnimationUtils
.loadAnimation(firstActivity.this,
android.R.anim.slide_in_left));
imgSwitcher.setOutAnimation(AnimationUtils
.loadAnimation(firstActivity.this,
android.R.anim.slide_out_right));
curIndex--;
if (curIndex < 0) {
curIndex = 5;
}
imgSwitcher.setImageResource(IMAGE_LIST[curIndex]);
firstActivity.this.switchTitle(curIndex);
} else if (downX - upX > 100) { // 从右拖到左,即看后一张
imgSwitcher.setInAnimation(AnimationUtils
.loadAnimation(firstActivity.this,
R.anim.slide_out_left));
imgSwitcher.setOutAnimation(AnimationUtils
.loadAnimation(firstActivity.this,
R.anim.slide_in_right));
curIndex++;
if (curIndex > 5) {
curIndex = 0;
}
imgSwitcher.setImageResource(IMAGE_LIST[curIndex]);
firstActivity.this.switchTitle(curIndex);
}
return true;
}
return false;
}
});