[java]代码库
package com.example.test2;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends Activity {
String[] path = new String[] {
"http://l2.51fanli.net//tuan//images//1//5806eac956808.jpg",
"http://l2.51fanli.net//tuan//images//b//580991bb30560.jpg",
"http://l0.51fanli.net//tuan//images//b//58115f2593dc3.jpg",
"http://l2.51fanli.net//tuan//images//0//57923840b054d.jpg",
"http://l2.51fanli.net//tuan//images//e//58101e11ab164.jpg", };
private TextView textView_main;
// 2小时的毫秒值
int count = 2 * 60 * 60 * 1000;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
//处理2小时倒计时
if(msg.what==1){
count = count - 1000;
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
String format = dateFormat.format(new Date(count));
textView_main.setText(format);
handler.sendEmptyMessageDelayed(1, 1000);
}else if(msg.what==2){
//处理轮播图
int currentItem = viewPager.getCurrentItem();
viewPager.setCurrentItem(currentItem+1);
handler.sendEmptyMessageDelayed(2, 1000);
}
};
};
private LinearLayout ll;
private ViewPager viewPager;
int index;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView_main = (TextView) findViewById(R.id.textView_main);
ll = (LinearLayout) findViewById(R.id.ll);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new MyAdapter(path, this));
//初始viewPager的页数,
//在Integer.MAX_VALUE的中间附近,所以开始时,就既可以向左滑,页可以向右滑
viewPager.setCurrentItem(Integer.MAX_VALUE/2 - Integer.MAX_VALUE/2%path.length );
//发送文字2小时倒计时
handler.sendEmptyMessageDelayed(1, 1000);
//发送轮播图自动轮播
handler.sendEmptyMessageDelayed(2, 1000);
addPoint();
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// 得到对应的线性布局的子控件
View childAt = ll.getChildAt(arg0%path.length);
childAt.setEnabled(false);
View view = ll.getChildAt(index%path.length);
view.setEnabled(true);
index=arg0;
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
private void addPoint() {
for (int i = 0; i < path.length; i++) {
ImageView imageView = new ImageView(this);
// 设置背景
imageView.setBackgroundResource(R.drawable.back);
// 设置控件参数
LayoutParams layoutParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// 设置右边距
layoutParams.rightMargin = 15;
imageView.setLayoutParams(layoutParams);
// 如果是第一个小点,就默认选中
if (i == 0) {
imageView.setEnabled(false);
}
// 把小圆点添加到线性布局里
ll.addView(imageView);
}
}
}
by: 发表于:2017-12-28 14:04:31 顶(0) | 踩(0) 回复
??
回复评论