您的位置:首页 > 移动开发 > Android开发

android 笔记之------Activity:如何进行屏幕切换

2014-03-28 17:24 519 查看
   开始学Android的都知道,理解Activity可以这样:对于每一个Activity可以看做是一个界面,界面之间的切换也就是Activity之间的切换

1首先定义一个main.xml

                                   

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<tjuci.dl.ui.MyScrollLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible">

<include layout="@layout/page1" />

<include layout="@layout/page2" />
<?xml version="1.0" encoding="utf-8"?>
再来一个page.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/page1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/page1_bg" >

<TextView
android:id="@+id/page1_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/page1_title_bg"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:text="@string/page1_title"
android:textColor="#ffffff"
android:textSize="15dp" />

<TextView
android:id="@+id/page1_title_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/page1_title"
android:layout_centerHorizontal="true"
android:text="@string/page1_title_1"
android:textColor="#000000"
android:textSize="10dp" />

<ImageView
android:id="@+id/page1_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_below="@+id/page1_title_1"
android:layout_centerHorizontal="true"
android:paddingTop="2dp"
android:src="@drawable/page1_image" />

<RelativeLayout
android:id="@+id/page1_login"
android:layout_width="fill_parent"
android:layout_height="110dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/page1_image"
android:gravity="center_horizontal"
android:orientation="vertical" >

<EditText
android:id="@+id/page1_login_user_edit"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingLeft="100dp"
android:paddingRight="80dp"
android:paddingBottom="17dp"
android:saveEnabled="true"
android:singleLine="true"
android:background="@drawable/page1_login_input"
style="@style/page1_text"
android:hint="@string/page1_login_userText_hint" />
<TextView
android:id="@+id/page1_login_user_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingLeft="20dp"
style="@style/page1_text"
android:text="@string/page1_login_userText" />
<TextView
android:layout_width="37dp"
android:layout_height="37dp"
android:paddingTop="16dp"
android:paddingRight="10dp"
android:textStyle="bold"
android:textSize="17sp"
android:layout_alignRight="@+id/page1_login_user_edit"
android:text="@string/page1_login_right_image"
/>
<EditText
android:id="@+id/page1_login_passwd_edit"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingTop="16dp"
android:paddingLeft="180dp"
android:paddingRight="80dp"
android:paddingBottom="15dp"
android:layout_marginBottom="11dp"
android:saveEnabled="true"
android:singleLine="true"
android:background="@drawable/page1_login_input"
style="@style/page1_text"
android:password="true"/>
<TextView
android:id="@+id/page1_login_passwd_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/page1_login_user_text"
android:paddingTop="28dp"
android:paddingLeft="20dp"
style="@style/page1_text"
android:text="@string/page1_login_passwdText" />
</RelativeLayout>
<TableLayout
android:id="@+id/page1_tableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/page1_login"
android:gravity="center_horizontal">
<TableRow >
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:paddingLeft="30dp"
android:layout_marginLeft="20dp"
android:paddingRight="40dp"
style="@style/page1_text"
android:text="@string/page1_network_button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:layout_marginLeft="20dp"
style="@style/page1_text"
android:text="@string/page1_login_button1"/>
</TableRow>
</TableLayout>
<RelativeLayout
android:id="@+id/checkbox_relativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/page1_tableLayout"
>
<CheckBox
android:id="@+id/page1_checbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="50dp"
style="@style/page1_checbox_text"
android:button="@drawable/page1_checbox_selector"
android:text="@string/page1_checbox_text1"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/page1_checbox1"
android:layout_marginLeft="50dp"
style="@style/page1_checbox_text"
android:button="@drawable/page1_checbox_selector"
android:text="@string/page1_checbox_text2"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<TextView
android:id="@+id/page1_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#000000"
android:text="@string/page1_tex1" />
<TextView
android:id="@+id/page1_text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:paddingLeft="60dp"
android:paddingBottom="10dp"
android:layout_alignLeft="@+id/page1_text1"
android:textColor="#ff0000"
android:text="@string/page1_tex2" />
</RelativeLayout>
</RelativeLayout>


<include layout="@layout/page3" /> </tjuci.dl.ui.MyScrollLayout></LinearLayout>


page2.Xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/page1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >

<TextView
android:id="@+id/page2_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/page1_title_bg"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:text="@string/page1_title"
android:textColor="#ffffff"
android:textSize="15dp" />

<TextView
android:id="@+id/page2_title_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/page2_title"
android:layout_centerHorizontal="true"
android:text="@string/page1_title_1"
android:textColor="#000000"
android:textSize="10dp" />

