0

我开发了一个相册应用程序,我在其中使用了网格视图。该应用程序正在运行,但每次当我按下返回按钮并返回我的应用程序时,网格视图中都会出现一个图像副本。例如,我在网格视图中有 7 张图像,当我按下后退按钮然后返回应用程序时,它变为 14。下次总图像将是 21 等等。我正在从 SD 卡读取图像。我的应用程序具有全屏功能,所以同样的事情也会发生。我不明白为什么会这样。下面,我发布我的代码:

专辑活动

public class Album3Activity extends Activity {
static File [] mediaFiles;
static File imageDir;
static GridView gridView;
ImageAdapter adapter;
Intent in;
static String name = null;
static ArrayList<Bitmap> bmpArray = new ArrayList<Bitmap>();
static ArrayList<String> fileName = new ArrayList<String>();
public static final String TAG = "Album3Activity";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid);
    imageDir = new File(Environment.getExternalStorageDirectory().toString()+
             "/diplomat");
    mediaFiles = imageDir.listFiles();
    //Log.d("Lenght of images",""+mediaFiles.length);
    for(File file : mediaFiles)
    {
        bmpArray.add(convertToBitmap(file));
        fileName.add(readFileName(file));
        Log.d(TAG + "bmpArray Size", ""+bmpArray.size());
        Log.d(TAG, "call to convertToBitmap");
    }//for

    adapter = new ImageAdapter(this, bmpArray, fileName);
    gridView = (GridView)findViewById(R.id.gridview);
    gridView.setAdapter(adapter);
    gridView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int      position,
                long arg3) {
            in = new Intent(getApplicationContext(), FullScreen.class);
            in.putExtra("id", position);
            startActivity(in);
        }
    });
}//onCreate

public static Bitmap convertToBitmap(File file) 
{
    URL url = null;
    try 
    {
        url = file.toURL();
    } catch (MalformedURLException e1) 
    {
        Log.d(TAG, e1.toString());
    }//catch

    Bitmap bmp = null;
    try
    {
       bmp = BitmapFactory.decodeStream(url.openStream());
       //bmp.recycle();
    }catch(Exception e)
    {
        Log.d(TAG, "Exception: "+e.toString());
    }//catch
    return bmp;
}//convertToBitmap

public String readFileName(File file){
    String name = file.getName();
    return name;
}//readFileName
}//class

图像适配器

public class ImageAdapter extends BaseAdapter {
Activity act;
ArrayList<Bitmap> mFiles;
static ArrayList<String> mName;
public static final String TAG = "ImageAdapter";
static String name = null;
public ImageAdapter(Activity act, ArrayList<Bitmap> mFiles, ArrayList<String>     mName) {
    super();
    this.act = act;
    this.mFiles = mFiles;
    ImageAdapter.mName = mName;
    Log.d("mfiles", "" + this.mFiles.size());
}// ImageAdapter

public int getCount() {
    return mFiles.size();
}// getCount

public Object getItem(int postion) {
    return mFiles.get(postion);
}// getItem

public long getItemId(int position) {
    return 0;
}// getItemId

public static class ViewHolder {
    ImageView iv;
    TextView tv;
}

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder view;
    LayoutInflater li = act.getLayoutInflater();
    if (convertView == null) {
        Log.d("tag", "null");
        view = new ViewHolder();
        convertView = li.inflate(R.layout.gridview_row, null);
        view.iv = (ImageView) convertView.findViewById(R.id.imageView1);
        view.tv = (TextView) convertView.findViewById(R.id.textView1);
        convertView.setTag(view);
    }// if
    else {
        view = (ViewHolder) convertView.getTag();
    }// else
    view.iv.setImageBitmap(mFiles.get(position));
    view.tv.setText(readName(position));
    Log.d(TAG, "getView method called");
    return convertView;
}// getView

public static String  readName(int position) {
    String name = mName.get(position);
    return name;
}//readName

    }// ImageAdapter

全屏.java

