gpt4 book ai didi

android - Android 中没有图库的全屏 ImageSwitcher

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:06:44 26 4
gpt4 key购买 nike

我正在尝试在 android 中构建一个应用程序,在我的一项 Activity 中,我想显示全屏图像并通过在图片上滑动手指使它们左右滑动。

我已经尝试过基本的画廊 View 和图像切换器,但我无法处理触摸事件以产生滑动效果,就像在自定义 android 画廊中一样,但没有缩略图。

这是我的简单图像切换器 xml 和 Activity 类。如果有人向我展示一种方法或在下面编辑我的代码,我将不胜感激。提前致谢...

布局 xml:

 <?xml version="1.0" encoding="utf-8"?>
<ImageSwitcher
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="imageClick"
android:src="@drawable/ic_launcher"
android:keepScreenOn="true">
</ImageSwitcher>

代码:

public class GalleryActivity extends Activity implements ViewFactory {

private ImageSwitcher imageSwitcher ;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activities);

imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
imageSwitcher.setFactory(this);
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));

imageSwitcher.setImageResource(R.drawable.menu);

imageSwitcher.setOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
findViewById(R.drawable.menu);
imageSwitcher.addView((ImageSwitcher) findViewById(R.drawable.etkinlik));
imageSwitcher.showNext();

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

在编辑了一些@imran khan 的解决方案之后,下面是运行良好的代码。

解决方案:

ImageSwitcher imageSwitcher ;

Integer[] imageList = {
R.drawable.gallery,
R.drawable.menu,
R.drawable.promotion,
R.drawable.info,
R.drawable.activities
};

int curIndex=0;
int downX,upX;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activities);

imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
imageSwitcher.setFactory(this);
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));

imageSwitcher.setImageResource(imageList[curIndex]);
imageSwitcher.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

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) {

//curIndex current image index in array viewed by user
curIndex--;
if (curIndex < 0) {
curIndex = imageList.length-1;
}

imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(GalleryActivity.this,R.anim.slide_in_left));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(GalleryActivity.this,R.anim.slide_out_right));

imageSwitcher.setImageResource(imageList[curIndex]);
//GalleryActivity.this.setTitle(curIndex);
}

else if (downX - upX > -100) {

curIndex++;
if (curIndex > 4) {
curIndex = 0;
}

imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(GalleryActivity.this,R.anim.slide_in_right));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(GalleryActivity.this,R.anim.slide_out_left));

imageSwitcher.setImageResource(imageList[curIndex]);
//GalleryActivity.this.setTitle(curIndex);
}
return true;
}
return false;
}
});
}
@Override
public View makeView() {
ImageView i = new ImageView(this);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
return i;
}

最佳答案

试试这个方法:

imageSwitcher1 = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
imageSwitcher1.setFactory(this);
imageSwitcher1.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher1.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
imageSwitcher1.setImageResource(R.drawable.girl2);
imageSwitcher1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
downX = (int) event.getX();
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
upX = (int) event.getX();
if (upX - downX > 100) {
imageSwitcher1.setInAnimation(AnimationUtils.loadAnimation(ShowPhotoActivity.this,
android.R.anim.slide_in_left));
mageSwitcher1.setOutAnimation(AnimationUtils.loadAnimation(ShowPhotoActivity.this,
android.R.anim.slide_out_right));
imageSwitcher1.setImageResource(R.drawable.girl1);
} else if (downX - upX > 100)// {
imageSwitcher1.setInAnimation(AnimationUtils.loadAnimation(ShowPhotoActivity.this,
android.R.anim.slide_in_left));
imageSwitcher1.setOutAnimation(AnimationUtils.loadAnimation(ShowPhotoActivity.this,
android.R.anim.slide_out_right));
imageSwitcher1.setImageResource(R.drawable.girl2);
}
return true;
}
return false;
}
};

如果你有图像数组,那么试试这个:

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 current image index in array viewed by user
curIndex--;
if (curIndex < 0) {
curIndex = 5;
}
//IMAGE_LIST :-image list array
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;
}
});

关于android - Android 中没有图库的全屏 ImageSwitcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10043535/

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