2

您好,我正在创建 ViewFlipper,如下所示。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:orientation="vertical" >

<ViewFlipper
    android:id="@+id/vfShow"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</ViewFlipper>

</LinearLayout>

并处理诸如

ViewFlipperSampleActivity.java

public class ViewFlipperSampleActivity extends Activity {

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;

private ViewFlipper vf;
private Context mContext;
private final GestureDetector detector = new GestureDetector(
        new MyGestureDetector());

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fliper);
    mContext = this;
    vf = (ViewFlipper) this.findViewById(R.id.vfShow);
    vf.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(final View view, final MotionEvent event) {
            detector.onTouchEvent(event);
            return true;
        }
    });

    vf.addView(addImageView(R.drawable.a));
    vf.addView(addImageView(R.drawable.b));
    vf.addView(addImageView(R.drawable.c));
    vf.addView(addImageView(R.drawable.d));

}

View addImageView(int resId) {
    ImageView iv = new ImageView(this);
    iv.setImageResource(resId);

    return iv;
}

class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        try {

            // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                vf.setInAnimation(AnimationUtils.loadAnimation(mContext,
                        R.anim.left_in));
                vf.setOutAnimation(AnimationUtils.loadAnimation(mContext,
                        R.anim.left_out));
                vf.showNext();
                return true;
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                vf.setInAnimation(AnimationUtils.loadAnimation(mContext,
                        R.anim.right_in));
                vf.setOutAnimation(AnimationUtils.loadAnimation(mContext,
                        R.anim.right_out));
                vf.showPrevious();
                return true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }
}

}

现在我很困惑如何用 viewflipper 在特定图像上播放声音?请帮助我解决这个问题。

谢谢

更新 :-

public class SoundClass {
static MediaPlayer theme1 = null, theme2 = null, theme3 = null,
        theme4 = null, theme5 = null, theme6 = null;
static MediaPlayer[] mPlayers = { theme1, theme2, theme3, theme4, theme5,
        theme6 };
static Integer[] resources = { R.raw.one, R.raw.two, R.raw.three,
        R.raw.four, R.raw.five, R.raw.six, R.raw.seven, R.raw.eight,
        R.raw.nine, R.raw.ten, R.raw.eleven, R.raw.twelve, R.raw.thirteen,
        R.raw.fourteen, R.raw.fifteen, R.raw.sixteen, R.raw.seventeen,
        R.raw.eighteen, R.raw.ninteen, R.raw.twenty };
static Context ctxt = null;

public SoundClass(Context context) {
    ctxt = context;
}

public static void createAndPlay(int id, Context context) {
    System.out.println(" --- in CreateAndPlay --->>> " + id);
    if (id >= 0 && id <= 20)
        stopAndRelease(id - 1);
    mPlayers[id] = MediaPlayer.create(context, resources[id]);
    if (mPlayers[id] != null && !mPlayers[id].isPlaying()) {
        mPlayers[id].start();
        mPlayers[id].setLooping(false);
    }
}

public static void stopAndRelease(int id) {
    if (id >= 0) {
        // System.out.println(" --- in StopAndRelease if condition --->>> "+id);
        if (mPlayers[id] != null && mPlayers[id].isPlaying()) {
            // System.out.println("in StopAndRelease 2nd if condition --->>> "+" mPlayers[id] -->> "+mPlayers[id]+" mPlayers[id].isPlaying() -> "+mPlayers[id].isPlaying());
            mPlayers[id].stop();
            mPlayers[id].reset();
            mPlayers[id].release();
            mPlayers[id] = null;
        }
    }
}

public static void mute(int id) {
    if (mPlayers[id] != null && mPlayers[id].isPlaying()) {
        mPlayers[id].pause();
    }
}

public static void unMute(int id) {
    if (mPlayers[id] != null && !mPlayers[id].isPlaying()) {
        mPlayers[id].start();
    }
}

}

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        try {

            // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                vf_letters.setInAnimation(AnimationUtils.loadAnimation(
                        mContext, R.anim.left_in));
                vf_letters.setOutAnimation(AnimationUtils.loadAnimation(
                        mContext, R.anim.left_out));

                if (count < 20) {
                    count += 1;
                    // if (isMute)
                    SoundClass.createAndPlay(count, LettersScreen.this);
                    vf_letters.showNext();
                } else {
                    SoundClass.stopAndRelease(count);
                    count = 0;
                    // if(isMute)
                    SoundClass.createAndPlay(count, LettersScreen.this);
                }
                return true;
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                vf_letters.setInAnimation(AnimationUtils.loadAnimation(
                        mContext, R.anim.right_in));
                vf_letters.setOutAnimation(AnimationUtils.loadAnimation(
                        mContext, R.anim.right_out));

                if (count > 0) {
                    SoundClass.stopAndRelease(count);

                    count -= 1;
                    // if (isMute)
                    SoundClass.createAndPlay(count, LettersScreen.this);
                    vf_letters.showPrevious();
                } else {
                    SoundClass.stopAndRelease(count);
                    count = 20;
                    // if(isMute)
                    SoundClass.createAndPlay(count, LettersScreen.this);
                    vf_letters.showPrevious();

                }

                return true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }
