展会信息港展会大全

android imageswitcher gallery根据数据库内图片名字进行查看/删除
来源:互联网   发布日期:2015-10-03 10:45:03   浏览:1607次  

导读:layout/imageswitch.xml<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="h...

layout/imageswitch.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<ImageSwitcher

android:id="@+id/arc_hf_img_switcher"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true" />

<LinearLayout

android:id="@+id/linearLayout1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_alignParentLeft="true"

android:gravity="center" >

<Gallery

android:id="@+id/arc_hf_img_gallery"

android:layout_width="fill_parent"

android:layout_height="60dp"

android:layout_weight="1"

android:background="#55000000"

android:gravity="center_vertical"

android:spacing="16dp" />

<Button

android:id="@+id/arc_hf_img_btnDel"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginRight="30dp"

android:text="@string/arc_hf_img_btnDel" />

<Button

android:id="@+id/arc_hf_img_btnBack"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginRight="30dp"

android:text="@string/arc_hf_img_btnBack" />

</LinearLayout>

</RelativeLayout>

ImgSwitch.java

public class ImgSwitch extends Activity implements OnItemSelectedListener,

ViewFactory {

private final String DATABASE_PATH = Environment

.getExternalStorageDirectory().getAbsolutePath() + "/hfdatabase";

private final String DATABASE_FILENAME = "hf_database.db";

private final String TABLE_NAME = "HF_IMG_THEME";

private final String strImgPath = Environment.getExternalStorageDirectory()

.toString() + "/dlion/";// 存放照片的文件夹

private SQLiteDatabase db;

private ImageSwitcher is;

private Gallery gallery;

private Bitmap[] bitmaps;

private int[] _ids;

private String[] imgNames;

private int parentId;

public int gPosition = 0;

private int isPosition = 0;

private Button btnDel;

private Button btnBack;

// 返回键监听,添加关闭数据库功能。

public void onBackPressed() {

super.onBackPressed();

db.close();

finish();

}

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.imageswitch);

db = openDatabase();

parentId = this.getIntent().getIntExtra("parentId", 0);

gallery = (Gallery) findViewById(R.id.arc_hf_img_gallery);

is = (ImageSwitcher) findViewById(R.id.arc_hf_img_switcher);

is.setFactory(this);

is.setInAnimation(AnimationUtils.loadAnimation(this,

android.R.anim.fade_in));

is.setOutAnimation(AnimationUtils.loadAnimation(this,

android.R.anim.fade_out));

btnDel = (Button) findViewById(R.id.arc_hf_img_btnDel);

btnBack = (Button) findViewById(R.id.arc_hf_img_btnBack);

btnDel.setOnClickListener(new btnListener());

btnBack.setOnClickListener(new btnListener());

initImgs();

}

// 按键监听

class btnListener implements OnClickListener {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

switch (v.getId()) {

case R.id.arc_hf_img_btnBack:

db.close();

finish();

break;

case R.id.arc_hf_img_btnDel:

db.delete(TABLE_NAME, "_id=" + _ids[gPosition], null);

File file = new File(imgNames[gPosition]);

file.delete();

Toast.makeText(ImgSwitch.this, imgNames[gPosition] + "删除成功",

Toast.LENGTH_SHORT).show();

initImgs();

break;

default:

break;

}

}

}

// 初始化图片视图

private void initImgs() {

Cursor cursorImgName = db.query(TABLE_NAME, null, "parentId = "

+ parentId, null, null, null, null);

bitmaps = new Bitmap[cursorImgName.getCount()];

imgNames = new String[cursorImgName.getCount()];

_ids = new int[cursorImgName.getCount()];

// 优化内存

BitmapFactory.Options options = new BitmapFactory.Options();

options.inSampleSize = 20;

for (int i = 0; i < cursorImgName.getCount(); i++) {

cursorImgName.moveToNext();

// 保存图片组名字、ID、完整路径

imgNames[i] = strImgPath

+ cursorImgName.getString(cursorImgName

.getColumnIndex("imgName"));

_ids[i] = cursorImgName.getInt(cursorImgName.getColumnIndex("_id"));

bitmaps[i] = BitmapFactory.decodeFile(imgNames[i], options);

}

gallery.setAdapter(new ImageAdapter(this));

gallery.setOnItemSelectedListener(this);

if (gPosition == cursorImgName.getCount()

&& cursorImgName.getCount() >= 1) {

is.setImageURI(Uri.parse(imgNames[isPosition - 1]));

} else if (cursorImgName.getCount() == 0) { // 没有图片时,取消删除键监听

btnDel.setEnabled(false);

}

}

@Override

public View makeView() {

// TODO Auto-generated method stub

ImageView i = new ImageView(this);

i.setBackgroundColor(0xFF000000);

i.setScaleType(ImageView.ScaleType.FIT_CENTER);

i.setLayoutParams(new ImageSwitcher.LayoutParams(

LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

return i;

}

// 覆写baseadapter类

public class ImageAdapter extends BaseAdapter {

private Context mContext;

public ImageAdapter(Context c) {

mContext = c;

}

@Override

public int getCount() {

// TODO Auto-generated method stub

return bitmaps.length;

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return position;

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return position;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

// TODO Auto-generated method stub

gPosition = position;

ImageView i = new ImageView(mContext);

i.setImageBitmap(bitmaps[position]);

i.setAdjustViewBounds(true);

i.setLayoutParams(new Gallery.LayoutParams(

LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

return i;

}

}

@Override

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,

long arg3) {

// TODO Auto-generated method stub

isPosition = arg2;

is.setImageURI(Uri.parse(imgNames[arg2]));

}

@Override

public void onNothingSelected(AdapterView<?> arg0) {

// TODO Auto-generated method stub

}

// 初始化数据库

private SQLiteDatabase openDatabase() {

try {

String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;

File dir = new File(DATABASE_PATH);

if (!dir.exists())

dir.mkdir();

if (!(new File(databaseFilename)).exists()) {

InputStream is = getResources().openRawResource(

R.raw.hf_database);

FileOutputStream fos = new FileOutputStream(databaseFilename);

byte[] buffer = new byte[8192];

int count = 0;

// 开始复制hf_database.db文件

while ((count = is.read(buffer)) > 0) {

fos.write(buffer, 0, count);

}

}

db = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);

return db;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

}

摘自 oldfeel

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港