如何在选择时使图像聚焦

时间:2012-07-26 20:33:38

标签: android

我找到了堆栈溢出的解决方案,当我实现这个回答链接在下面时我发现了一个错误(R.styleable.Gallery1)是什么?它是任何特定值或单独的文件夹? android circular gallery?是单独的文件夹还是ny特定值? 代码是这样的:

public class TestGallery extends Activity {
    /** Called when the activity is first created. */

    private Integer[] mImageIds = { R.drawable.sample_1, R.drawable.sample_2,       
    R.drawable.sample_3, R.drawable.sample_4 }; 

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

        Gallery g = (Gallery) findViewById(R.id.gallery); 
        g.setAdapter(new ImageAdapter(this)); 

        g.setOnItemClickListener(new OnItemClickListener() { 
            public void onItemClick(AdapterView parent, View v, int position, long id) { 
                if (position >= mImageIds.length) { 
                    position = position % mImageIds.length; 
                }
                Toast.makeText(TestGallery.this, "" + position, Toast.LENGTH_SHORT).show(); 
            } 
        });
    }

    public class ImageAdapter extends BaseAdapter { 
         int mGalleryItemBackground; 
         private Context mContext; 

        public ImageAdapter(Context c) { 
            mContext = c; 
            TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); 
            mGalleryItemBackground =     
        a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0); 

            a.recycle(); 
        } 

        public int getCount() { 
            return Integer.MAX_VALUE; 
        } 

        public Object getItem(int position) { 
            if (position >= mImageIds.length) { 
                position = position % mImageIds.length; 
            } 
            return position; 
        } 

        public long getItemId(int position) { 
            if (position >= mImageIds.length) { 
                position = position % mImageIds.length; 
            } 
            return position; 
        } 

        public View getView(int position, View convertView, ViewGroup parent) { 
            ImageView i = new ImageView(mContext); 
            if (position >= mImageIds.length) { 
                position = position % mImageIds.length; 
            } 
            i.setImageResource(mImageIds[position]); 
            i.setLayoutParams(new Gallery.LayoutParams(80, 80)); 
            i.setScaleType(ImageView.ScaleType.FIT_XY); 
            i.setBackgroundResource(mGalleryItemBackground); 
            return i; 
         } 

         public int checkPosition(int position) { 
            if (position >= mImageIds.length) { 
                position = position % mImageIds.length; 
            } 
            return position; 
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在您的图库中设置以下代码

g.setFocusable(true);
g.setFocusableInTouchMode(true);

会聚焦图像。

它为我而战。