Android-Gallery instance

Source: Internet
Author: User

1) Create Project Gallery04
2) Import images
3) write an ImageAdapter
4) Compile GalleryFlow
5) Compile layout_gallery.xml
6) Compile Gallery04Activity

 

 

Import android. content. Context;
Import android. content. res. Resources;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. Canvas;
Import android. graphics. LinearGradient;
Import android. graphics. Matrix;
Import android. graphics. Paint;
Import android. graphics. porterduduxfermode;
Import android. graphics. Bitmap. Config;
Import android. graphics. PorterDuff. Mode;
Import android. graphics. Shader. TileMode;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. ImageView;
Import android. widget. ImageView. ScaleType;


Public class ImageAdapter extends BaseAdapter {


Int mGalleryItemBackground;
Private Context mContext;
Private Integer [] mImageIds;
Private ImageView [] mImages;


Public ImageAdapter (Context c, Integer [] ImageIds ){
MContext = c;
MImageIds = ImageIds;
MImages = new ImageView [mImageIds. length];
}


Public boolean createReflectedImages (){
Final int reflectionGap = 4;
Int index = 0;


For (int imageId: mImageIds ){
Bitmap originalImage = BitmapFactory. decodeResource (mContext
. GetResources (), imageId );
Int width = originalImage. getWidth ();
Int height = originalImage. getHeight ();


Matrix matrix = new Matrix ();
Matrix. preScale (1,-1 );


Bitmap reflectionImage = Bitmap. createBitmap (originalImage, 0,
Height/2, width, height/2, matrix, false );


Bitmap bitmapWithReflection = Bitmap. createBitmap (width,
(Height + height/2), Config. ARGB_8888 );


Canvas canvas = new Canvas (bitmapWithReflection );


Canvas. drawBitmap (originalImage, 0, 0, null );


Paint deafaultPaint = new Paint ();
Canvas. drawRect (0, height, width, height + reflectionGap,
DeafaultPaint );


Canvas. drawBitmap (reflectionImage, 0, height + reflectionGap, null );


Paint paint = new Paint ();
LinearGradient shader = new LinearGradient (0, originalImage
. GetHeight (), 0, bitmapWithReflection. getHeight ()
+ ReflectionGap, 0x70ffffff, 0x00ffffff, TileMode. CLAMP );


Paint. setShader (shader );


Paint. setXfermode (new porterduxfermode (Mode. DST_IN ));


Canvas. drawRect (0, height, width, bitmapWithReflection. getHeight ()
+ ReflectionGap, paint );


ImageView imageView = new ImageView (mContext );
ImageView. setImageBitmap (bitmapWithReflection );
ImageView. setLayoutParams (new GalleryFlow. LayoutParams (180,240 ));
// ImageView. setScaleType (ScaleType. MATRIX );
MImages [index ++] = imageView;
}
Return true;
}


Private Resources getResources (){
// TODO Auto-generated method stub
Return null;
}


Public int getCount (){
Return mImageIds. length;
}


Public Object getItem (int position ){
Return position;
}


Public long getItemId (int position ){
Return position;
}


Public View getView (int position, View convertView, ViewGroup parent ){
Return mImages [position];
}


Public float getScale (boolean focused, int offset ){
Return Math. max (0, 1.0f/(float) Math. pow (2, Math. abs (offset )));
}


}


Package com. kingsoft. gallery04;


Import android. content. Context;
Import android. graphics. Camera;
Import android. util. AttributeSet;
Import android. view. View;
Import android. widget. Gallery;


Public class GalleryFlow extends Gallery {

Private Camera mCamera = new Camera ();
Private int mMaxRotationAngle = 60;
Private int mMaxZoom =-120;
Private int mCoveflowCenter;


Public GalleryFlow (Context context ){
Super (context );
// TODO Auto-generated constructor stub
This. setStaticTransformationsEnabled (true );
}

Public GalleryFlow (Context context, AttributeSet attrs ){
Super (context, attrs );
// TODO Auto-generated constructor stub
This. setStaticTransformationsEnabled (true );
}

Public GalleryFlow (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
// TODO Auto-generated constructor stub
This. setStaticTransformationsEnabled (true );
}

Public int getMaxRotationAngle (){
Return mMaxRotationAngle;
}

Public void setMaxRotationAngle (int maxRotationAngle ){
MMaxRotationAngle = maxRotationAngle;
}

Public int getMaxZoom (){
Return mMaxZoom;
}

Public void setMaxZoom (int maxZoom ){
MMaxZoom = maxZoom;
}

Private int getCenterOfCoverflow (){
Return (getWidth ()-getPaddingLeft ()-getPaddingRight ()/2
+ GetPaddingLeft ();
}


Private static int getCenterOfView (View view ){
Return view. getLeft () + view. getWidth ()/2;
}

Protected void onSizeChanged (int w, int h, int oldw, int oldh ){
MCoveflowCenter = getCenterOfCoverflow ();
Super. onSizeChanged (w, h, oldw, oldh );
}

}

Www.2cto.com
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "# ffffff"
>


<Com. kingsoft. gallery04.GalleryFlow
Android: id = "@ + id/Gallery04"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_centerInParent = "true"/>
</RelativeLayout>


Package com. kingsoft. gallery04;


Import android. app. Activity;
Import android. OS. Bundle;


Public class Gallery04Activity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. layout_gallery );

