This article illustrates the method of adding image and reflection and 3D effect to the gallery dynamically by Android. Share to everyone for your reference, specific as follows:
Gallery in Android can provide a good way to display pictures, achieve the above effect and dynamically add a database or download the image resources on the network. We first implement a custom gallery class.
Mygallery.java:
Package nate.android.Service;
Import Android.content.Context;
Import Android.graphics.Camera;
Import Android.graphics.Matrix;
Import Android.graphics.Rect;
Import Android.util.AttributeSet;
Import Android.view.View;
Import android.view.animation.Transformation;
Import Android.widget.Gallery;
Import Android.widget.ImageView;
Import Android.widget.Toast; Publicclass Mygallery extends Gallery {private Camera Mcamera =new Camera (); Privateint Mmaxrotationangle =45
Mmaxzoom =-120;
Privateint Mcoveflowcenter;
Public Mygallery {Super (context); this.setstatictransformationsenabled (true); Public Mygallery (context, AttributeSet attrs) {Super (context, attrs); This.setstatictransformationsenabled (
true); Public Mygallery (context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); This.setstatictra
Nsformationsenabled (TRUE);
} publicint Getmaxrotationangle () {return mmaxrotationangle; } publicvoid setmaxrotationangle (int maxrOtationangle) {mmaxrotationangle = Maxrotationangle;
} publicint Getmaxzoom () {return mmaxzoom;
} publicvoid setmaxzoom (int maxzoom) {mmaxzoom = Maxzoom;
} privateint Getcenterofcoverflow () {return (GetWidth ()-Getpaddingleft ()-getpaddingright ())/2 + getpaddingleft ();
} privatestaticint Getcenterofview (view view) {return view.getleft () + view.getwidth ()/2; } Protectedboolean getchildstatictransformation (View child, Transformation t) {Finalint childcenter = GetCenterOfView (
Child);
Finalint childwidth = Child.getwidth ();
int rotationangle = 0;
T.clear ();
T.settransformationtype (Transformation.type_matrix);
if (Childcenter = = Mcoveflowcenter) {transformimagebitmap (ImageView) child, T, 0); else {rotationangle = (int) (((float) (mcoveflowcenter-childcenter)/childwidth) * mmaxrotationangle); if (Math
. ABS (RotationAngle) > Mmaxrotationangle) {rotationangle = (RotationAngle <0)?-mmaxrotationangle
: Mmaxrotationangle; } transformimagebitmap ((ImageView) child, T, RotationAngle);
} returntrue; Protectedvoid onsizechanged (int w, int h, int oldw, int oldh) {mcoveflowcenter = Getcenterofcoverflow (); super.onsiz
Echanged (W, H, OLDW, OLDH); } privatevoid Transformimagebitmap (ImageView child, Transformation T, int rotationangle) {mcamera.save (); final Matrix
Imagematrix = T.getmatrix ();
Finalint imageheight = Child.getlayoutparams (). Height;
Finalint imagewidth = Child.getlayoutparams (). width;
Finalint rotation = Math.Abs (rotationangle);
The perspective of the forward moving camera on the z-axis, the actual effect is to enlarge the picture.
If you move on the y-axis, the picture moves up and down, and the corresponding picture moves around the x-axis.
Mcamera.translate (0.0f, 0.0f, 100.0f); As the angle of the view gets less, zoom in if (Rotation < Mmaxrotationangle) {float zoomamount = (float) (mmaxzoom
+ (rotation *1.5));
Mcamera.translate (0.0f, 0.0f, Zoomamount);
//rotate on the y-axis and flip the picture vertically inward.
If you rotate on the x-axis, the corresponding picture is flipped sideways.
Mcamera.rotatey (RotationAngle);
Mcamera.getmatrix (Imagematrix); Imagematrix.pretranslate (-(ImagewidTH/2),-(IMAGEHEIGHT/2));
Imagematrix.posttranslate ((IMAGEWIDTH/2), (IMAGEHEIGHT/2));
Mcamera.restore ();
}
}
In the layout file
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout 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" > <linearlayout android:layout_width= "fill_parent android:layout_height=" Wrap_ Content "android:orientation=" vertical "android:paddingtop=" 10px "> <linearlayout android:layout_width=" Fill_ Parent "android:layout_height=" wrap_content "android:orientation=" horizontal "> <textview android:layout_
Width= "Wrap_content" android:layout_height= "wrap_content" android:id= "@+id/dishname" android:textSize= "18pt"
android:text= "vegetable name"/> <linearlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:orientation= "Horizontal" android:paddingleft= "10px" > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:id= "@+id/ds" android:textsize= "18pt" android:text= "rating:"/> <ratingbar android:numstars= "5" android:rating= "3" android:stepsize= "0.2" android:layout_ Width= "Wrap_content" android:layout_height= "Wrap_content" android:isindicator= "true" android:id= "@+id/dishScores"
style= "Android:attr/ratingbarstylesmall"/> </LinearLayout> </LinearLayout> <linearlayout Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > < TextView android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:id= "@+id/dishPrice" android:text= "Price" android:textsize= "18pt"/> </LinearLayout> </LinearLayout> < Nate.android.Service.MyGallery android:id= "@+id/gallery01" android:layout_width= "Fill_parent" Android:layout_ height= "Wrap_content" android:layout_centerinparent= "true"/> <textview android:text= "\n\n\n\n" Here is information about each dish, Click on the picture to enter the comment "android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:layout_below=" @+id/Gallery01 "android:paddingleft=" 5px "android:id=" @+id/showhint "/> </LinearLayout>
In the XML file above, we used the custom mygallery.
Then the top one Imageadapter class inherits from Baseadapter.
Package nate.android.Service;
Import java.util.ArrayList;
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.PorterDuffXfermode;
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; Publicclass Imageadapter extends Baseadapter {int mgalleryitembackground; private context mcontext; private arraylist<
;byte[]> dishimages =new arraylist<byte[]> ();
Private imageview[] mimages;
Public Imageadapter (Context c,arraylist<byte[]> tmpdishimages) {mcontext = C;
Dishimages = tmpdishimages;
Mimages =new imageview[dishimages.size ()]; } publiCboolean createreflectedimages () {finalint reflectiongap =4; int index = 0;
System.out.println ("dishimages size" + dishimages.size ());
for (int i =0 i < dishimages.size (); ++i) {System.out.println ("dishimage---" + dishimages.get (i));
Bitmap originalimage = Bitmapfactory.decodebytearray (Dishimages.get (i), 0, Dishimages.get (i). length);
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 () + Reflec
Tiongap, 0x70ffffff, 0X00FFFFFF, Tilemode.clamp);
Paint.setshader (shader);
Paint.setxfermode (New Porterduffxfermode (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.setlayoutparams (New Mygallery.layoutparams (270, 360));
Imageview.setscaletype (Scaletype.matrix);
mimages[index++] = ImageView;
} returntrue;
Private Resources getresources () {returnnull;
} publicint GetCount () {return dishimages.size ();
Public Object getitem (int position) {return position;
} publiclong getitemid (int position) {return position; public view GetView (int position, view Convertview, ViewgroUp parent) {return mimages[position];
Publicfloat Getscale (boolean focused, int offset) {return Math.max (0, 1.0f/(float) Math.pow (2, Math.Abs (offset));
}
}
In this class, the constructor needs to pass in the picture data that will be drawn in the gallery, as an example of the byte[type, as I convert it to byte[in SQLite and from a picture download from the Web, and we use
Copy Code code as follows:
Bitmap originalimage = Bitmapfactory.decodebytearray (Dishimages.get (i), 0, Dishimages.get (i). length);
In this article there is a more detailed description: http://www.jb51.net/article/88588.htm
"Restore" the picture data of the byte[] type. Byte[] Type of picture source data is stored in a arraylist<byte[]>. This provides a source of data for the dynamic implementation of adding pictures to the gallery.
Use our custom baseadapter and gallery in the following activity. Achieve the effect shown in the above illustration.
Working with instance classes
Package com.nate.wte2;
Import java.io.IOException;
Import java.util.ArrayList;
Import Nate.InfoService.DishInfo;
Import Nate.InfoService.StoreInfoService;
Import Nate.InfoService.WhichChoice;
Import Nate.NetConnection.GetConnectionSock;
Import Nate.android.Service.GalleryFlow;
Import Nate.android.Service.ImageAdapter;
Import android.app.Activity;
Import Android.app.ProgressDialog;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.RatingBar;
Import Android.widget.TextView;
Import Android.widget.Toast;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.AdapterView.OnItemSelectedListener;
Import Com.nate.wte.LocalSql.StoresInfoDB; Publicclass Dishmenuactivity extends Activity {private arraylist<dishinfo> dishinfolist =new arraylist<
Dishinfo> ();
Private TextView Dishname;
Private Ratingbar dishscores; Private TextView Dishprice;
3:send the dish ' whole info to fill the activity (send the comments of the dish) privateint flag3 = 3;
Whichchoice choice3 =new Whichchoice (FLAG3);
Private Storeinfoservice Storeinfo;
Private ProgressDialog Loadingdialog; /** * Handler Handle dialog dismission/private handler Handler =new () {@Override handler publicvoid
GE (Message msg) {Loadingdialog.dismiss ();//other operation Super.handlemessage (msg);
}
}; /** * thread to load the data from local database or from the server * @author Administrator * * */class Loading Impl
Ements runnable{@Override publicvoid Run () {try {//The sleep here will be replaced by a loop, knowing that a certain condition only ends when the loop is met, so that dialog terminates thread.sleep (1500);
Handler.sendemptymessage (0);
catch (Interruptedexception e) {e.printstacktrace (); }}/** * loading the Items,start the thread to load * * publicvoid Loadingitems () {loadingdialog = ProgressDialog
. Show (Dishmenuactivity.this, "", "Loading ..."); Thread T =new thread(New Loading ());
T.start ();
} publicvoid onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.dishmenu_gallery);
Storesinfodb Infodb;
int dishinfolistlength;
Arraylist<byte[]> dishimages =new arraylist<byte[]> ();
Byte[] Dishimage;
Dishname = (TextView) This.findviewbyid (r.id.dishname);
Dishprice = (TextView) This.findviewbyid (R.id.dishprice);
Dishscores = (Ratingbar) This.findviewbyid (r.id.dishscores);
The object Intent Intent = Getintent () is obtained from the choices class in Intent.
Bundle Bundle = Intent.getbundleextra ("Bundledata");
Storeinfo = (storeinfoservice) bundle.getserializable ("Storeinfo");
Dishinfolist = (arraylist<dishinfo>) bundle.getserializable ("Dishinfolist");
System.out.println ("Look at the info received from choices activity"); for (int i =0 i < dishinfolist.size (); i++) {System.out.println ("---" + i + dishinfolist.get (i). Getdishimage (). tostr
ing ());
} dishinfolistlength = Dishinfolist.size (); Class Dishimages for (int i =0 < dishinfolistlength; i++) {Dishimages.add (Dishinfolist.get (i) getdishimage ());
} System.out.println ("The length of the dishimages----" + dishimages.size ()); Note Here a section of////////////////////////////////Imageadapter adapter =new imageadapter (
Dishmenuactivity.this,dishimages);
Adapter.createreflectedimages ();
Galleryflow Galleryflow = (galleryflow) Findviewbyid (R.ID.GALLERY01); Galleryflow.setonitemselectedlistener (New Onitemselectedlistener () {@Override publicvoid onitemselected ( Adapterview<?> arg0, View arg1, int arg2, long arg3) {String showname = "Menu Name:" + dishinfolist.get ((int) arg3). Get
Dishname () + "";
Dishname.settext (ShowName);
Dishscores.setrating (dishinfolist.get (int) arg3). Getdishscores ());
Dishprice.settext ("Price:" + dishinfolist.get ((int) arg3). GetPrice () + "Yuan \n\n\n");
@Override publicvoid onnothingselected (adapterview<?> arg0) {}}); Galleryflow.setonitemcLicklistener (New Onitemclicklistener () {@Override publicvoid Onitemclick (adapterview<?> arg0, View arg1, int arg2
, long Arg3) {loadingitems ();
Dishinfo Dishinfo = dishinfolist.get ((int) arg3);
try {GetConnectionSock.fromClient.writeObject (CHOICE3);
SYSTEM.OUT.PRINTLN ("Send the Flag 3");
GetConnectionSock.fromClient.writeObject (Dishinfo);
SYSTEM.OUT.PRINTLN ("Send the name back to server");
Dishinfo dishcomments = (dishinfo) GetConnectionSock.fromServer.readObject ();
System.out.println ("recv The dish Comments");
Dishinfo.setdishname (dishinfolist.get (int) arg3). Getdishname ());
Dishinfo.setdishcomments (Dishcomments.getdishcomments ());
System.out.println ("Full dish info");
catch (IOException e) {e.printstacktrace ();
catch (ClassNotFoundException e) {e.printstacktrace ();
} Intent Intent =new Intent ();
Bundle Bundle =new Bundle ();
Bundle.putserializable ("Dishinfo", dishinfo); Bundle.putserializable ("StoreInfo ", storeinfo);
Intent.putextra ("Dishbundledata", bundle);
Intent.setclass (Dishmenuactivity.this,dishinfodynamic.class);
Toast.maketext (Dishmenuactivity.this, "Enter comment on this dish", Toast.length_long). Show ();
DishMenuActivity.this.startActivity (Intent);
}
}); Galleryflow.setadapter (adapter);
Note here}}
In this activity is related to this article, that is, add a picture in the galley function, just pay attention to the above code to mark out some of the code, as to the source of data is not the same, here as long as the data is a arraylist<byte[]> On the line. It is important to take advantage of the above mygallery as well as the imageadapter, of course, by simple understanding, easy to these two classes can be reused in other projects
For more information on Android-related content readers can view the site: "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course", "Android debugging techniques and common problems solution summary", " Android Multimedia How-to Summary (audio, video, audio, etc), summary of Android Basic components usage, Android View tips Summary, Android layout layout tips and a summary of Android controls usage
I hope this article will help you with the Android program.