突出无边界的画廊

时间:2011-10-21 22:09:08

标签: android gallery

如何在不向图像添加灰色边框的情况下突出显示图库所选项目, 不使用它。

TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(
    R.styleable.GalleryTheme_android_galleryItemBackground, 3);
typArray.recycle();

我可以为图像添加反射,

1 个答案:

答案 0 :(得分:1)

您可以像这样定义自己的视图:

自定义背景:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" android:layout_width="wrap_content">
            <stroke android:width="1dp" android:color="#FF000000" />
            <solid android:color="#00000000" />
            <padding android:left="1dp" android:top="1dp" android:right="1dp"
                android:bottom="1dp" />
            <corners android:radius="1dp" />

        </shape>
    </item>

    <item android:top="1dp" android:bottom="1dp">
        <shape android:shape="rectangle">
            <gradient android:startColor="#252525" android:endColor="#252525"
                android:angle="270" android:centerColor="#545454" />
            <!-- border width and color -->
            <stroke android:width="1dp" android:color="#FFDDDDDD" />
        </shape>
    </item>

</layer-list>

其适配器:

public class AdapterGalleryProducts extends ArrayAdapter<String> {

    private int ITEM_WIDTH = 136;
    private int ITEM_HEIGHT = 88;

    private final int mGalleryItemBackground;
    private final Context mContext;
    private final float mDensity;

    public AdapterGalleryProducts(Context context, int resource,
            List<String> items) {
        super(context, resource, items);

        mContext = context;

        TypedArray a = mContext
                    .obtainStyledAttributes(R.styleable.Gallery1);
        mGalleryItemBackground = R.drawable.background02;

        a.recycle();

        mDensity = mContext.getResources().getDisplayMetrics().density;
        boInvProducts = new BoInvProducts(mContext);
    }

    public void setImageSize(int width, int height) {
        ITEM_WIDTH = width;
        ITEM_HEIGHT = height;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            convertView = new ImageView(mContext);

            imageView = (ImageView) convertView;
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(
                    (int) (ITEM_WIDTH * mDensity + 0.5f),
                    (int) (ITEM_HEIGHT * mDensity + 0.5f)));

            // The preferred Gallery item background
            imageView.setBackgroundResource(mGalleryItemBackground);
            imageView.setPadding(5, 5, 5, 5);
        } else {
            imageView = (ImageView) convertView;
        }

        Bitmap bitmap = null;
        try {
            bitmap = getBitmapByFilePath(getItem(position));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
            imageView.setAdjustViewBounds(true);
        }

        return imageView;
    }
}

并将动画效果添加到所选项目:

gal.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                gal_onItemClick(parent, v, position, id);
            }
        });



protected void gal_onItemClick(AdapterView<?> parent, View v,
            int position, long id) {        

        // animate selected image
        Animation growAnimation = AnimationUtils.loadAnimation(this,
                R.anim.grow_shrink_image);
        v.startAnimation(growAnimation);
}

动画示例(grow_shrink_image.xml):

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="200" android:fromXScale="1.0" android:toXScale="1.20"
        android:fromYScale="1.0" android:toYScale="1.20" android:pivotX="50%"
        android:pivotY="50%" android:interpolator="@android:anim/accelerate_interpolator"
        android:fillAfter="false" />
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:startOffset="200" android:duration="200" android:fromXScale="1.0"
        android:toXScale="0.8333" android:fromYScale="1.0" android:toYScale="0.8333"
        android:pivotX="50%" android:pivotY="50%"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:fillAfter="false" />
</set>