<ImageView
android:id="@+id/page2_image"
android:layout_width="fill_parent"
android:layout_height="240dp"
android:layout_below="@+id/page2_title_1"
android:layout_centerHorizontal="true"
android:src="@drawable/page2_image1" />

<RelativeLayout
android:id="@+id/page2_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/page2_image" >

<TextView
android:id="@+id/page2_show_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textSize="12sp"
android:text="@string/page2_date"
android:textColor="#000000" />

<LinearLayout
android:id="@+id/page2_images_layout"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignRight="@+id/page2_show_date"
android:gravity="right"
android:orientation="horizontal" >

eac6
<ImageView
android:layout_width="7dp"
android:layout_height="7dp"
android:layout_gravity="center_vertical"
android:layout_margin="5.0dp"
android:background="@drawable/page2_image_point_selector" />

<ImageView
android:layout_width="7dp"
android:layout_height="7dp"
android:layout_gravity="center_vertical"
android:layout_margin="5.0dp"
android:background="@drawable/page2_image_point_selector" />

<ImageView
android:layout_width="7dp"
android:layout_height="7dp"
android:layout_gravity="center_vertical"
android:layout_margin="5.0dp"
android:background="@drawable/page2_image_point_selector" />

<ImageView
android:layout_width="7dp"
android:layout_height="7dp"
android:layout_gravity="center_vertical"
android:layout_margin="5.0dp"
android:background="@drawable/page2_image_point_selector" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/page2_main_text_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="@+id/page2_layout">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="18sp"
android:textColor="#000000"
android:text="@string/page2_main_text_line"/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="13dp"
android:layout_marginRight="13dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textSize="13sp"
android:textColor="#777777"
android:text="@string/page2_main_text"/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="18sp"
android:textColor="#000000"
android:text="@string/page2_main_text_line"/>
</LinearLayout>
<LinearLayout
android:id="@+id/page2_bottom_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_marginBottom="14dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp"
android:textColor="#000000"
android:layout_marginTop="16dp"
android:background="@drawable/page2_bottom_button_bg"
android:text="@string/page2_bottom_button1"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView
android:layout_width="15dp"
android:layout_height="12dp"
android:layout_marginTop="12dp"
android:gravity="center"
android:text="8"
android:textSize="10sp"
android:textColor="#ffffff"
android:background="@drawable/page2_bottom_button_bg_tip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="bottom"
android:textSize="9sp"
android:text="216"
android:textColor="#777777"
/>
</LinearLayout>
<ImageView
android:layout_width="2dp"
android:layout_height="35dp"
android:layout_gravity="center_vertical"
android:layout_margin="5.0dp"
android:background="@drawable/page2_bottom_btn_line" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp"
android:textColor="#000000"
android:layout_marginTop="16dp"
android:background="@drawable/page2_bottom_button_bg"
android:text="@string/page2_bottom_button2"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView
android:layout_width="15dp"
android:layout_height="12dp"
android:layout_marginTop="12dp"
android:gravity="center"
android:text="40"
android:textSize="10sp"
android:textColor="#ffffff"
android:background="@drawable/page2_bottom_button_bg_tip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="bottom"
android:textSize="9sp"
android:text="40"
android:textColor="#777777"
/>
</LinearLayout>
<ImageView
android:layout_width="2dp"
android:layout_height="35dp"
android:layout_gravity="center_vertical"
android:layout_margin="5.0dp"
android:background="@drawable/page2_bottom_btn_line" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp"
android:textColor="#000000"
android:layout_marginTop="16dp"
android:background="@drawable/page2_bottom_button_bg"
android:text="@string/page2_bottom_button3"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView
android:layout_width="15dp"
android:layout_height="12dp"
android:layout_marginTop="12dp"
android:gravity="center"
android:text="11"
android:textSize="10sp"
android:textColor="#ffffff"
android:background="@drawable/page2_bottom_button_bg_tip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="bottom"
android:textSize="9sp"
android:text="563"
android:textColor="#777777"
/>
</LinearLayout>
<ImageView
android:layout_width="2dp"
android:layout_height="35dp"
android:layout_gravity="center_vertical"
android:layout_margin="5.0dp"
android:background="@drawable/page2_bottom_btn_line" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp"
android:textColor="#000000"
android:layout_marginTop="16dp"
android:background="@drawable/page2_bottom_button_bg"
android:text="@string/page2_bottom_button3"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:textSize="9sp"
android:text="96"
android:textColor="#777777"
/>
</LinearLayout>
<ImageView
android:layout_width="2dp"
android:layout_height="35dp"
android:layout_gravity="center_vertical"
android:layout_margin="5.0dp"
android:background="@drawable/page2_bottom_btn_line" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp"
android:textColor="#000000"
android:layout_marginTop="16dp"
android:background="@drawable/page2_bottom_button_bg"
android:text="@string/page2_bottom_button1"/>
</LinearLayout>
</RelativeLayout>