public class FullScreen extends Activity implements OnClickListener, OnGestureListener{
ImageView imageView1;
ViewSwitcher viewSwitcher;
ImageAdapter adapter = new ImageAdapter(getParent(), Album3Activity.bmpArray, Album3Activity.fileName);
Button avbryt, camera, trash, opendialog, slett, 
    avbryt1, epost, mms, avbryt2, facebook;
TextView textView;
ImageButton btnNext, btnPrevious;
GestureDetector gesturedetector = null;
Intent in; 
int touches; 
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.fullscreen);
    viewSwitcher = (ViewSwitcher)findViewById(R.id.viewSwitcher);
    imageView1 = (ImageView)findViewById(R.id.imageView1);
    textView = (TextView)findViewById(R.id.textView1);

    in = getIntent();
    touches = in.getExtras().getInt("id");
    imageView1.setImageBitmap((Bitmap)adapter.getItem(touches)); 
    textView.setText(ImageAdapter.readName(touches));

    btnNext = (ImageButton)findViewById(R.id.btnNext);
    btnNext.setOnClickListener(this);

    btnPrevious = (ImageButton)findViewById(R.id.btnPrevious);
    btnPrevious.setOnClickListener(this);

    trash = (Button)findViewById(R.id.trash);
    trash.setOnClickListener(this);

    opendialog = (Button)findViewById(R.id.opendialog);
    opendialog.setOnClickListener(this);

    avbryt = (Button)findViewById(R.id.avbryt);
    avbryt.setOnClickListener(this);

    camera = (Button)findViewById(R.id.camera_albumbutton);
    camera.setOnClickListener(this);

    gesturedetector=new GestureDetector(this, this);
}//onCreate

    public void onClick(View v) {

    switch (v.getId()) {
    case R.id.btnNext:
        Animation animationNext = AnimationUtils.loadAnimation(this, R.anim.btn_style_next);
        btnNext.startAnimation(animationNext);
        nextView();
        break;

    case R.id.btnPrevious:
        Animation animationPrevious = AnimationUtils.loadAnimation(this, R.anim.btn_style_previous);
        btnPrevious.startAnimation(animationPrevious);
        previousView();
        break;

    case R.id.trash:
        final Dialog dialog  = new Dialog(FullScreen.this);
        dialog.setContentView(R.layout.trashdialog);
        slett = (Button)dialog.findViewById(R.id.slett);
        avbryt1 = (Button)dialog.findViewById(R.id.avbryt);

        slett.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                File file = new File(Environment.getExternalStorageDirectory()+
                            "/diplomat/"+ ImageAdapter.readName(touches));
                Log.d("FullScreen Path", file.getAbsolutePath());
                Log.d("Hi1 touches value", ""+touches);
                try
                {

                    if(touches == (Album3Activity.bmpArray.size() - 1))
                    {
                        file.delete();
                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED
                                , Uri.parse(Environment.getExternalStorageDirectory().toString()+"/diplomat")));
                        Album3Activity.bmpArray.remove(touches);
                        Log.d("Hi2 new bmpArray size", ""+Album3Activity.bmpArray.size());

                        Album3Activity.fileName.remove(touches);
                        Log.d("Hi2 new fileName size", ""+Album3Activity.fileName.size());

                        Album3Activity.gridView.invalidateViews();
                        adapter.notifyDataSetChanged();

                        touches = Album3Activity.bmpArray.size() - touches;
                        Log.d("FullScreen delete:if block", "Updated touches value:"+touches);

                        do
                        {
                            Log.d("FullScreen:delete if", "in while loop");
                            imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
                            textView.setText(ImageAdapter.readName(touches));
                            break;
                        }//do
                        while(Album3Activity.bmpArray.isEmpty() == false);
                    }//if
                    else
                    {
                        Log.d("fullscreen:delete else", "else block executed");
                        file.delete();
                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED
                                , Uri.parse(Environment.getExternalStorageDirectory().toString()+"/diplomat")));

                        Album3Activity.bmpArray.remove(touches);
                        Log.d("Hi2 new bmpArray size", ""+Album3Activity.bmpArray.size());

                        Album3Activity.fileName.remove(touches);
                        Log.d("Hi2 new fileName size", ""+Album3Activity.fileName.size());

                        Album3Activity.gridView.invalidateViews();
                        adapter.notifyDataSetChanged();

                        imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
                        textView.setText(ImageAdapter.readName(touches));

                    }//else
                }//try
                catch(IndexOutOfBoundsException e)
                {
                    Log.d("Fullscreen delete method", e.toString());
                    Toast.makeText(FullScreen.this, "No more image left to diaplay"
                            , Toast.LENGTH_SHORT).show();
                }//catch
                finally
                {
                    dialog.dismiss();
                }//finally
            }//onClick
        });

        avbryt1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog.cancel();
            }//onClick
        });
        dialog.show();
        break;

    case R.id.opendialog:
        final Dialog dialog1 = new Dialog(FullScreen.this);
        dialog1.setContentView(R.layout.openbuttondialog);
        epost = (Button)dialog1.findViewById(R.id.epost);
        mms = (Button)dialog1.findViewById(R.id.mms);
        facebook = (Button)dialog1.findViewById(R.id.facebook);
        avbryt2 =(Button)dialog1.findViewById(R.id.openavbryt);

        epost.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }//onClick
        });

        mms.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }//onClick
        });

        facebook.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }//onClick
        });

        avbryt2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog1.cancel();
            }//onClick
        });
        dialog1.show();
        break;

    case R.id.avbryt:
        break;

    case R.id.camera_albumbutton:
        break;

    default:
        break;
    }//switch

}//onClick

private void previousView() {
    if(touches > 0){
        touches = touches - 1;
        imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
        textView.setText(ImageAdapter.readName(touches));
        viewSwitcher.setInAnimation(this, R.anim.in_animation1);
        viewSwitcher.setOutAnimation(this, R.anim.out_animation1);
        viewSwitcher.showPrevious();
    }//if
    else{
        Toast.makeText(this, "No image left", Toast.LENGTH_SHORT).show();
    }//else
    Log.d("Switcher", "previousView");
}//previousView

private void nextView() {
    if(touches < Album3Activity.bmpArray.size() - 1){
        touches = touches + 1;
        imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
        textView.setText(ImageAdapter.readName(touches));
        viewSwitcher.setInAnimation(this, R.anim.in_animation);
        viewSwitcher.setOutAnimation(this, R.anim.out_animation);
        viewSwitcher.showNext();
    }//if
    else{
        Toast.makeText(this, "No image left", Toast.LENGTH_SHORT).show();
    }//else
    Log.d("Switcher", "nextView");
}//nextView

public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    return true;
}


int SWIPE_MIN_VELOCITY = 50;
int SWIPE_MIN_DISTANCE = 50;


public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    float ev1X = e1.getX();
    float ev2X = e2.getX();
    Log.d("On", "Onfling");
    //Get distance of X (e1) to X (e2)
    final float xdistance = Math.abs(ev1X - ev2X);
    //Get veclocity of cusor
    //Vận tốc = số điểm ảnh (px) / giây
    final float xvelocity = Math.abs(velocityX);

    //Vận tốc chuyển đổi X > 100 và khoảng cách từ điểm kéo đầu đến điểm kéo cuối > 100
    if( (xvelocity > SWIPE_MIN_VELOCITY) && (xdistance > SWIPE_MIN_DISTANCE) )
    {
        if(ev1X > ev2X)//Switch Left
        {
            previousView();
        }
        else//Switch Right
        {
            nextView();
        }
    }

    return false;
}

public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub

}

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    // TODO Auto-generated method stub
    return false;
}

public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub

}

public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d("on touch","touch");
    // TODO Auto-generated method stub
    //return gesturedetector.onTouchEvent(event);

    this.gesturedetector.onTouchEvent(event);
      return super.onTouchEvent(event);
}

    }//FullScreen
4

3 回答 3

2

您将所有变量声明为static? static变量在应用程序中只维护一份副本。它将文件名添加到同一个副本。如果您不想重复值,最好使用HashSet而不是ArrayList.

static HashSet<Bitmap> bmpArray = new HashSet<Bitmap>();
static HashSet<String> fileName = new HashSet<String>();
于 2012-05-17T13:03:18.963 回答
0

专辑活动

public class Album3Activity extends Activity {
File [] mediaFiles;
File imageDir;
static GridView gridView;
ImageAdapter adapter;
Intent in;
String name = null;
ArrayList<Bitmap> bmpArray = new ArrayList<Bitmap>();
ArrayList<String> fileName = new ArrayList<String>();
public static final String TAG = "Album3Activity";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid);
    imageDir = new File(Environment.getExternalStorageDirectory().toString()+
             "/diplomat");
    mediaFiles = imageDir.listFiles();
    //Log.d("Length of images",""+mediaFiles.length);
    try
    {
        for(File file : mediaFiles)
        {
            bmpArray.add(convertToBitmap(file));
            fileName.add(readFileName(file));
            Log.d(TAG + "bmpArray Size", ""+bmpArray.size());
            Log.d(TAG, "call to convertToBitmap");
        }//for
    }//try
    catch(Exception e)
    {
        Log.d(TAG, e.toString());
        Toast.makeText(Album3Activity.this, "No files available", Toast.LENGTH_SHORT).show();
    }//catch
    adapter = new ImageAdapter(this, bmpArray, fileName);
    gridView = (GridView)findViewById(R.id.gridview);
    gridView.setAdapter(adapter);
    gridView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            in = new Intent(getApplicationContext(), FullScreen.class);
            in.putExtra("id", position);
            startActivity(in);
        }
    });
}//onCreate

public static Bitmap convertToBitmap(File file) 
{
    URL url = null;
    try 
    {
        url = file.toURL();
    } catch (MalformedURLException e1) 
    {
        Log.d(TAG, e1.toString());
    }//catch

    Bitmap bmp = null;
    try
    {
       bmp = BitmapFactory.decodeStream(url.openStream());
       //bmp.recycle();
    }catch(Exception e)
    {
        Log.d(TAG, "Exception: "+e.toString());
    }//catch
    return bmp;
}//convertToBitmap

public String readFileName(File file){
    String name = file.getName();
    return name;
}//readFileName
}//class

图像适配器

public class ImageAdapter extends BaseAdapter {
Activity act;
static ArrayList<Bitmap> mFiles;
static ArrayList<String> mName;
public static final String TAG = "ImageAdapter";
String name = null;
public ImageAdapter(Activity act, ArrayList<Bitmap> mFiles, ArrayList<String> mName) {
    super();
    this.act = act;
    ImageAdapter.mFiles = mFiles;
    ImageAdapter.mName = mName;
    Log.d("mfiles", "" + ImageAdapter.mFiles.size());
}// ImageAdapter

public int getCount() {
    return mFiles.size();
}// getCount

public Object getItem(int postion) {
    return mFiles.get(postion);
}// getItem

public long getItemId(int position) {
    return 0;
}// getItemId

public static class ViewHolder {
    ImageView iv;
    TextView tv;
}

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder view;
    LayoutInflater li = act.getLayoutInflater();
    if (convertView == null) {
        Log.d("tag", "null");
        view = new ViewHolder();
        convertView = li.inflate(R.layout.gridview_row, null);
        view.iv = (ImageView) convertView.findViewById(R.id.imageView1);
        view.tv = (TextView) convertView.findViewById(R.id.textView1);
        convertView.setTag(view);
    }// if
    else {
        view = (ViewHolder) convertView.getTag();
    }// else
    view.iv.setImageBitmap(mFiles.get(position));
    view.tv.setText(readName(position));
    Log.d(TAG, "getView method called");
    return convertView;
}// getView

public String readName(int position) {
    String name = mName.get(position);
    return name;
}//readName

}// ImageAdapter

全屏

