Galleries are available in many app designs, as shown in the following illustration:
The example is when I am OK to write a small project, the specific source address please visit https://github.com/AlexSmille/YingMi.
The gallery similar to the effect of the cover, slide into the middle of the picture will slowly become larger, leaving the view will slowly shrink, while setting up sliding monitoring and click monitoring.
There are many examples on the Internet are implemented through the gallery, and the implementation of the above example through the implementation of the Viewpager, to solve the problem of performance optimization, today we take it out, encapsulation, so that later convenient use. The effect of the final implementation is as follows:
How to use
Add the custom control to the layout
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:layout_width=" match_parent "
android:layout_height=" Match_parent " >
< Add custom controls to the!--layout-->
<com.mahao.alex.customviewdemo.viewpager.coverflowviewpager
Android:id= "@+id/cover"
android:layout_width= "match_parent" android:layout_height= "wrap_content"
/>
</RelativeLayout>
Set in code
The settings in the
Code are divided into the following steps:
Find control
initialization data
set the data that needs to be displayed to the control
set sliding listening
public class Mainactivity extends Appcompatactivity {private Coverflowviewpager mcover;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mcover = (Coverflowviewpager) Findviewbyid (r.id.cover);
Initialize data list<view> List = new arraylist<> ();
for (int i = 0;i<10;i++) {imageview img = new ImageView (this);
Img.setbackgroundcolor (Color.parsecolor ("#" +getrandcolorcode ()));
List.add (IMG);
//Set the displayed data Mcover.setviewlist (list); Sets a sliding listener that listens for index Mcover.setonpageselectlistener (new Onpageselectlistener () {@Override public void Select when sliding to the center of the current page) (int position)
{Toast.maketext (Getapplicationcontext (), position+ "", Toast.length_short). Show ();
}
});
/** * Get random color, easy to distinguish * @return/public static string Getrandcolorcode () {string r,g,b;
Random Random = new Random ();
R = integer.tohexstring (Random.nextint (256)). toUpperCase (); g = integer.tohexstring (random.Nextint (256)). toUpperCase ();
b = integer.tohexstring (Random.nextint (256)). toUpperCase (); R = r.length () ==1?
"0" + r:r; g = G.length () ==1?
"0" + g:g; b = b.length () ==1?
"0" + b:b;
return r+g+b;
}
}
Implementation principle
There are two difficulties in the implementation process:
• How to achieve zooming and narrowing in the process of sliding
• How to display pages that are not displayed in Viewpager
How to realize zooming and shrinking in the process of sliding?
When you set up each Viewpager page, set a fixed padding value for each page, so that each page will display a shrinking state. At the same time Viewpager set Addonpagechangelistener (), sliding monitoring, in the sliding monitor will callback Viewpager sliding state, sliding offset, etc., according to the sliding offset to zoom in and out. and setting the display size of the control based on the padding value
How to display pages that are not displayed in Viewpager
There is an infrequently used attribute Android:clipchildren in XML that restricts the display of child view. Set to False, the display of the child view is not restricted by the parent control.
Code implementation
Writing a layout file for a control
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width= "Match_ Parent "
android:layout_height=" wrap_content "
android:clipchildren=" false ">
< Android.support.v4.view.ViewPager
android:id= "@+id/vp_conver_flow"
android:layout_width= "180DP
" android:layout_height= "260DP"
android:layout_centerhorizontal= "true"
android:clipchildren= "false"/>
</RelativeLayout>
A viewpager is embedded in a relative layout, and the relative layout is used to determine the scope of the display, viewpager to achieve sliding, zooming, etc.
Create Coverflowviewpager, load layout
/**
* *
implements cover view
* Created by Alex_mahao on 2016/8/25.
* * Public
class Coverflowviewpager extends Relativelayout implements Onpageselectlistener {
/**
* for left and right scrolling
* *
private viewpager Mviewpager;
Public Coverflowviewpager (context, AttributeSet attrs) {
Super (context, attrs);
Inflate (context, r.layout.widget_cover_flow,this);
Mviewpager = (Viewpager) Findviewbyid (r.id.vp_conver_flow);
Init ();
}
Find the control and load the layout.
Writing adapters to enable sliding monitoring
Now that you have a viewpager, you must have an adapter adapter. Because we want to operate each of the child elements according to the offset, zoom in or out, and for the child element, of course the adapter is the easiest to get, so the adapter implements the Viewpager sliding listener interface.
/** * Scrolling Adapter * Created by Alex_mahao on 2016/8/25.
* * public class Coverflowadapter extends Pageradapter implements Viewpager.onpagechangelistener {/** * default reduced padding value
* * public static int swidthpadding;
public static int sheightpadding;
/** * The set of child elements * * Private list<view> mviewlist;
/** * Sliding Monitor Callback interface * * Private Onpageselectlistener listener;
/** * Contextual object/private context Mcontext;
Public Coverflowadapter (list<view> mimageviewlist) {this.mviewlist = mimageviewlist;
Mcontext = context;
Set padding Value swidthpadding = dp2px (24);
Sheightpadding = dp2px (32); @Override public void Destroyitem (ViewGroup container, int position, object object) {Container.removeview (mviewlist
. get (position));
@Override public Object Instantiateitem (viewgroup container, int position) {View view = Mviewlist.get (position);
Container.addview (view);
return view; @Override public int GetCount () {return mviewlist = = null? 0:mviewList.size ();
@Override public boolean isviewfromobject (view view, Object object) {return view = = object; @Override public void onpagescrolled (int position, float positionoffset, int positionoffsetpixels) {//This method callback Viewpag ER sliding offset if (mviewlist.size () > 0 && Position < Mviewlist.size ()) {//Current finger touch sliding page, slide from 0 page to 1 page offset getting bigger, PA
Dding more and more large log.i ("info", "reset padding");
int outheightpadding = (int) (Positionoffset * sheightpadding);
int outwidthpadding = (int) (Positionoffset * swidthpadding); Slide from 0 to one o'clock, at which point position = 0, which should be shrunk, in line with mviewlist.get (position). setpadding (outwidthpadding, outheightpadding,
Outwidthpadding, outheightpadding); Position+1 for upcoming pages, more and more large if (Position < Mviewlist.size ()-1) {int inwidthpadding = (int) ((1-positionoffset)
* swidthpadding);
int inheightpadding = (int) ((1-positionoffset) * sheightpadding);
Mviewlist.get (position + 1). setpadding (Inwidthpadding, inheightpadding, inwidthpadding, inheightpadding); @Override public void onpageselected (int position) {//callback-selected interface if (listener!= null) {Listener.select (
position);
@Override public void onpagescrollstatechanged (int state) {}/** * When one is the most central callback * @param listener * *
public void Setonpageselectlistener (Onpageselectlistener listener) {This.listener = listener; /** * DP to PX * * @param DP * @return/public int dp2px (int dp) {int px = (int) typedvalue.applydimension (T
Ypedvalue.complex_unit_dip, DP, Mcontext.getresources (). Getdisplaymetrics ());
return px;
}
}
Code is divided into two parts, pageradapter implementation, sliding monitoring implementation. The realization of pageradapter not to say more, the most basic things. Focus on the implementation of sliding monitoring.
There are three callback methods for sliding monitoring: where onpagescrolled (int position, float positionoffset, int positionoffsetpixels) callback is the sliding offset of the viewpager, We again dynamically set the corresponding elements of the padding value, to achieve zoom in and out.
Onpageselected () is a selected callback that is called back to its caller through a custom interface. will be mentioned later.
Initialize Viewpager
Now that you have the adapter, it's natural to start writing the parts of the adapter:
/**
* Initialization method *
/private void init () {
//construct adapter, incoming data source
Madapter = new Coverflowadapter (mviewlist, GetContext ());
Sets the selected callback
Madapter.setonpageselectlistener (this);
Set adapter
mviewpager.setadapter (madapter);
Set the sliding monitor because the Adpter implements the interface of the sliding callback, so here you set the Adpter
Mviewpager.addonpagechangelistener (madapter) directly;
Own Baidu
Mviewpager.setoffscreenpagelimit (5);
Sets the distribution setontouchlistener for the Touch event
(new Ontouchlistener () {
@Override public
boolean Ontouch (View v) Motionevent event) {
//pass to Viewpager for sliding processing return
mviewpager.dispatchtouchevent (event);}}
);
The note is detailed and the only thing to explain is the distribution of the event.
The size of our viewpager is fixed, only in the middle of the display area, then for the fingers on the two side sliding, Viewpager naturally accept the touch event, by setting the outer relative layout of the touch event monitoring, the touch of the event passed to Viewpager, The Viewpager can still achieve the corresponding sliding when implementing the sliding Viewpager area.
Packaging for data sources
Adapter has, Viewpager also have, then only the data source is left.
Because we are based on the set padding value, then for the control that needs to be displayed, his background will not be able to zoom in and out, so the control is wrapping a layer of external controls, so set the external control padding value, the natural need to display the control will zoom out.
/**
* Set the displayed data for a layer of encapsulation
* @param lists
/public void Setviewlist (list<view> lists) {
if lists ==null) {return
;
}
Mviewlist.clear ();
for (View view:lists) {
framelayout layout = new Framelayout (GetContext ());
Set the padding value, and shrink layout.setpadding by default
(Coverflowadapter.swidthpadding,coverflowadapter.sheightpadding, coverflowadapter.swidthpadding,coverflowadapter.sheightpadding);
Layout.addview (view);
Mviewlist.add (layout);
}
Refresh Data
madapter.notifydatasetchanged ();
}
Check for a listener callback
When we slide, different data may be displayed depending on the slide.
After the sliding monitoring is set, the Onpageselected interface callback is implemented for the layer.
Definition of Interface Onpageselectlistener
Public interface Onpageselectlistener {
void Select (int position);
}
Add Callback in Coverflowadapter
@Override public
void onpageselected (int position) {
//callback-selected interface
if (listener!= null) {
Listener.select (position);
}
Add Callback in Coverflowviewpager
The callback shown
@Override public
void Select (int position) {
if (listener!=null) {
Listener.select ( position);
}
Click the event settings
Directly to the data source loop set up monitoring can
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.