MyScrollLayout.java
package tjuci.dl.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
* 此类主要是横向滑动屏幕的类
*
* @author dl
*
*/
public class MyScrollLayout extends ViewGroup {
Scroller scroller;// 滑动控制
VelocityTracker velocity;// 速度控制
int mCurScreen;// 当前屏幕位置
float mLastX;//最近的一点X坐标
public static final int SNAP_VELOCITY = 600;

public MyScrollLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);// 初始化变量
}

public MyScrollLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);// 初始化变量
}

public MyScrollLayout(Context context) {
super(context);
init(context);// 初始化变量
}

public void init(Context context) {
scroller = new Scroller(context);// 创建滑动控制的对象
mCurScreen = 0;// 当前屏幕为0
}

/**
* 对子内容大小进行定义
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int childCount = getChildCount();// 得到子组件的数量
int width = MeasureSpec.getSize(widthMeasureSpec);
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);// 得到子组件
childView.measure(widthMeasureSpec, heightMeasureSpec);// 调用measure为子组件大小赋值
}

scrollTo(mCurScreen * width, 0);// 初始化滑动位置,使其滑动到第一个界面
}

/**
* 对内容的布局进行定义
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if(changed){
int childCount = getChildCount();//得到子组件的数量
int childLeft = 0;
for(int i = 0; i < childCount; i ++){
View childView = getChildAt(i);//得到子组件
int width = childView.getMeasuredWidth();//得到子组件的宽度
childView.layout(childLeft, 0, childLeft + width, childView.getMeasuredHeight());

childLeft += width;
}
}
}

@Override
public void computeScroll() {//当父组件要求子组件滑动式调用此方法
if(scroller.computeScrollOffset()){//当动画没有停止时
scrollTo(scroller.getCurrX(), scroller.getCurrY());//如果动画没有停止 那么一直更新子View的值
postInvalidate();
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
float curX = event.getX();
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN://按下操作时
if(velocity == null ){
velocity = VelocityTracker.obtain();//初始化速度控制
velocity.addMovement(event);//将触屏事件交给速度控制
}
if(!scroller.isFinished()){//还没完成滑动动画 但是已碰到屏幕
scroller.abortAnimation();//停止动画
}
mLastX = curX;
break;

case MotionEvent.ACTION_MOVE://移动操作时
int distance_x = (int)(mLastX - curX);
if(IsCanMove(distance_x)){//判断是否能移动
if(velocity != null ){
velocity.addMovement(event);//将触屏事件交给速度控制
}
mLastX = curX;
scrollBy(distance_x, 0);
}
break;
case MotionEvent.ACTION_UP://抬起操作时
int velocityX = 0;
if (velocity != null)
{
velocity.addMovement(event);
velocity.computeCurrentVelocity(1000);
velocityX = (int) velocity.getXVelocity();
}

if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
// Fling enough to move left
snapToScreen(mCurScreen - 1);
} else if (velocityX < -SNAP_VELOCITY
&& mCurScreen < getChildCount() - 1) {
// Fling enough to move right
snapToScreen(mCurScreen + 1);
} else {
snapToDestination();
}

if (velocity != null) {
velocity.recycle();
velocity = null;
}

break;
}
return true;//范围true 则说明 已完成操作 不用再扩张操作了
}

public void snapToDestination() {
final int screenWidth = getWidth();

final int destScreen = (getScrollX()+ screenWidth/2)/screenWidth;
snapToScreen(destScreen);
}

public void snapToScreen(int whichScreen) {

// get the valid layout page
whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));
if (getScrollX() != (whichScreen*getWidth())) {

final int delta = whichScreen*getWidth()-getScrollX();

scroller.startScroll(getScrollX(), 0,
delta, 0, Math.abs(delta)*2);

mCurScreen = whichScreen;
invalidate(); // Redraw the layout

}
}

/**
* @param distance_x 移动距离
* @return 是否能够移动
*/
public boolean IsCanMove(int distance_x){
//滑动向右滑、如果distance_x小于0 并且 偏移量小于0了 则不能滑动了
if(distance_x < 0 && getScrollX() < 0){
return false;
}
//滑动向右滑、如果滑动大于组件0 并且 偏移量大于了所有组件的宽度和、 那么则返回false
if(getScrollX() > (getChildCount() - 1) * getWidth() && distance_x > 0){
return false;
}
return true;
}
}


