0
アンドロイドのviewflipperクラスを使用して画像をスワイプしています。画像配列があります。それぞれの画像にはそれぞれ独自の機能があります。たとえば、最初の画像は私を別のアクティビティに、同様に他の画像に移動します。私がしたいことは、各画像のクリックリスナーを作成して、画像に応じて異なるタスクを実行できるようにすることです。 私のコードは次のとおりです。イメージアンドロイドのViewflipperクラスを使用してリスナーをクリックします。
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);
}
ありがとうございます。
@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);
}
注:
という名前です。しかし、もし私が50以上の画像を持っているなら、私は何ができますか? –
@UzairQaiser私の更新答えを見てください。私はそれを試していないが、あなたはそれを処理する方法のアイデアを得るでしょう – mrid
あなたのコードのエラー:ステートメントは、ケースラベルの前に追加する必要があります。 –