The custom style of the Image Browsing gallery control is added with a gray border:
1. The main. xml file is as follows:
[Java]
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "www.2cto.com"
Android: id = "@ + id/widget0"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Gallery
Android: id = "@ + id/Gallery"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_centerInParent = "true"
/>
</RelativeLayout>
2. The attrs. xml file is as follows:
[Java]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Declare-styleable name = "Gallery1">
<Attr name = "android: galleryItemBackground"/>
</Declare-styleable>
</Resources>
3. the Activity code is as follows:
[Java]
Public class gallery extends Activity {
Private int size;
Private List <Drawable> list = new ArrayList <Drawable> ();
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Full screen
RequestWindowFeature (Window. FEATURE_NO_TITLE );
GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN, WindowManager. LayoutParams. FLAG_FULLSCREEN );
SetContentView (R. layout. main );
Final Object data = getLastNonConfigurationInstance ();
If (data = null ){
GetImage ();
} Else {
List = (List <Drawable>) data;
}
Final Gallery g = (Gallery) findViewById (R. id. Gallery );
G. setAdapter (new ImageAdapter (this, list ));
// A Gallery image is displayed by default.
G. setSelection (size/2 );
}
@ Override
Public Object onRetainNonConfigurationInstance (){
Return list;
}
// Obtain the system application icon
Private List <Drawable> getImage (){
PackageManager packageManager = this. getPackageManager ();
Intent intent = new Intent (Intent. ACTION_MAIN );
Intent. addCategory (Intent. CATEGORY_LAUNCHER );
List <ResolveInfo> infos = packageManager. queryIntentActivities (intent, 0 );
For (ResolveInfo info: infos ){
ActivityInfo ai = info. activityInfo;
Drawable icon = ai. loadIcon (packageManager );
List. add (icon );
}
Return list;
}
}
4. the adapter code is as follows:
[Java]
Public class ImageAdapter extends BaseAdapter {
Int mGalleryItemBackGround;
Private Context mContext;
Private List <Drawable> list;
Public ImageAdapter (Context mContext, List <Drawable> li ){
This. mContext = mContext;
This. list = li;
TypedArray a = mContext. obtainStyledAttributes (R. styleable. Gallery1 );
MGalleryItemBackGround = a. getResourceId (R. styleable. Gallery1_android_galleryItemBackground, 0 );
A. recycle ();
}
Public int getCount (){
Return list. size ();
}
Public Object getItem (int position ){
Return position;
}
Public long getItemId (int position ){
Return position;
}
Public View getView (int position, View convertView, ViewGroup parent ){
// Declare an ImageView object
ImageView I = new ImageView (mContext );
BitmapFactory. Options options = new BitmapFactory. Options ();
// Set the image size. The width and height are 1/2 of the original size, that is, the image size is 1/4 of the original size.
Options. inSampleSize = 2;
I. setImageDrawable (list. get (position ));
// Set the width and height of layout
I. setLayoutParams (new Gallery. LayoutParams (200,200); // layout
// Reset the image width and height
I. setScaleType (ImageView. ScaleType. FIT_XY); // set scale type
// Set the background of the image, that is, the custom background style.
I. setBackgroundResource (mGalleryItemBackGround );
Return I;
}
Class ViewHolder {
ImageView image;
}
}
Author: huweilong1030