ScrollUIActivity.java
package tjuci.dl.ui;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ScrollUIActivity extends Activity {
// page2的变量
LinearLayout images_layout;// page2中images的线性布局
ImageView[] images_point;// 存放布局中的所有image点
int[] images;// 存放所有图片
int mCurImage = 1;// 当前的image
ImageView page_image;// 被换的image

// page3的变量
ExpandableListView expandable;
// 对于ExpandableListView的一级目录
String[] oneLayer = new String[] { "财务管理", "物资管理", "项目管理", "法律事务管理系统",
"设备管理", "人力资源" };
// 对于ExpandableListView的二级目录
String[][] twoLayer = new String[][] {
{ "财务管理1", "财务管理2", "财务管理3", "财务管理4", "财务管理5", "财务管理6", "财务管理7" },
{ "物资管理1", "物资管理2", "物资管理3", "物资管理4", "物资管理5", "物资管理6", "物资管理7",
"物资管理8", "物资管理9", "物资管理10", "物资管理11", "物资管理12", }, {}, {},
{}, {} };
int[] GroupImageId = { R.drawable.page3_image1, R.drawable.page3_image2,
R.drawable.page3_image3, R.drawable.page3_image4,
R.drawable.page3_image5, R.drawable.page3_image6 };

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
page2Event();// 处理page2的事件
page3Event();// 处理page3的事件
}

/**
* 处理page3页面的所有事件
*/
public void page3Event() {
// 初始化变量
expandable = (ExpandableListView) findViewById(R.id.page3_expandable);
expandable.setGroupIndicator(null);// 取消一级图标的箭头
ExpandableListAdapter ea = new BaseExpandableListAdapter() {
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(ScrollUIActivity.this);
ll.setLayoutParams(new AbsListView.LayoutParams(600,
ViewGroup.LayoutParams.WRAP_CONTENT));
ll.setPadding(20, 5, 5, 5);
ll.setOrientation(LinearLayout.HORIZONTAL);
ImageView iv = new ImageView(ScrollUIActivity.this);
iv.setImageResource(GroupImageId[groupPosition]);
ll.addView(iv);
TextView tv = new TextView(ScrollUIActivity.this);
tv.setLayoutParams(new AbsListView.LayoutParams(500,
AbsListView.LayoutParams.WRAP_CONTENT));
tv.setText(oneLayer[groupPosition]);
ll.addView(tv);
return ll;
}

@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}

@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return oneLayer.length;
}

@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return oneLayer[groupPosition];
}

@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return twoLayer[groupPosition].length;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView tv = new TextView(ScrollUIActivity.this);
tv.setText(twoLayer[groupPosition][childPosition]);
return tv;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return twoLayer[groupPosition][childPosition];
}
};
expandable.setAdapter(ea);
}

/**
* 处理page2页面的所有事件
*/
public void page2Event() {
// page2中变量的定义 与 赋值
images_layout = (LinearLayout) findViewById(R.id.page2_images_layout);
images_point = new ImageView[images_layout.getChildCount()];
page_image = (ImageView) findViewById(R.id.page2_image);
images = new int[] { R.drawable.page2_image1, R.drawable.page2_image2,
R.drawable.page2_image3, R.drawable.page2_image4 };

setScrollerImagePoint();// 初始化 page2中的 滚动点

final Handler h = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
if (mCurImage > images.length - 1) {// 如果大于图片的数目 那么重新开始加载
mCurImage = 0;
}
setScrollerImagePoint();
page_image.setAnimation(AnimationUtils.loadAnimation(
ScrollUIActivity.this, android.R.anim.fade_out));
page_image.setImageResource(images[mCurImage]);
page_image.setAnimation(AnimationUtils.loadAnimation(
ScrollUIActivity.this, android.R.anim.fade_in));
mCurImage++;
}
}
};

new Timer().schedule(new TimerTask() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
h.sendEmptyMessage(0x123);
}
}
}, 0, 500);
}

/*
* 为page2设置滚动点的效果
*/
public void setScrollerImagePoint() {
for (int i = 0; i < images_layout.getChildCount(); i++) {
images_point[i] = (ImageView) images_layout.getChildAt(i);
images_point[i].setEnabled(false);
}
images_point[mCurImage].setEnabled(true);// 设置可用的点 图片
}
}


                 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: