首页实现
布局文件
首先页面的上半部分是一个轮播图的展现,在布局文件中应该设置一个ViewPager,可以让用户左右切换当前的view,还需要设置每个轮播的显示文字以及该页面是处于得到焦点的页面还是失去焦点的页面,是否处于焦点状态可以用两张图片的变换来表示,每个页面都有一个焦点标记,将该标记放在一个view中;在页面的下部是功能按钮以及对该功能按钮的标记文字,点击按钮可以进入到对应的功能中进行测试。
核心代码展示
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图片标题"
android:textColor="#FF00FF"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:orientation="horizontal" >
<View
android:id="@+id/dot_0"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_focused"/>
<View
android:id="@+id/dot_1"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_normal"/>
<!--有几张图片就有几个view>
</LinearLayout>
</LinearLayout>
<!--功能测试按钮-->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="onHeartRateClick">
<Button
android:id="@+id/heartbtn"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:background="@drawable/heart_rate"
android:onClick="onHeartRateClick" />
<TextView
android:id="@+id/hearttextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="40dp"
android:layout_toRightOf="@id/heartbtn"
android:text="心率测试"
android:textColor="@color/red"
android:textSize="20sp" />
</RelativeLayout>
Activity文件
在main_activity中要对轮播图的逻辑予以实现,ViewPager类需要一个PagerAdapter适配器类给它提供数据,所以需要在其中实现适配器类
main_activity代码:
public class MainActivity extends BaseActivity {
private ViewPager mViewPaper;
private List<ImageView> images;
private List<View> dots;
private int currentItem;
//记录上一次点的位置
private int oldPosition = 0;
//存放图片的id
private int[] imageIds = new int[]{
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d
};
//存放图片的标题
private String[] titles = new String[]{
"生命在于运动",
"常吃燕麦有益心脏健康",
"保护听力从我做起",
"清肺养肺,常吃这些"
};
private TextView title;
private ViewPagerAdapter adapter;
private ScheduledExecutorService scheduledExecutorService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置主界面布局
setContentView(R.layout.activity_main);
mViewPaper = (ViewPager) findViewById(R.id.vp);
//显示的图片
images = new ArrayList<ImageView>();
for(int i = 0; i < imageIds.length; i++){
ImageView imageView = new ImageView(this);
imageView.setBackgroundResource(imageIds[i]);
images.add(imageView);
}
//显示的小点
dots = new ArrayList<View>();
dots.add(findViewById(R.id.dot_0));
dots.add(findViewById(R.id.dot_1));
dots.add(findViewById(R.id.dot_2));
dots.add(findViewById(R.id.dot_3));
title = (TextView) findViewById(R.id.title);
title.setText(titles[0]);
adapter = new ViewPagerAdapter();
mViewPaper.setAdapter(adapter);
mViewPaper.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
title.setText(titles[position]);
dots.get(position).setBackgroundResource(R.drawable.dot_focused);
dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);
oldPosition = position;
currentItem = position;
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
//主界面上“心率检测”的点击事件
public void onHeartRateClick(View view) {
startActivity(new Intent(this, HeartRateActivity.class));//打开“心率检测”界面
}
//主界面上“听力测试”的点击事件
public void onEarListenClick(View view) {
startActivity(new Intent(this, EarListenActivity.class)); //打开“听力测试”界面
}
//主界面上“呼吸频率”的点击事件
public void onBreathSpeedClick(View view) {
startActivity(new Intent(this, BreathSpeedActivity.class));//打开“呼吸频率”界面
}
/**
* 自定义Adapter
* @author liuyazhuang
*
*/
private class ViewPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
return images.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup view, int position, Object object) {
// TODO Auto-generated method stub
// super.destroyItem(container, position, object);
// view.removeView(view.getChildAt(position));
// view.removeViewAt(position);
view.removeView(images.get(position));
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
// TODO Auto-generated method stub
view.addView(images.get(position));
return images.get(position);
}
}
/**
* 利用线程池定时执行动画轮播
*/
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleWithFixedDelay(
new ViewPageTask(),
2,
2,
TimeUnit.SECONDS);
}
/**
* 图片轮播任务
* @author liuyazhuang
*
*/
private class ViewPageTask implements Runnable{
@Override
public void run() {
currentItem = (currentItem + 1) % imageIds.length;
mHandler.sendEmptyMessage(0);
}
}
/**
* 接收子线程传递过来的数据
*/
private Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
mViewPaper.setCurrentItem(currentItem);
};
};
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(scheduledExecutorService != null){
scheduledExecutorService.shutdown();
scheduledExecutorService = null;
}
}
}