Android rotation control and android rotation control

Source: Internet
Author: User

Android rotation control and android rotation control

First, control rotation

 

1. Create a Master Layout

1. The control used is TextSwitcher (Text rotation)

Then the other corresponding ones are ImageSwitcher (image rotation)

1 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical" 6 android: paddingBottom = "@ dimen/activity_vertical_margin" 7 android: paddingLeft = "@ dimen/activity_horizontal_margin" 8 android: paddingRight = "@ dimen/activity_horizontal_margin" 9 android: paddingTop = "@ dimen/activity_vertical_margin" 10 tools: context = "com. yuxuan. lunhuan. activity. mainActivity "> 11 12 <TextSwitcher13 android: id =" @ + id/textSwitcher1 "14 android: layout_width =" match_parent "15 android: layout_height =" wrap_content "16 android: background = "# 44366ff"> 17 18 </TextSwitcher> 19 20 <ImageSwitcher21 android: id = "@ + id/imageSwitcher1" 22 android: layout_width = "match_parent" 23 android: layout_height = "wrap_content" 24 android: background = "#44ff0000"> 25 26 </ImageSwitcher> 27 28 </LinearLayout>Activity_main.xml

Ii. Main Code

1. Declare the control TextSwitcher.

private TextSwitcher textSwitcher1;

 

2. Define a String array for Data

private String[] strs = new String[] { "1", "2", "3" };

 

3. Create an int type variable to record the subscript

private int index = 0;

 

4. Create two int-type values to save the following X values for pressing and releasing the touch event mobile phone.

private int startx;private int endx;

 

5. Start to write code in the initialization event!

TextSwitcher1 = (TextSwitcher) findViewById (R. id. textSwitcher1); // create a factory anonymous internal class textSwitcher1.setFactory (new ViewFactory () {@ Override // use this method to create TextView public View makeView () {return new TextView (MainActivity. this) ;}}); // defines an animation (Optional). TranslateAnimation Animation = new TranslateAnimation (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]); trim (new OnTouchListener () {@ Override // set the touch event public boolean onTouch (View v, motionEvent) {// press if (event. getAction () = MotionEvent. ACTION_DOWN) {startx = event. getX ();} // release if (event. getAction () = MotionEvent. ACTION_UP) {endx = event. getX (); // if (startx-endx> 100) is executed only after sliding for a certain distance {// determine the subscript if (index = strs. length) {index = 0 ;}// set the text subscript plus 1 textSwitcher1.setText (strs [index]); index = index + 1 ;}return true ;}});

 

Then, the image rotation is actually the same as the Code directly.

 

First, add an ImageSwitcher control to the main layout file.

 

Then enter the code

 

1. Declare the ImageSwitcher control.

private ImageSwitcher imageSwitcher1;

 

2. Define an int array to save the ID of the image.

private int[] imgs = new int[] { android.R.drawable.alert_dark_frame,            android.R.drawable.arrow_down_float,            android.R.drawable.btn_dropdown, };

 

3. Create an int type variable to record the subscript (same as above)

4. Create two int-type values to save the following touch event mobile phone press and release X values (same as above)

 

5. Start to write code in the initialization event!

ImageSwitcher1 = (ImageSwitcher) findViewById (R. id. imageSwitcher1); imageSwitcher1.setFactory (new ViewFactory () {@ Override // create an ImageView. here we need to process the background public View makeView () {ImageView lv = new ImageView (MainActivity. this); lv. setBackgroundColor (Color. RED); FrameLayout. layoutParams params = new FrameLayout. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); params. gravity = Gravity. CENTER_VERTICAL; lv. setLayoutParams (params); return lv ;}}); TranslateAnimation inanimation = new TranslateAnimation (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); updated (imgs [index]); updated (new OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent) {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 ;}});

 

Last write rotation between 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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = "com. yuxuan. lunhuanym. mainActivity "> // here is used is the control details inside the android-support-v4 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

 

Directly attach the main code below

Package com. yuxuan. lunhuanym; import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import android. support. v4.view. pagerAdapter; import android. support. v4.view. viewPager; import android. view. view; import android. view. viewGroup; public class MainActivity extends Activity {private ViewPager pager; private int [] pagids = new int [] {R. layout. activity_view1, R. layout. activity_view2, R. layout. activity_view3,}; private PagerAdapter adapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); initView ();} private void initView () {setContentView (R. layout. activity_main); pager = (ViewPager) findViewById (R. id. vp); adapter = new MyPageAdapter (); pager. setAdapter (adapter);} // custom adapter private class MyPageAdapter extends PagerAdapter {private List <View> vs; public MyPageAdapter () {vs = new ArrayList <View> (); for (int I = 0; I <pagids. length; I ++) {View view = View. inflate (MainActivity. this, pagids [I], null);. add (view) ;}@ Override // total number of public int getCount () {return pagids on the page to be rotated. length ;}@ Override public boolean isViewFromObject (View view, Object object) {return view = object ;} @ Override // Initialize an entry // container viewpager itself // position the attempt to public Object instantiateItem (ViewGroup container, int position) {container. addView (. get (position); return. get (position) ;}@ Override // destroy an entry // iner container itself // position destroy subscript // page public void destroyItem destroyed by the object (ViewGroup container, int position, object object) {container. removeView (View) object );}}}

 

A little awesome! The controls are related to the adapter ~

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.