gpt4 book ai didi

android - 在 Android 中使用 Viewflipper 类的图像点击监听器

转载 作者:行者123 更新时间:2023-11-30 00:21:55 28 4
gpt4 key购买 nike

我正在使用 android 中的 viewflipper 类滑动我的图像。我有图像数组。现在每个图像都有自己的功能。例如,第一个图像应该带我去另一个 Activity 和其他类似的 Activity 。我想做的是为每个图像制作点击监听器,以便我可以根据图像执行不同的任务。这是我的代码:

public class gesture extends AppCompatActivity {
private ViewFlipper mViewFlipper;
private GestureDetector mGestureDetector;
MediaPlayer mp;
int i;
int a=0;
ImageView imageView;

int[] image = {
R.drawable.p1,
R.drawable.p2,
R.drawable.p3,
R.drawable.p4,
R.drawable.p5,
R.drawable.p6
};
int[] audio={
R.raw.p1,
R.raw.p2,
R.raw.p3,
R.raw.p4,
R.raw.p5,
R.raw.p6

};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gesture);
CustomGestureDetector customGestureDetector = new CustomGestureDetector();
mGestureDetector = new GestureDetector(this, customGestureDetector);


// Get the ViewFlipper
mViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);


if (mp != null) {
mp.stop();
mp.release();
}
mp = MediaPlayer.create(gesture.this, audio[a]);
mp.start();


// Add all the images to the ViewFlipper
for (i = 0; i < image.length; i++) {
imageView = new ImageView(this);

imageView.setImageResource(image[i]);
mViewFlipper.addView(imageView);

}


}

class CustomGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

// Swipe left (next)
if (e1.getX() > e2.getX()) {

if (mp != null) {
mp.stop();
mp.release();
}


a++;
if (a==6){
a=0;
}
mp = MediaPlayer.create(gesture.this, audio[a]);
mp.start();
mViewFlipper.showNext();


}

// Swipe right (previous)
if (e1.getX() < e2.getX()) {

if (mp != null) {
mp.stop();
mp.release();
}

a--;
if(a==-1){
a=5;
}
mp = MediaPlayer.create(gesture.this, audio[a]);
mp.start();
mViewFlipper.showPrevious();



}

return super.onFling(e1, e2, velocityX, velocityY);


}

}
@Override
public boolean onTouchEvent(MotionEvent event) {
mGestureDetector.onTouchEvent(event);

return super.onTouchEvent(event);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return false;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();
if (id == R.id.action_settings) {
return false;
}
return super.onOptionsItemSelected(item);
}

提前致谢。

最佳答案

首先在你的 Activity 中实现OnClickListener

public class gesture extends AppCompatActivity implements View.OnClickListener {

然后给每张图片一个id

for (i = 0; i < image.length; i++) {
imageView = new ImageView(this);

imageView.setImageResource(image[i]);
imageView.setId(i); // <---- assigning id
mViewFlipper.addView(imageView);

}

最后,处理点击:

@Override
public void onClick(View v) {
Class<?> c = null;
int imageId = v.getId();

try {
c = Class.forName("ActivityToStartOn"+imageId);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent(gesture.this, c);
startActivity(intent);

}

注意:我假设您要启动的 Activity 名为 ActivityToStartOn0ActivityToStartOn1、....

关于android - 在 Android 中使用 Viewflipper 类的图像点击监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46129429/

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