2011-04-29 2 views
0

두 가지 내가 갤러리보기를 달성 할 수 있습니다 : 사용자가 갤러리보기에서 선택 나는 (이미지가 이동하지 않으을 할 때갤러리를 사용하여 이미지를 터치 한 후 선택 영역을 강조 표시하고 행을 이동하지 않는 방법은 무엇입니까?

1) 즉 내가 선택한 이미지를 원하지 않는다

2) 사용자가 갤러리보기에서 선택하면 ACTION_DOWN에서 선택한 이미지가 주황색으로 강조 표시되고 ACTION_UP에서 강조 표시가 제거됩니다 (버튼과 마찬가지로).

어떻게 완료 되나요?

+0

내가 돈을 ' 알았어. 당신이 묘사하는 것은 갤러리와 공통점이 없습니다. 왜 GridView를 사용하여 모든 것을 배치하지 않는가? 2)는 자동으로 수행되고 1) Gallery가 아니기 때문에 1) GridView가 발생하지 않습니다. – WarrenFaith

+0

이미지의 1 행이있는 수평 스크롤 격자보기를 보여 주면 내가 원하는 것을 얻게됩니다. –

+0

그래서 수평 스크롤이 필요합니다 (언급하지 않았습니다). 거기에 좋은 해결 방법은 가로 스크롤보기와 하나의 행이있는 tableview 있습니다. 그럴거야. 검색을 통한 기본 아이디어 : http://groups.google.com/group/android-developers/msg/20f56658082576af – WarrenFaith

답변

3

GODAWFUL 를 사용하지 않고

