sd图java_SD卡图片自动播放功能实现

package com.test;

import java.io.File;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

import android.view.View;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.BaseAdapter;

import android.widget.Gallery;

import android.widget.ImageView;

import android.widget.Toast;

import android.widget.AdapterView.OnItemClickListener;public classToolsActivity extends Activity {

Gallery g= null;

List it = null;//遍历符合条件的列表

public String actionUrl = null;//图片的数量

private int count_drawble = 0;//当前正在播放的图片ID

private int cur_index = 0;private boolean isalive = true;//线程循环运行的条件//更新条件

private static int MSG_UPDATE = 1;//SD卡路径

private final String SD_PATH =android.os.Environment

.getExternalStorageDirectory().getAbsolutePath();public voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.location_photo);

g=(Gallery) findViewById(R.id.mygallery);//添加一个ImageAdapter并设置给Gallery对象

g.setAdapter(new ImageAdapter(this, getSD()));

count_drawble= new ImageAdapter(this, getSD()).getCount();//设置一个itemclickListener并Toast被单击图片的位置

g.setOnItemClickListener(newOnItemClickListener() {public void onItemClick(AdapterView>parent, View v,int position, longid) {

Toast.makeText(ToolsActivity.this,"序列:" + (position + 1) + "\n路径:" + it.get(position),

Toast.LENGTH_LONG).show();

}

});//利用线程来更新 当前欲显示的图片id, 调用handler来选中当前图片

new Thread(newRunnable() {

@Overridepublic voidrun() {//TODO Auto-generated method stub

while(isalive) {

cur_index= cur_index % count_drawble; //图片区间[0,count_drawable)//msg.arg1 = cur_index

Message msg =mhandler.obtainMessage(MSG_UPDATE, cur_index,0);

mhandler.sendMessage(msg);//更新时间间隔为 2s

try{

Thread.sleep(2000);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

cur_index++; //放置在Thread.sleep(2000)//;防止mhandler处理消息的同步性,导致cur_index>=count_drawble

}

}

}).start();

}//通过handler来更新主界面//mgallery.setSelection(positon),选中第position的图片,然后调用OnItemSelectedListener监听改变图像

private Handler mhandler = newHandler() {public voidhandleMessage(Message msg) {if (msg.what ==MSG_UPDATE) {

g.setSelection(msg.arg1);//UI Thread直接更改图片 ,不利用Gallery.OnItemSelectedListener监听更改//imgSwitcher.setBackgroundResource(imgAdapter.getResId(msg.arg1));

}

}

};//遍历SD卡中某一路径下指定类型的图片

private ListgetSD() {

it= new ArrayList();

File f= new File(SD_PATH + "//" + "pictures");

File[] files=f.listFiles();for (int i = 0; i < files.length; i++) {

File file=files[i];if(getImageFile(file.getPath()))

it.add(file.getPath());

}returnit;

}//指定遍历文件类型

privateboolean getImageFile(String fName) {

boolean re;

String end=fName

.substring(fName.lastIndexOf(".") + 1, fName.length())

.toLowerCase();if (end.equals("jpg") || end.equals("gif") || end.equals("png")|| end.equals("jpeg") || end.equals("bmp")) {

re= true;

}else{

re= false;

}returnre;

}//改写BaseAdapter自定义一ImageAdapter class

public classImageAdapter extends BaseAdapter {//要是是图片的背景ID

intmGalleryItemBackground;privateContext mContext;private Listlis;public ImageAdapter(Context c, Listli) {

mContext=c;

lis=li;//使用在res/values/aatrs.xml中的定义

TypedArray a =obtainStyledAttributes(R.styleable.Gallery);//取得Gallery属性

mGalleryItemBackground =a.getResourceId(

R.styleable.Gallery_android_galleryItemBackground,0);//让对象的styleable属性能够反复使用

a.recycle();

}//重写的方法,返回图片数目

public intgetCount() {returnlis.size();

}//重写的方法,返回图片的数组id

public Object getItem(intposition) {returnposition;

}public long getItemId(intposition) {returnposition;

}//重写的方法,返回一View对象

public View getView(intposition, View convertView, ViewGroup parent) {//产生ImageView对象

ImageView i = newImageView(mContext);//设置图片给ImageView对象

Bitmap bm = BitmapFactory.decodeFile(lis.get(position).toString());

i.setImageBitmap(bm);//重新设置图片的宽度

i.setScaleType(ImageView.ScaleType.CENTER);//重新设置Layout的宽度

i.setLayoutParams(new Gallery.LayoutParams(1024, 768));//设置Callery背景图

i.setBackgroundResource(mGalleryItemBackground);returni;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值