在水平滚动中实现网格视图(视图鳍状肢)

时间:2012-02-20 09:04:02

标签: android-layout android-emulator

我想在网格视图中实现水平滑动,以便一张幻灯片显示9张图片,下一张幻灯片显示其他9张图片,依此类推。这里的图片来自SD卡。请任何人帮帮忙.. !!

1 个答案:

答案 0 :(得分:0)

经过很长一段时间的努力,我得到了正确的代码

package flipper.view;
import java.io.File;

import java.io.FilenameFilter;
import java.util.Arrays;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class NewflipperActivity extends Activity implements OnClickListener,OnTouchListener{
    ViewFlipper flip;
    Animation animFlipInForeward;
       Animation animFlipOutForeward;
       Animation animFlipInBackward;
       Animation animFlipOutBackward;
       int filepos=0;
       int y;
       private GestureDetector gestureDetector;
        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;
        RelativeLayout rl1,rl2;
    TextView tv1,tv2;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Display dip=getWindowManager().getDefaultDisplay();
        int width=dip.getWidth();
        int actualheight=dip.getHeight();
        System.out.println("width is--->"+width);
        System.out.println("height is--->"+actualheight);

        int layoutheight=(actualheight)/8;
        int flipheight=6*layoutheight;

        RelativeLayout.LayoutParams params1=new RelativeLayout.LayoutParams(width,layoutheight-30);
        params1.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        tv1=(TextView)findViewById(R.id.textView1);
        tv1.setLayoutParams(params1);
        RelativeLayout.LayoutParams params2=new RelativeLayout.LayoutParams(width,layoutheight-30);
        params2.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
        tv2=(TextView)findViewById(R.id.textView2);  
        tv2.setLayoutParams(params2);

        gestureDetector = new GestureDetector(new MyGestureDetector());

        flip=(ViewFlipper)findViewById(R.id.viewFlipper1);
        flip.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return false;
                }
                return true;
            }
      });

        LinearLayout mylay=(LinearLayout)findViewById(R.id.linearlay);
        LinearLayout.LayoutParams layparam=new LinearLayout.LayoutParams(width,flipheight);
        mylay.setLayoutParams(layparam);

        animFlipInForeward = AnimationUtils.loadAnimation(this, R.anim.flipin);
        animFlipOutForeward = AnimationUtils.loadAnimation(this, R.anim.flipout);
        animFlipInBackward = AnimationUtils.loadAnimation(this, R.anim.flipin_reverse);
        animFlipOutBackward = AnimationUtils.loadAnimation(this, R.anim.flipout_reverse);



        String folderpath=Environment.getExternalStorageDirectory()+"/New Folder/";
        File filepath=new File(folderpath);
        File[] mylist=filepath.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String filename) {
                // TODO Auto-generated method stub
                return (filename.toLowerCase().endsWith(".jpg")||filename.toLowerCase().endsWith(".png")||filename.toLowerCase().endsWith(".jpeg"));

            }
        });
       Arrays.sort(mylist);
        int folderlength=mylist.length;
        int mylength=folderlength/9;

        if(folderlength%9==0)
        {
            y=mylength;
        }
        else
        {
            y=(mylength)+1;
        }

     for(int i=0;i<y;i++)
        {System.out.println("abc"+i);
            TableLayout mtable=new TableLayout(this);
            TableLayout.LayoutParams myparams=new TableLayout.LayoutParams(width,flipheight);
            mtable.setLayoutParams(myparams);
            mtable.setWeightSum(3);

           mtable.setOnTouchListener(NewflipperActivity.this);
            for(int j=0;j<3;j++)
            {
                TableRow mrow=new TableRow(this);
                mrow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
                mrow.setOnTouchListener(NewflipperActivity.this);
                for(int k=0;k<3;k++)
                {
                    if(filepos<folderlength)
                    {
                           Log.i("displaying","image at file pos"+mylist[filepos]);
                           final ImageView imageView = new ImageView(this);

                           Bitmap imgBitmap,b1;

                           try
                           {
                              if(mylist[filepos].length()>1500){
                              BitmapFactory.Options bounds = new BitmapFactory.Options();
                              bounds.inSampleSize = 4;
                              imgBitmap = BitmapFactory.decodeFile(mylist[filepos] + "",bounds);
                              b1=Bitmap.createScaledBitmap(imgBitmap, ((int)(width/3))-20, ((int)(flipheight/3))-20, true);
                                   }
                           else
                           {
                               imgBitmap=BitmapFactory.decodeFile(mylist[filepos] + "");
                               b1=Bitmap.createScaledBitmap(imgBitmap, ((int)(width/3))-20,((int)(flipheight/3))-20, true);
                           }
                           imageView.setImageBitmap(b1);
                           imageView.setAdjustViewBounds(true);
                           imageView.setPadding(10,10,10,10);
                           imageView.setTag(mylist[filepos]+"");
                         imageView.setOnTouchListener(NewflipperActivity.this);
                        imageView.setOnClickListener(NewflipperActivity.this);

//                           final String imagename=mylist[filepos].getName();                              

                           }
                           catch (Exception e)
                           {
                               // TODO Auto-generated catch block
                            e.printStackTrace();
                           }


                           mrow.addView(imageView);
                           filepos++;

                    }

                }
                mtable.addView(mrow);
            }
            flip.addView(mtable);

        }

    }
         private void SwipeRight(){
           flip.setInAnimation(animFlipInBackward);
           flip.setOutAnimation(animFlipOutBackward);
            flip.showPrevious();
              }

              private void SwipeLeft(){
              flip.setInAnimation(animFlipInForeward);
            flip.setOutAnimation(animFlipOutForeward);
            flip.showNext();
              }


        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) {
                         Toast.makeText(NewflipperActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
                         SwipeLeft();
                     }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                         Toast.makeText(NewflipperActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
                         SwipeRight();
                     }
                 } catch (Exception e) {
                     e.printStackTrace();
                 }

             return true;
            }

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

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


              }


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 ImageView imagename1 = (ImageView)v;
                 String imgName = (String) imagename1.getTag();

                Toast.makeText(NewflipperActivity.this, "clicked on image-->"+imgName, Toast.LENGTH_SHORT).show();
                Log.e("displaying","clicked onimage::::::"+imgName);

            }
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }

    }