다음과 같은 기능

  • 선택 감지 스크롤이 샘플 코드

    1. 분명히-끝이 수평 에 포함되어 있습니다 ... 그것을 자신을 해결 Gallery.setOnItemSelectedListener(). 선택 을 강조
    2. (이것은 더 많은 작업이 필요하지만 - 즉이 오프 스크린 스크롤있어 후 갤러리 강조를 잃게됩니다 때문에 선택 저장)

    public class ActivityMine extends Activity implements OnTouchListener 
    { 
        //UI references 
        private Gallery mGallery; 
    
        //member variables 
        private float fX, fY; 
        private ImageBuffer mImageBuffer;//just an array of images 
    
    
        @Override 
        protected void onCreate(Bundle savedInstanceState) 
        { 
         super.onCreate(savedInstanceState); 
         setContentView(R.layout.filter); 
    
         //get references to UI components 
         mGallery = (Gallery) findViewById(R.id.filter_gallery); 
    
         //make widgets touch sensitive 
         mGallery.setOnTouchListener(this); 
    
         //connect Gallery with adapter 
         mGallery.setAdapter(new GalleryAdapter(this, mImageBuffer)); 
    
         //auto-select first image (position 1073741820) 
         final int iTemp = (int)(Integer.MAX_VALUE/2) - ((Integer.MAX_VALUE/2) % mImageBuffer.getCount()); 
         mGallery.setSelection(iTemp); 
        } 
    
    
        @Override 
        protected void onDestroy() 
        { 
         super.onDestroy(); 
        } 
    
    
        public boolean onTouch(View v, MotionEvent event) 
        { 
         final int actionPerformed = event.getAction(); 
         final int widgetID = v.getId(); 
    
         if (actionPerformed == MotionEvent.ACTION_DOWN) 
         { 
          if (widgetID == R.id.filter_gallery) 
          { 
           fX=event.getRawX(); 
           fY=event.getRawY(); 
          } 
         } 
    
         if (actionPerformed == MotionEvent.ACTION_UP) 
         { 
          switch (widgetID) 
          { 
           case R.id.filter_gallery: 
           { 
            final float posX = event.getRawX(); 
            final float posY = event.getRawY(); 
    
            final float MAXPRESSRANGE = 10.0f; 
    
            //detect if user performed a simple press 
            if ((posX < fX+MAXPRESSRANGE) && (posX > fX-MAXPRESSRANGE)) 
            { 
             if ((posY < fY+MAXPRESSRANGE) && (posY > fY-MAXPRESSRANGE)) 
             { 
              //valid press detected! 
    
              //convert gallery coordinates to a position 
              final int iPosition = mGallery.pointToPosition((int)event.getX(), (int)event.getY()); 
    
              if (iPosition != AdapterView.INVALID_POSITION) 
              { 
               //this is necessary to obtain the index of the view currently visible and pressed 
               final int iVisibleViewIndex = iPosition - mGallery.getFirstVisiblePosition(); 
    
               //get a reference to the child and modify the border 
               View child = mGallery.getChildAt(iVisibleViewIndex); 
               if (child != null) 
               { 
                RelativeLayout borderImg = (RelativeLayout)child; 
                borderImg.setBackgroundColor(0xFFFFFFFF); 
               } 
              } 
    
              // consume event 
              return true; 
             } 
            } 
           }//end of: case R.id.filter_gallery 
          }//end of: switch() 
         } 
    
         return false;//do not consume event otherwise the UI widget touched won't receive the event 
        } 
    
    
        public class GalleryAdapter extends BaseAdapter 
        { 
         //member variables 
         private Context mContext; 
         private ImageBuffer mImageBuffer; 
    
    
         public GalleryAdapter(Context context, ImageBuffer imageBuffer) 
         { 
          mContext = context; 
          mImageBuffer = imageBuffer; 
         } 
    
         //get count of images in the gallery 
         public int getCount() 
         { 
          return Integer.MAX_VALUE; 
         } 
    
         public Object getItem(int position) 
         { 
          if (position >= mImageBuffer.getCount()) 
          { 
           position = position % mImageBuffer.getCount(); 
          } 
          return position; 
         } 
    
         public long getItemId(int position) 
         { 
          if (position >= mImageBuffer.getCount()) 
          { 
           position = position % mImageBuffer.getCount(); 
          } 
          return position; 
         } 
    
         //returns the individual images to the widget as it requires them 
         public View getView(int position, View convertView, ViewGroup parent) 
         { 
          final ImageView imgView = new ImageView(mContext); 
    
          if (position >= mImageBuffer.getCount()) 
          { 
           position = position % mImageBuffer.getCount(); 
          } 
    
          imgView.setImageBitmap(mImageBuffer.getBitmapFromBuffer(position)); 
    
          //put black borders around the image 
          final RelativeLayout borderImg = new RelativeLayout(mContext); 
          borderImg.setPadding(5, 5, 5, 5); 
          borderImg.setBackgroundColor(0xff000000); 
          borderImg.addView(imgView); 
          return borderImg; 
         } 
        } 
    } 
    
    
    
    import java.io.File; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.util.ArrayList; 
    import java.util.HashMap; 
    import java.util.List; 
    import java.util.Map; 
    
    import android.app.Activity; 
    import android.content.res.AssetManager; 
    import android.graphics.Bitmap; 
    import android.graphics.BitmapFactory; 
    import android.os.Environment; 
    
    public class ImageBuffer 
    { 
        //member variables 
        private Activity mActivity; 
        private Map<String,Integer> mIndexMap; 
        private List<Bitmap> mBitmapList; 
        private int mImageCount; 
    
    
        public ImageBuffer(Activity activity) 
        { 
         mActivity = activity; 
         mIndexMap = new HashMap<String,Integer>(); 
         mBitmapList = new ArrayList<Bitmap>(); 
         mImageCount=0; 
        } 
    
    
        public int getCount() 
        { 
         return mImageCount; 
        } 
    
    
        public int putAssetBitmapInBuffer(String assetfilename, String overrideFilename) 
        { 
         int iValueToReturn = -1; 
    
         try 
         { 
          Bitmap bitmap = getBitmapFromAsset(assetfilename); 
          if (bitmap != null) 
          { 
           final int iIndex = mBitmapList.size(); 
           if (overrideFilename == null) 
           { 
            mIndexMap.put(assetfilename, iIndex); 
           } 
           else 
           { 
            mIndexMap.put(overrideFilename, iIndex); 
           } 
           mBitmapList.add(bitmap); 
           iValueToReturn = iIndex; 
           mImageCount++; 
          } 
         } 
         catch (IOException e) 
         { 
          e.printStackTrace(); 
         } 
    
         return iValueToReturn; 
        } 
    
    
        /** 
        * put an image from filesystem folder into buffer 
        * @param filename name of file, i.e. "image.png" 
        * @param strSubFolder subfolder, i.e. "/products" 
        * @param bGetFromSDCARD when true the image is read from SDCARD, otherwise main memory 
        * @return on success: index of image in buffer. on error: -1 
        */ 
        public int putLocalFSbitmapInBuffer(String filename, String strSubFolder, boolean bGetFromSDCARD) 
        { 
         int iValueToReturn = -1; 
    
         Bitmap bitmap = getBitmapFromLocalFS(filename, strSubFolder, bGetFromSDCARD); 
         if (bitmap != null) 
         { 
          final int iIndex = mBitmapList.size(); 
          mIndexMap.put(filename, iIndex); 
          mBitmapList.add(bitmap); 
          iValueToReturn = iIndex; 
          mImageCount++; 
         } 
    
         return iValueToReturn; 
        } 
    
    
        public Bitmap getBitmapFromBuffer(String assetfilename) 
        { 
         Bitmap bitmap = null; 
    
         if (mIndexMap.containsKey(assetfilename)) 
         { 
          final int iIndex = mIndexMap.get(assetfilename); 
          bitmap = mBitmapList.get(iIndex); 
         } 
    
         return bitmap; 
        } 
    
    
        public Bitmap getBitmapFromBuffer(int position) 
        { 
         Bitmap bitmap = null; 
    
         if (position < mBitmapList.size() && position > -1) 
         { 
          bitmap = mBitmapList.get(position); 
         } 
    
         return bitmap; 
        } 
    
    
        /** 
        * get bitmap from assets folder in project 
        * @param assetfilename name of asset, i.e. "image.png" 
        * @return Bitmap 
        * @exception IOException when file is not found 
        */ 
        private Bitmap getBitmapFromAsset(String assetfilename) throws IOException 
        { 
         AssetManager assetManager = mActivity.getAssets(); 
    
         InputStream istr = assetManager.open(assetfilename); 
         Bitmap bitmap = BitmapFactory.decodeStream(istr); 
         istr.close(); 
    
         return bitmap; 
        } 
    
    
        /** 
        * get bitmap from file system 
        * @param filename name of image, i.e. "image.png" 
        * @param strSubFolder subfolder, i.e. "/products" 
        * @param bGetFromSDCARD when true the image is read from SDCARD, otherwise main memory 
        * @return Bitmap 
        */ 
        private Bitmap getBitmapFromLocalFS(String filename, String strSubFolder, boolean bGetFromSDCARD) 
        { 
         File file; 
    
         if (bGetFromSDCARD == true) 
         { 
          //store in SDCARD 
          file = new File(Environment.getExternalStorageDirectory()+strSubFolder, filename); 
         } 
         else 
         { 
          //store in main memory 
          file = new File(mActivity.getFilesDir().getAbsolutePath()+strSubFolder, filename); 
         } 
    
    
         Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); 
    
         return bitmap; 
        } 
    
    } 
    
  • +0

    ImageBuffer는 무엇입니까, 그것은 사용자 정의 클래스입니까? 그것을 제공하십시오. – Ankit

    +0

    Ankit - 코드를 업데이트했습니다. –

    관련 문제