2011-10-21 49 views
0

如何在不添加圖像灰色邊框的情況下高亮顯示畫廊選定的項目,而不使用此項即可 。高亮無邊框畫廊

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

,可能我添加一個反映圖像,

+0

你可以添加一個動畫項目選擇 – breceivemail

+0

你能解釋更多,請..有例 – Vervatovskis

回答

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>