gpt4 book ai didi

Android 问题 - 如何准备 100 张图像以通过 Fling/Swipe 显示?

转载 作者:太空狗 更新时间:2023-10-29 12:58:51 27 4
gpt4 key购买 nike

我对此完全陌生,已经修修补补了一个星期。

为 2 张图片设计了一个简单的图片查看器应用程序。

特点:左右滑动将切换图像。非常简单。

我想做什么:最多有 100 张图片。

注意:我所有的图片都在我的 res/drawable 文件夹中。
它们被命名为 image1.png 到 image100.png

我显然不想这样做:

    ImageView i = new ImageView(this);
i.setImageResource(R.drawable.image1);
viewFlipper.addView(i);
ImageView i2 = new ImageView(this);
i2.setImageResource(R.drawable.image2);
viewFlipper.addView(i2);
ImageView i3 = new ImageView(this);
i3.setImageResource(R.drawable.image3);
viewFlipper.addView(i3);

一直到 i100。

我如何将它变成一个循环,它是灵活的并从可绘制文件夹中读取所有内容(并且不限于 100 张图像)?

来源:

    public class ImageViewTest extends Activity {
private static final String LOGID = "CHECKTHISOUT";

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;

private Animation slideLeftIn;
private Animation slideLeftOut;
private Animation slideRightIn;
private Animation slideRightOut;
private ViewFlipper viewFlipper;

String message = "Initial Message";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Set up viewflipper
viewFlipper = new ViewFlipper(this);
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.sample_1);
ImageView i2 = new ImageView(this);
i2.setImageResource(R.drawable.sample_2);
viewFlipper.addView(i);
viewFlipper.addView(i2);

//set up animations
slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);

//put up a brownie as a starter
setContentView(viewFlipper);

gestureDetector = new GestureDetector(new MyGestureDetector());
}

public class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.v(LOGID,"right to left swipe detected");
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideLeftOut);
viewFlipper.showNext();
setContentView(viewFlipper);

} // left to right swipe
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.v(LOGID,"left to right swipe detected");
viewFlipper.setInAnimation(slideRightIn);
viewFlipper.setOutAnimation(slideRightOut);
viewFlipper.showPrevious();
setContentView(viewFlipper);

}
} catch (Exception e) {
// nothing
}
return false;
}
}

// This doesn't work
@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)){
Log.v(LOGID,"screen touched");
return true;
}
else{
return false;
}
}
}

最佳答案

没有脚蹼,我像这样重写你的代码,对我来说就像魅力一样。

package com.mayuonline.swipe;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class SwipetestActivity extends Activity{
private static final String LOGID = "CHECKTHISOUT";
public int[] list = {R.drawable.a,R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e };
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
public int i = 0;

String message = "Initial Message";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

ImageView imageview = (ImageView)findViewById(R.id.imageView1);
imageview.setImageResource(list[0]);

gestureDetector = new GestureDetector(new MyGestureDetector());
}

public class MyGestureDetector extends SimpleOnGestureListener implements OnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
ImageView imageview = (ImageView)findViewById(R.id.imageView1);
if(list.length>i){
i++;
imageview.setImageResource(list[i]);
}
Log.v(LOGID,"right to left swipe detected");


} // left to right swipe
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
ImageView imageview = (ImageView)findViewById(R.id.imageView1);
if(i>0){
i--;
imageview.setImageResource(list[i]);
}
Log.v(LOGID,"left to right swipe detected");


}
} catch (Exception e) {
// nothing
}
return false;
}
}

// This doesn't work
@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)){
Log.v(LOGID,"screen touched");
return true;
}
else{
return false;
}
}

}

关于Android 问题 - 如何准备 100 张图像以通过 Fling/Swipe 显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2162049/

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