public class FullScreen extends Activity implements OnClickListener, OnGestureListener{
ImageView imageView1;
ViewSwitcher viewSwitcher;
ImageAdapter adapter = new ImageAdapter(getParent(), ImageAdapter.mFiles, ImageAdapter.mName);
Button avbryt, camera, trash, opendialog, slett, 
    avbryt1, epost, mms, avbryt2, facebook;
TextView textView;
ImageButton btnNext, btnPrevious;
GestureDetector gesturedetector = null;
Intent in; 
int touches; 

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.fullscreen);
    viewSwitcher = (ViewSwitcher)findViewById(R.id.viewSwitcher);
    imageView1 = (ImageView)findViewById(R.id.imageView1);
    textView = (TextView)findViewById(R.id.textView1);

    in = getIntent();
    touches = in.getExtras().getInt("id");
    imageView1.setImageBitmap((Bitmap)adapter.getItem(touches)); 
    textView.setText(adapter.readName(touches));
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED
            , Uri.parse(Environment.getExternalStorageDirectory().toString()+"/diplomat")));

    btnNext = (ImageButton)findViewById(R.id.btnNext);
    btnNext.setOnClickListener(this);

    btnPrevious = (ImageButton)findViewById(R.id.btnPrevious);
    btnPrevious.setOnClickListener(this);

    trash = (Button)findViewById(R.id.trash);
    trash.setOnClickListener(this);

    opendialog = (Button)findViewById(R.id.opendialog);
    opendialog.setOnClickListener(this);

    avbryt = (Button)findViewById(R.id.avbryt);
    avbryt.setOnClickListener(this);

    camera = (Button)findViewById(R.id.camera_albumbutton);
    camera.setOnClickListener(this);

    gesturedetector=new GestureDetector(this, this);
}//onCreate

公共无效 onClick(查看 v){

    switch (v.getId()) {
    case R.id.btnNext:
        Animation animationNext = AnimationUtils.loadAnimation(this, R.anim.btn_style_next);
        btnNext.startAnimation(animationNext);
        nextView();
        break;

    case R.id.btnPrevious:
        Animation animationPrevious = AnimationUtils.loadAnimation(this, R.anim.btn_style_previous);
        btnPrevious.startAnimation(animationPrevious);
        previousView();
        break;

    case R.id.trash:
        final Dialog dialog  = new Dialog(FullScreen.this);
        dialog.setContentView(R.layout.trashdialog);
        slett = (Button)dialog.findViewById(R.id.slett);
        avbryt1 = (Button)dialog.findViewById(R.id.avbryt);

        slett.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                File file = new File(Environment.getExternalStorageDirectory()+
                            "/diplomat/"+ adapter.readName(touches));
                Log.d("FullScreen Path", file.getAbsolutePath());
                Log.d("Hi1 touches value", ""+touches);
                try
                {

                    if(touches == (ImageAdapter.mFiles.size() - 1))
                    {
                        file.delete();
                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED
                                , Uri.parse(Environment.getExternalStorageDirectory().toString()+"/diplomat")));
                        ImageAdapter.mFiles.remove(touches);
                        Log.d("Hi2 new bmpArray size", ""+ImageAdapter.mFiles.size());

                        ImageAdapter.mName.remove(touches);
                        Log.d("Hi2 new fileName size", ""+ImageAdapter.mName.size());

                        Album3Activity.gridView.invalidateViews();
                        adapter.notifyDataSetChanged();

                        touches = ImageAdapter.mFiles.size() - touches;
                        Log.d("FullScreen delete:if block", "Updated touches value:"+touches);

                        do
                        {
                            Log.d("FullScreen:delete if", "in while loop");
                            imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
                            textView.setText(adapter.readName(touches));
                            break;
                        }//do
                        while(ImageAdapter.mFiles.isEmpty() == false);
                    }//if
                    else
                    {
                        Log.d("fullscreen:delete else", "else block executed");
                        file.delete();
                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED
                                , Uri.parse(Environment.getExternalStorageDirectory().toString()+"/diplomat")));

                        ImageAdapter.mFiles.remove(touches);
                        Log.d("Hi2 new bmpArray size", ""+ImageAdapter.mFiles.size());

                        ImageAdapter.mName.remove(touches);
                        Log.d("Hi2 new fileName size", ""+ImageAdapter.mName.size());

                        Album3Activity.gridView.invalidateViews();
                        adapter.notifyDataSetChanged();

                        imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
                        textView.setText(adapter.readName(touches));

                    }//else
                }//try
                catch(IndexOutOfBoundsException e)
                {
                    Log.d("Fullscreen delete method", e.toString());
                    Toast.makeText(FullScreen.this, "No more image left to diaplay"
                            , Toast.LENGTH_SHORT).show();
                }//catch
                finally
                {
                    dialog.dismiss();
                }//finally
            }//onClick
        });

        avbryt1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog.cancel();
            }//onClick
        });
        dialog.show();
        break;

    case R.id.opendialog:
        final Dialog dialog1 = new Dialog(FullScreen.this);
        dialog1.setContentView(R.layout.openbuttondialog);
        epost = (Button)dialog1.findViewById(R.id.epost);
        mms = (Button)dialog1.findViewById(R.id.mms);
        facebook = (Button)dialog1.findViewById(R.id.facebook);
        avbryt2 =(Button)dialog1.findViewById(R.id.openavbryt);

        epost.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }//onClick
        });

        mms.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }//onClick
        });

        facebook.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

            }//onClick
        });

        avbryt2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog1.cancel();
            }//onClick
        });
        dialog1.show();
        break;

    case R.id.avbryt:
        break;

    case R.id.camera_albumbutton:
        break;

    default:
        break;
    }//switch

}//onClick

