Android 輪換控制項,android輪換控制項

來源:互聯網
上載者:User

Android 輪換控制項,android輪換控制項

首先是控制項輪換

 

一.建立主布局 

1.用到的控制項是 TextSwitcher (文本輪換)

  那麼其他對應的也就是 ImageSwitcher (圖片輪換)

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="#440000ff" >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

二.主要代碼

1.聲明一下控制項 TextSwitcher

private TextSwitcher textSwitcher1;

 

2.定義一個String類型的數組用來做資料

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

 

3.建立一個int型的變數用來記錄下標

private int index = 0;

 

4.建立兩個int型的值用來儲存下面會用到的觸摸事件手機按下和鬆開的X值

private int startx;private int endx;

 

5.在初始化事件裡開始寫代碼把!

textSwitcher1 = (TextSwitcher) findViewById(R.id.textSwitcher1);                // 建立工廠 匿名內部類        textSwitcher1.setFactory(new ViewFactory() {            @Override                        // 用這個方法建立TextView            public View makeView() {                                return new TextView(MainActivity.this);            }        });                // 定義一個動畫(可有可無)        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]);        textSwitcher1.setOnTouchListener(new OnTouchListener() {            @Override                        // 設定觸摸事件            public boolean onTouch(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;                        }                                                // 設定文本 下標加1                        textSwitcher1.setText(strs[index]);                        index = index + 1;                    }                }                return true;            }        });

 

然後是圖片輪換 其實大致上是一樣的 直接上代碼

 

首先在主布局檔案中添加一個 ImageSwitcher 控制項 

 

然後進入代碼編寫

 

1.聲明一下控制項 ImageSwitcher 

private ImageSwitcher imageSwitcher1;

 

2.定義一個int類型的數組用來儲存所需圖片的ID

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

 

3.建立一個int型的變數用來記錄下標 (同上)

4.建立兩個int型的值用來儲存下面會用到的觸摸事件手機按下和鬆開的X值 (同上)

 

5.在初始化事件裡開始寫代碼把!

imageSwitcher1 = (ImageSwitcher) findViewById(R.id.imageSwitcher1);        imageSwitcher1.setFactory(new ViewFactory() {            @Override                        // 建立 ImageView 這裡我們需要處理一下背景            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);        imageSwitcher1.setImageResource(imgs[index]);                imageSwitcher1.setOnTouchListener(new OnTouchListener() {            @Override            public boolean onTouch(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;            }        });

 

最後寫一寫頁面間的輪換

 

首先放上主布局代碼

<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" >// 這裡用的是用的是 android-support-v4 裡面的控制項 詳情可以百度 <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

 

下面的直接附上主代碼

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);    }    // 自訂配接器    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);                vs.add(view);            }        }        @Override        // 要輪放的頁面總共有多少        public int getCount() {            return pagids.length;        }        @Override        public boolean isViewFromObject(View view, Object object) {            return view == object;        }        @Override        // 初始化一個條目        // container viewpager 本身        // position 馬上出來的試圖        public Object instantiateItem(ViewGroup container, int position) {            container.addView(vs.get(position));            return vs.get(position);        }        @Override        // 銷毀一個條目        // container 容器本身        // position 銷毀的下標        // object 銷毀的page        public void destroyItem(ViewGroup container, int position, Object object) {            container.removeView((View) object);        }    }}

 

感覺稍微牛逼點的控制項都和適配器有關 有木有~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.