4

1 回答 1

2

这可能并不完美,但提供了有关如何实施的想法

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) 
            {                   
                analogflipper.setInAnimation(slideLeftIn);
                analogflipper.setOutAnimation(slideLeftOut);                    

//                  System.out.println(" Flipping next... ");
                    if(count < 5)
                    {
                        count += 1;  
                        if(isMute)
                            SoundClass.CreateAndPlay(count, AnalogClockTime.this);      
                        analogflipper.showNext();
                    }
                    else
                    {
                        SoundClass.StopAndRelease(count);
                        count = 0;
                        if(isMute)
                            SoundClass.CreateAndPlay(count, AnalogClockTime.this);      
                        analogflipper.showNext();
                    }
                    /*else
                        count = 0;*/

                }  
                else if(e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
                {                       
                    analogflipper.setInAnimation(slideRightIn);
                    analogflipper.setOutAnimation(slideRightOut);                   

//                  System.out.println(" Flipping previous... ");
                    if(count > 0)
                    {
                        SoundClass.StopAndRelease(count);


                    count -= 1;
                    if(isMute)
                        SoundClass.CreateAndPlay(count, AnalogClockTime.this);
                    analogflipper.showPrevious();
                }
                else
                {
                    SoundClass.StopAndRelease(count);  
                    count = 5;
                    if(isMute)
                        SoundClass.CreateAndPlay(count, AnalogClockTime.this);
                    analogflipper.showPrevious();
                }
                /*else
                    count = 4;*/


                }
            } 
            catch (Exception e) 
            {
//                System.out.println("Exception...");
            }
            return false;
        }
    }

此类处理所有声音创建、播放等任务

public class SoundClass 
{   

static MediaPlayer theme1 = null, theme2 = null, theme3 = null, theme4 = null, theme5 = null, theme6 = null;
    static MediaPlayer[] mPlayers = { theme1, theme2, theme3, theme4, theme5, theme6 };
    static Integer[] resources = {R.raw.theme1, R.raw.theme2, R.raw.theme3, R.raw.theme4, R.raw.theme5, R.raw.theme6};
    static Context ctxt = null;

public SoundClass(Context context) 
{
    ctxt = context;
}

public static void createAndPlay(int id, Context context)
{
System.out.println(" --- in CreateAndPlay --->>> "+id);
        if(id >= 0 && id <= 5)
            StopAndRelease(id - 1);
        mPlayers[id] = MediaPlayer.create(context, resources[id]);
        if(mPlayers[id] != null && !mPlayers[id].isPlaying())
        {
            mPlayers[id].start();
            mPlayers[id].setLooping(true);
        }
    }

public static void stopAndRelease(int id)
{
    if(id >= 0)
    {
//          System.out.println(" --- in StopAndRelease if condition --->>> "+id);
            if(mPlayers[id] != null && mPlayers[id].isPlaying())
            {
//              System.out.println("in StopAndRelease 2nd if condition --->>> "+" mPlayers[id] -->> "+mPlayers[id]+" mPlayers[id].isPlaying() -> "+mPlayers[id].isPlaying());
                mPlayers[id].stop();
                mPlayers[id].reset();
                mPlayers[id].release();
                mPlayers[id] = null;
            }
        }       
    }

public static void mute(int id)
{
    if(mPlayers[id] != null && mPlayers[id].isPlaying())
    {
        mPlayers[id].pause();
    }
}

public static void unMute(int id)
{
    if(mPlayers[id] != null && !mPlayers[id].isPlaying())
    {
        mPlayers[id].start();
    }
}
}
于 2012-10-19T09:32:12.503 回答