Android Viewpager Sliding background gradient

Source: Internet
Author: User

Principle

The overall layout is relativelayout or framelayout, and here we use Relativelayout. First set the background image, width and height are fill_parent, set Viewpager,viewpager background to be transparent yo ~ so background picture can be displayed on the page. Slide monitoring of the Viewpager, fade the current background by sliding the offset, and display the next background.

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"

Tools:context= ". MyActivity ">

<us.yydcdut.viewpagercolorchange.backgroundimage

Android:id= "@+id/img"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"/>

<android.support.v4.view.viewpager

Android:id= "@+id/viewpager"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"

/>

</RelativeLayout>

A custom view, fade out, and so on, is done in a custom view.

Code

public class MyActivity extends Activity {

Private BackgroundImage mimg;

Private Viewpager Mviewpager;

Private list<view> mviewlists;

Private list<drawable> mdrawablelists;

private static final int all = 5;

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (r.layout.activity_my);

Initviews ();

Mimg.setmdrawablelists (mdrawablelists);

Mviewpager.setadapter (New Myadapter ());

Mviewpager.setonpagechangelistener (New Viewpager.onpagechangelistener () {

@Override

public void onpagescrolled (int i, float v, int i2) {

Mimg.setmdegree (v);

Mimg.setmposition (i);

Mimg.invalidate ();//Refresh

}

@Override

public void onpageselected (int i) {

}

@Override

public void onpagescrollstatechanged (int i) {

}

});

}

private void Initviews () {

Mimg = (backgroundimage) Findviewbyid (r.id.img);

Mviewpager = (Viewpager) Findviewbyid (R.id.viewpager);

mviewlists = new arraylist<view> ();

mdrawablelists = new arraylist<drawable> ();

for (int i = 0; i < all; i++) {

View view = Getlayoutinflater (). Inflate (R.LAYOUT.VP, NULL);

Mviewlists.add (view);

if (i% 2 = = 0) {

Mdrawablelists.add (Getresources (). getdrawable (R.DRAWABLE.BG_IMG1));

} else {

Mdrawablelists.add (Getresources (). getdrawable (R.DRAWABLE.BG_IMG2));

}

}

}

Class Myadapter extends Pageradapter {

@Override

public int GetCount () {

return Mviewlists.size ();

}

@Override

public Boolean isviewfromobject (view view, Object o) {

return view = = O;

}

@Override

public void Destroyitem (ViewGroup container, int position, object object) {

((Viewpager) container). Removeview (Mviewlists.get (position));

Super.destroyitem (container, Position, object);

}

@Override

Public Object Instantiateitem (viewgroup container, int position) {

View v = mviewlists.get (position);

TextView TextView = (TextView) V.findviewbyid (r.id.txt);

Textview.settext ("first" + position + "one");

((Viewpager) container). AddView (v);

return v;

}

}

}

public class BackgroundImage extends View {

private int mposition;

private float Mdegree;

Private list<drawable> mdrawablelists;

private int mpreposition = 0;

Private drawable Mnext;

public void Setmdrawablelists (list<drawable> mdrawablelists) {

This.mdrawablelists = mdrawablelists;

Mnext = Mdrawablelists.get (1);//Set the next background picture of the drawable

}

public void setmposition (int mposition) {

This.mposition = mposition;

}

public void Setmdegree (float mdegree) {

This.mdegree = Mdegree;

}

Public BackgroundImage (Context context) {

Super (context);

Setwillnotdraw (FALSE);

}

Public BackgroundImage (context context, AttributeSet Attrs) {

Super (context, attrs);

Setwillnotdraw (FALSE);

}

Public BackgroundImage (context context, AttributeSet attrs, int defstyleattr) {

Super (context, attrs, defstyleattr);

Setwillnotdraw (FALSE);

}

@Override

protected void OnDraw (canvas canvas) {

LOG.I ("111", "OnDraw");

if (null = = Mdrawablelists) {

Return

}

int alpha1 = (int) (255-(Mdegree * 255));

drawable fore = Mdrawablelists.get (mposition);

Fore.setbounds (0, 0, getwidth (), getheight ());

Mnext.setbounds (0, 0, getwidth (), getheight ());

if (mpreposition! = mposition) {//Boundary judgment

if (mposition! = Mdrawablelists.size ()-1) {

Mnext = Mdrawablelists.get (mposition + 1);

} else {

Mnext = Mdrawablelists.get (mposition);

}

}

Fore.setalpha (ALPHA1);//Fade out

Mnext.setalpha (255);

Mnext.draw (canvas);

Fore.draw (canvas);

Mpreposition = mposition;

Super.ondraw (canvas);

}

}

I'm the dividing line of the king of the Land Tiger.

Http://www.jinhusns.com/Products/Download/?type=xcj

Android Viewpager Sliding background gradient

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.