Integer [] images = {R. drawable. img0001, R. drawable. img0030,
R. drawable. im0000100, R. drawable. img0130, R. drawable. im0000200,
R. drawable. img0230, R. drawable. im0000300, R. drawable. img0330,
R. drawable. im1_354 };

ImageAdapter adapter = new ImageAdapter (this, images );
// Adapter. createReflectionImages ();
Adapter. createReflectedImages ();
GalleryFlow galleryFlow = (GalleryFlow) findViewById (R. id. Gallery04 );
GalleryFlow. setAdapter (adapter );


}
}

Run.

 

Modify the following to add the animation effect:

Anim. xml


<? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android">
<Scale
Android: interpolator = "@ android: anim/accelerate_decelerate_interpolator"
Android: fromXScale = "0.5"
Android: toXScale = "1"
Android: fromYScale = "0.5"
Android: toYScale = "1"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: fillBefore = "true"
Android: fillAfter = "true"
Android: duration= "1000"
>

</Scale>
</Set>
Modify Gallery04Activity


Package com. kingsoft. gallery04;


Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. MotionEvent;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. View. OnFocusChangeListener;
Import android. view. View. OnTouchListener;
Import android. view. animation. Animation;
Import android. view. animation. AnimationSet;
Import android. view. animation. AnimationUtils;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemClickListener;
Import android. widget. AdapterView. OnItemLongClickListener;
Import android. widget. AdapterView. OnItemSelectedListener;


Public class Gallery04Activity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. layout_gallery );

Integer [] images = {R. drawable. img0001, R. drawable. img0030,
R. drawable. im0000100, R. drawable. img0130, R. drawable. im0000200,
R. drawable. img0230, R. drawable. im0000300, R. drawable. img0330,
R. drawable. im1_354 };

Final Animation anim = AnimationUtils. loadAnimation (getApplicationContext (), R. layout. anim );
Final AnimationSet animationSet = new AnimationSet (true );

ImageAdapter adapter = new ImageAdapter (this, images );
// Adapter. createReflectionImages ();
Adapter. createReflectedImages ();
GalleryFlow galleryFlow = (GalleryFlow) findViewById (R. id. Gallery04 );
GalleryFlow. setAdapter (adapter );

/*
* Animation triggered in the middle of the screen
*/
// GalleryFlow. setOnItemSelectedListener (new OnItemSelectedListener (){
//
// @ Override
// Public void onItemSelected (AdapterView <?> Arg0, View arg1,
// Int arg2, long arg3 ){
/// TODO Auto-generated method stub
// Gallery04Activity. this. setTitle (String. valueOf (arg2 ));
// Arg1.startAnimation (anim );
//
//
//}
//
// @ Override
// Public void onNothingSelected (AdapterView <?> Arg0 ){
/// TODO Auto-generated method stub
//
//}
//});


/**
* Click the option to trigger the animation.
*/
// GalleryFlow. setOnItemClickListener (new OnItemClickListener (){
//
// @ Override
// Public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2,
// Long arg3 ){
/// TODO Auto-generated method stub
// Gallery04Activity. this. setTitle (String. valueOf (arg3 ));
// Arg1.startAnimation (anim );
//
//}
//});



/**
* Animation effects cannot be triggered.
*/
// GalleryFlow. setOnFocusChangeListener (new OnFocusChangeListener (){
//
// @ Override
// Public void onFocusChange (View v, boolean hasFocus ){
/// TODO Auto-generated method stub
// Gallery04Activity. this. setTitle (String. valueOf (v. getId ()));
// V. startAnimation (anim );
//
//}
//});


/**
* Not applicable. It applies to the entire gallery instead of a single Item.
*/
// GalleryFlow. setOnTouchListener (new OnTouchListener (){
//
// @ Override
// Public boolean onTouch (View v, MotionEvent event ){
/// TODO Auto-generated method stub
// Gallery04Activity. this. setTitle (String. valueOf (v. getId ()));
// V. startAnimation (anim );
// Return true;
//}
//});


GalleryFlow. setOnItemClickListener (new OnItemClickListener (){


@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2,
Long arg3 ){
// TODO Auto-generated method stub
Arg0.setOnItemSelectedListener (new OnItemSelectedListener (){


@ Override
Public void onItemSelected (AdapterView <?> Arg0, View arg1,
Int arg2, long arg3 ){
// TODO Auto-generated method stub
Gallery04Activity. this. setTitle (String. valueOf (arg2 ));
Arg1.startAnimation (anim );
}


@ Override
Public void onNothingSelected (AdapterView <?> Arg0 ){
// TODO Auto-generated method stub

}
});
}

 

});


/**
* No obvious effect
*/
// GalleryFlow. setOnItemLongClickListener (new OnItemLongClickListener (){
//
// @ Override
// Public boolean onItemLongClick (AdapterView <?> Arg0, View arg1,
// Int arg2, long arg3 ){
/// TODO Auto-generated method stub
// Return false;
//}
//});
}
}

From the column of BELLSONG

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.