private void previousView() {
    if(touches > 0){
        touches = touches - 1;
        imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
        textView.setText(adapter.readName(touches));
        viewSwitcher.setInAnimation(this, R.anim.in_animation1);
        viewSwitcher.setOutAnimation(this, R.anim.out_animation1);
        viewSwitcher.showPrevious();
    }//if
    else{
        Toast.makeText(this, "No image left", Toast.LENGTH_SHORT).show();
    }//else
    Log.d("Switcher", "previousView");
}//previousView

private void nextView() {
    if(touches < ImageAdapter.mFiles.size() - 1){
        touches = touches + 1;
        imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
        textView.setText(adapter.readName(touches));
        viewSwitcher.setInAnimation(this, R.anim.in_animation);
        viewSwitcher.setOutAnimation(this, R.anim.out_animation);
        viewSwitcher.showNext();
    //}//if
    //else{
        //Toast.makeText(this, "No image left", Toast.LENGTH_SHORT).show();
        while(touches == (ImageAdapter.mFiles.size() - 1))
        {
            touches = 0;
            imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
            touches = touches + 1;
            break;
        }//while
    }//else
    Log.d("Switcher", "nextView");
}//nextView

public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    return true;
}


int SWIPE_MIN_VELOCITY = 50;
int SWIPE_MIN_DISTANCE = 50;


public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    float ev1X = e1.getX();
    float ev2X = e2.getX();
    Log.d("On", "Onfling");
    //Get distance of X (e1) to X (e2)
    final float xdistance = Math.abs(ev1X - ev2X);
    //Get veclocity of cusor
    //Vận tốc = số điểm ảnh (px) / giây
    final float xvelocity = Math.abs(velocityX);

    //Vận tốc chuyển đổi X > 100 và khoảng cách từ điểm kéo đầu đến điểm kéo cuối > 100
    if( (xvelocity > SWIPE_MIN_VELOCITY) && (xdistance > SWIPE_MIN_DISTANCE) )
    {
        if(ev1X > ev2X)//Switch Left
        {
            previousView();
        }
        else//Switch Right
        {
            nextView();
        }
    }

    return false;
}

public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub

}

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    // TODO Auto-generated method stub
    return false;
}

public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub

}

public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d("on touch","touch");
    // TODO Auto-generated method stub
    //return gesturedetector.onTouchEvent(event);

    this.gesturedetector.onTouchEvent(event);
      return super.onTouchEvent(event);
}

}//FullScreen
于 2012-05-18T07:24:47.503 回答
0

现在,当您离开应用程序时,正在创建一个捆绑包,当您返回时,系统会使用它来将您恢复到原来的位置(给您原来的七个),然后您再次添加它们,因为onCreate再次运行。

你需要检查一个 savedInstanceState,如果它存在,不要做任何事情。你应该在你的所有onCreate方法中都有这个。

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

    if (savedInstanceState == null) { 
         // Do your oncreate stuff because there is no bundle    
    } 
// Do stuff that needs to be done even if there is a saved instance, or do nothing 
} 
于 2012-05-17T13:08:03.160 回答