启动页面的设置以及ListView的优化实例
废话不多说,看代码以及注释:
欢迎界面的自动跳转
首先创建启动界面的xml文件 activity_splash.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@drawable/aa"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_splash_icon"
android:src="@drawable/ic_launcher"
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_centerInParent="true"/>
<LinearLayout
android:gravity="center_vertical"
android:layout_below="@id/iv_splash_icon"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:layout_width="30dip"
android:layout_height="30dip"/>
<TextView
android:textColor="#ffffff"
android:layout_marginLeft="5dip"
android:text="软件正在启动中…"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
再创建 SplashActivity.java 文件:
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
//设置隐藏标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//耗时的操作要放在子线程中
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startVideoList();
}
}, 2000); //两秒后自动跳转
}
//用来判断视屏启动界面启动了没有
private boolean isStartVideoList = false;
//启动是视屏列表
private void startVideoList(){
if(!isStartVideoList){
isStartVideoList = true;
Intent intent = new Intent(this, VideoListActivity.class);
startActivity(intent);
//关闭当前启动页面
finish();
}
}
//点击启动界面可跳过设置的时间直接进入所要跳转的界面
@Override
public boolean onTouchEvent(MotionEvent event) {
startVideoList();
return super.onTouchEvent(event);
}
}
列表的优化:
创建列表的xml文件activity_videolist.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@drawable/aa"
android:layout_height="match_parent" >
<ListView
android:cacheColorHint="@android:color/transparent"
android:id="@+id/lv_videolist"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/lv_videolist_novideo"
android:visibility="gone"
android:textColor="#ffffff"
android:text="没有发现视屏..."
android:textSize="18sp"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
接下来创建java文件VideoListActivity.java:
public class VideoListActivity extends Activity {
private ListView lv_videolist;
private TextView lv_videolist_novideo;
private List<VideoItem> videoItems;
private Utils utils;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
if(videoItems != null && videoItems.size() > 0){
//有视屏信息
lv_videolist_novideo.setVisibility(View.GONE);
//显示在ListView中
lv_videolist.setAdapter(new VideoListAdapter());
}else{
//没有视屏信息
lv_videolist_novideo.setVisibility(View.VISIBLE);
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videolist);
initView();
getData();
}
class VideoListAdapter extends BaseAdapter{
//返回总条数
@Override
public int getCount() {
return videoItems.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
//把布局文件实例化 --> View 对象
View view;
ViewHolder holder;
//有历史缓存就用历史的
if(arg1 != null){
view = arg1;
System.out.println("使用历史缓存的View:"+ arg0);
holder = (ViewHolder) view.getTag();
}else{
System.out.println("创建新的View"+ arg0);
view = View.inflate(VideoListActivity.this, R.layout.videolist_item, null);
holder = new ViewHolder();
//查找View的ID也是消耗资源的,当创建View的时候,把查找的放在一个容器(类)中
//实例化孩子
holder.tv_videolist_name = (TextView) view.findViewById(R.id.tv_videolist_name);
holder.tv_videolist_duration = (TextView) view.findViewById(R.id.tv_videolist_duration);
holder.tv_videolist_size = (TextView) view.findViewById(R.id.tv_videolist_size);
//把容器和View 关系保存起来
view.setTag(holder);
}
//根据位置得到对应的视屏信息
VideoItem item = videoItems.get(arg0);
holder.tv_videolist_name.setText(item.getName());//设置名称
holder.tv_videolist_size.setText(Formatter.formatFileSize(VideoListActivity.this, item.getSize()));
holder.tv_videolist_duration.setText(utils.stringForTime((int)item.getDuration()));
return view;
}
}
//放在类中可减少对布局查找ID的操作,每次实例化的时候直接在这里取即可
public static class ViewHolder{
TextView tv_videolist_name;
TextView tv_videolist_duration;
TextView tv_videolist_size;
}
//得到手机的视屏
private void getData() {
new Thread(){
public void run(){
//实例化列表
videoItems = new ArrayList<VideoItem>();
//视屏的路径
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = {
MediaStore.Video.Media.DISPLAY_NAME,//视屏的名称
MediaStore.Video.Media.DURATION,//视屏的时长
MediaStore.Video.Media.SIZE,//视屏的大小
MediaStore.Video.Media.DATA,//视屏在sdcard下的绝对路径
};
//读取手机里面所有的视屏
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
while (cursor.moveToNext()) {
VideoItem item = new VideoItem();
String name = cursor.getString(0);//视屏的名称
item.setName(name);
long duration = cursor.getLong(1);//视屏长度
item.setDuration(duration);
long size = cursor.getLong(2);//视屏大小
item.setSize(size);
String data = cursor.getString(3);//视屏的播放地址
item.setData(data);
//放入视屏列表中
videoItems.add(item);
}
cursor.close();//关闭
//发消息到主线程中显示数据
handler.sendEmptyMessage(1);
};
}.start();
}
//初始化View
private void initView(){
lv_videolist = (ListView) findViewById(R.id.lv_videolist);
lv_videolist_novideo = (TextView) findViewById(R.id.lv_videolist_novideo);
utils = new Utils();
}
}
以上设计到的文件如下:
videolist_item.xml:
//列表选项布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dip" >
<ImageView
android:id="@+id/iv_videolist_icon"
android:layout_width="60dip"
android:layout_height="60dip"
android:layout_marginLeft="5dip"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv_videolist_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginTop="8dip"
android:layout_toRightOf="@id/iv_videolist_icon"
android:text="视屏的名称"
android:textColor="#ffffff"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_videolist_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_videolist_name"
android:layout_marginLeft="5dip"
android:layout_marginTop="5dip"
android:layout_toRightOf="@id/iv_videolist_icon"
android:text="视屏的长度"
android:textColor="#ffffff"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_videolist_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dip"
android:layout_marginTop="8dip"
android:text="视屏的大小"
android:textColor="#66ffff"
android:textSize="14sp" />
<View
android:background="#66ffff"
android:layout_width="match_parent"
android:layout_height="0.2dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Utils.java文件:
public class Utils {
private StringBuilder mFormatBuilder;
private Formatter mFormatter;
public Utils() {
// 转换成字符串的时间
mFormatBuilder = new StringBuilder();
mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
}
/**
* 把毫秒转换成:1:20:30这里形式
* @param timeMs
* @return
*/
public String stringForTime(int timeMs) {
int totalSeconds = timeMs / 1000;
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
mFormatBuilder.setLength(0);
if (hours > 0) {
return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds)
.toString();
} else {
return mFormatter.format("%02d:%02d", minutes, seconds).toString();
}
}
}
VideoItem.java文件:
//get 与 set 方法省略
public class VideoItem {
private String name;
private long duration;
private long size;
private String data;
}