Android Rotation controls

Source: Internet
Author: User

The first is the control rotation

I. Creating a Master Layout

1. The control used is textswitcher (text rotation)

So the other corresponding is imageswitcher (picture rotation)

1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:orientation= "Vertical"6android:paddingbottom= "@dimen/activity_vertical_margin"7android:paddingleft= "@dimen/activity_horizontal_margin"8android:paddingright= "@dimen/activity_horizontal_margin"9android:paddingtop= "@dimen/activity_vertical_margin"Tentools:context= "Com.yuxuan.lunhuan.activity.MainActivity" > One  A<Textswitcher -Android:id= "@+id/textswitcher1" -Android:layout_width= "Match_parent" theandroid:layout_height= "Wrap_content" -android:background= "#440000ff" > -  -</TextSwitcher> +  -<Imageswitcher +Android:id= "@+id/imageswitcher1" AAndroid:layout_width= "Match_parent" atandroid:layout_height= "Wrap_content" -android:background= "#44ff0000" > -  -</ImageSwitcher> -  -</LinearLayout>
Activity_main.xml

Two. Main code

1. Declare the control Textswitcher

Private Textswitcher TextSwitcher1;

2. Define an array of type string to do the data

Private New String[] {"1", "2", "3"};

3. Create a variable of type int to record the subscript

Private int index = 0;

4. Create a value of two int to hold the X value of the touch event phone pressed and released below

Private int StartX; Private int EndX;

5. Start writing code in the initialization event!

TextSwitcher1 =(Textswitcher) Findviewbyid (r.id.textswitcher1); //Create factory anonymous inner classTextswitcher1.setfactory (Newviewfactory () {@Override//Use this method to create a TextView             PublicView Makeview () {return NewTextView (mainactivity. This);                }        }); //Define an animation (optional)translateanimation animation =Newtranslateanimation (animation.relative_to_parent,1.0f, Animation.relative_to_parent,0.0f, Animation.relative_to_self,0.0f, Animation.relative_to_self, 0.0f); Animation.setduration (1500); Animation.setfillafter (true);        Textswitcher1.setinanimation (animation); Textswitcher1.settext (strs[0]); Textswitcher1.setontouchlistener (NewOntouchlistener () {@Override//Set Touch Events             Public BooleanOnTouch (View V, motionevent event) {//Press the                if(event.getaction () = =Motionevent.action_down) {StartX=Event.getx (); }                                //Loosen                if(event.getaction () = =motionevent.action_up) {EndX=Event.getx (); //slide a certain distance to execute                    if(Startx-endx > 100) {                                                //judging the subscript                        if(Index = =strs.length) {index= 0; }                                                //set text subscript plus 1Textswitcher1.settext (Strs[index]); Index= index + 1; }                }                return true; }        });

And then the picture rotation is actually roughly the same directly on the code

First, add a Imageswitcher control to the main layout file

Then go into code writing

1. Declare the control Imageswitcher

Private Imageswitcher ImageSwitcher1;

2. Define an array of type int to hold the ID of the desired picture

Private int New int [] {Android. R.drawable.alert_dark_frame,            Android. R.drawable.arrow_down_float,            Android. R.drawable.btn_dropdown,};

3. Create a variable of type int to record subscript (IBID.)

4. Create a value of two int to hold the X value of the touch event phone pressed and released below (IBID.)

5. Start writing code in the initialization event!

ImageSwitcher1 =(Imageswitcher) Findviewbyid (r.id.imageswitcher1); Imageswitcher1.setfactory (Newviewfactory () {@Override//Create ImageView Here we need to deal with the background .             PublicView Makeview () {ImageView LV=NewImageView (mainactivity. This);                Lv.setbackgroundcolor (color.red); Framelayout.layoutparams params=Newframelayout.layoutparams (layoutparams.wrap_content, layoutparams.wrap_content); Params.gravity=gravity.center_vertical;                Lv.setlayoutparams (params); returnLV;        }        }); Translateanimation inanimation=Newtranslateanimation (animation.relative_to_parent,1.0f, Animation.relative_to_parent,0.0f, Animation.relative_to_self,0.0f, Animation.relative_to_self, 0.0f); Inanimation.setduration (1500); Inanimation.setfillafter (true);        Imageswitcher1.setinanimation (inanimation);                Imageswitcher1.setimageresource (Imgs[index]); Imageswitcher1.setontouchlistener (NewOntouchlistener () {@Override Public BooleanOnTouch (View V, motionevent event) {if(event.getaction () = =Motionevent.action_down) {StartX=Event.getx (); }                if(event.getaction () = =motionevent.action_up) {EndX=Event.getx (); if(Startx-endx > 100) {                        if(Index = =strs.length) {index= 0;                        } imageswitcher1.setimageresource (Imgs[index]); Index= index + 1; }                }                return true; }        });

Finally write a rotation between the pages

First put the main layout code

<linearlayout 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"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.yuxuan.lunhuanym.MainActivity" >//here is the use of android-support-v4 inside the control details can be Baidu<Android.support.v4.view.ViewPager Android:id= "@+id/vp"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > </android.support.v4.view.ViewPager></LinearLayout>
Activity_main.xml

The following direct attached main code

 Packagecom.yuxuan.lunhuanym;Importjava.util.ArrayList;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.support.v4.view.PagerAdapter;ImportAndroid.support.v4.view.ViewPager;ImportAndroid.view.View;ImportAndroid.view.ViewGroup; Public classMainactivityextendsActivity {PrivateViewpager Pager; Private int[] Pagids =New int[] {r.layout.activity_view1, r.layout.activity_view2, R.LAYOUT.ACTIVITY_VIEW3,}; PrivatePageradapter Adapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Initview (); }    Private voidInitview () {Setcontentview (r.layout.activity_main); Pager=(Viewpager) Findviewbyid (R.ID.VP); Adapter=NewMyPageAdapter ();    Pager.setadapter (adapter); }    //Custom Adapters    Private classMyPageAdapterextendsPageradapter {PrivateList<view>vs;  PublicMyPageAdapter () {vs=NewArraylist<view>();  for(inti = 0; i < pagids.length; i++) {View View= View.inflate (mainactivity. This, Pagids[i],NULL);            Vs.add (view); }} @Override//How many pages do you want to put on the wheel?         Public intGetCount () {returnpagids.length; } @Override Public Booleanisviewfromobject (View view, Object object) {returnView = =object; } @Override//Initialize an entry//Container Viewpager itself//position immediately out of the attempt to         PublicObject Instantiateitem (ViewGroup container,intposition)            {Container.addview (Vs.get (position)); returnVs.get (position); } @Override//Destroy an entry//container container itself//position destruction of subscript//object destroyed by page         Public voidDestroyitem (ViewGroup container,intposition, Object object)        {Container.removeview (View) object); }    }}

The controls that feel a little bit more are related to the adapter there's Wood there ~

Android Rotation controls

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.