Android ViewPager sliding background gradient, androidviewpager

Source: Internet
Author: User

Android ViewPager sliding background gradient, androidviewpager
Principle

The total layout is RelativeLayout or FrameLayout. Here we use RelativeLayout. First, set the background image. The width and height are both fill_parent. When setting viewpager, the background of viewpager should be transparent ~ In this way, the background image can be displayed on the page. Monitor the sliding of viewpager, fade out the current background by the sliding 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>

There is a custom View, fade out, and other operations are performed in the 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 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 ("nth" + position + "");

(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 drawable of the next background image

}

 

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 am the dividing line of tiantiao

 

 

 

 

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

Related Article

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.