gpt4 book ai didi

android viewflow 从 sdcard 刷卡图像

转载 作者:行者123 更新时间:2023-11-29 22:18:34 26 4
gpt4 key购买 nike

我正在做一个将一些图像保存到 sdcard 的项目,现在我必须创建 View 流来显示这些图像。我通过 sdCard 的 Id 获取图像,我现在的问题是如何在不同图像之间滑动。这是我正在使用的代码:

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class Cards extends Activity {

public Cursor cursor;
int position;
int indexxx;
Bitmap b;
int objectId;
int cardsId;

private ViewFlow viewFlow;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.circle_layout);
UserDatabaseHelper userDbHelper = new UserDatabaseHelper(this, null, 1);
userDbHelper.initialize(this);


final int cardId = getIntent().getIntExtra("card_id",0);
Log.i("Card Id ","Card Id : "+cardId);
final int collId = getIntent().getIntExtra("collection_id",0);
Log.i("Collection Id ","Collection Id : "+collId);

position = getIntent().getIntExtra("position",0);
Log.i("position","position : "+position);

String cardSQL = "SELECT cm.objectId "+
"FROM cardmedias AS cm "+
"INNER JOIN cards AS cd "+
"ON (cm.cardId = cd.objectId) "+
"WHERE cd.collectionId="+collId;

Cursor cards = userDbHelper.executeSQLQuery(cardSQL);
if (cards.getCount() == 0) {
Log.i("", "No Image file");
cards.close();
} else if (cards.getCount() > 0) {
for (cards.move(0); cards.moveToNext(); cards.isAfterLast()) {
cardsId = Integer.parseInt(cards.getString(cards
.getColumnIndex("objectId")));
Log.i("", "cards objectId : " + cardsId);

String path = Environment.getExternalStorageDirectory()
+ "/.Stampii/MediaCard/" + cardsId + ".png";
Log.i("", "path : " + path);
}
}

String sql = "SELECT objectId FROM cardmedias WHERE cardId=" + cardId
+ " LIMIT 1";
Cursor cursor = userDbHelper.executeSQLQuery(sql);
if (cursor.getCount() == 0) {
Log.i("", "No Image file");
cursor.close();
} else if (cursor.getCount() > 0) {
for (cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
objectId = Integer.parseInt(cursor.getString(cursor
.getColumnIndex("objectId")));
Log.i("", "objectId : " + objectId);
}
}



Button info = (Button) findViewById(R.id.info_button);
info.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(Cards.this, SingleCardInfo.class);
intent.putExtra("card_id", cardId);
intent.putExtra("collection_id", collId);
startActivity(intent);
}
});

Button back = (Button) findViewById(R.id.back_button);
back.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
finish();
}
});

final ArrayList<Bitmap> images = new ArrayList<Bitmap>();
String path = Environment.getExternalStorageDirectory()+"/.Stampii/MediaCard/"+objectId+".png";

BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];

Bitmap b = BitmapFactory.decodeFile(path, options);
images.add(b);

viewFlow = (ViewFlow) findViewById(R.id.viewflow);
viewFlow.setAdapter(new ImageAdapter(this, images),position);


ImageButton prevBtn = (ImageButton) findViewById(R.id.previous_button);
prevBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
indexxx = viewFlow.getSelectedItemPosition()-1;
if (indexxx>=0) {
viewFlow.setAdapter(new ImageAdapter(Cards.this, images),indexxx);
viewFlow.setSelectedItemPosition(indexxx);
Log.i("indexxx", "indexxx : " + indexxx);
}
}
});

ImageButton nextBtn = (ImageButton) findViewById(R.id.next_button);
nextBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
indexxx = viewFlow.getSelectedItemPosition()+1;
if (indexxx<=images.size()) {
viewFlow.setAdapter(new ImageAdapter(Cards.this, images),indexxx);
viewFlow.setSelectedItemPosition(indexxx);
Log.i("indexxx", "indexxx : " + indexxx);
}
}
});

}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
viewFlow.onConfigurationChanged(newConfig);
}

}

这是我的 ImageAdapter 类:

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

private LayoutInflater mInflater;
private ArrayList<Bitmap> ids = new ArrayList<Bitmap>();
private Bitmap bitmap;

public ImageAdapter(Context context, ArrayList<Bitmap> images) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ids = images;
}

@Override
public int getCount() {
return 1;
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.image_item, null);
}
((ImageView) convertView.findViewById(R.id.imgView)).setImageBitmap(ids.get(position));
return convertView;
}

}

知道如何通过从数据库中获取图像 ID、找到具有该名称的图像并将其添加到 View 流和滑动来设置下一张或上一张图像。我不希望有人代替我编写代码...我一直在想我该怎么做...所以如果有人可以帮助我提供建议、想法或其他东西,那就去做吧。

提前致谢!

最佳答案

我认为您已经非常接近成功了。

getCount() 应该是这样的:

@Override
public int getCount() {
return ids.size();
}

已编辑:
您应该只创建一个 ImageAdapter 实例,您正在 onClick 中创建新实例。您还应该使用 setSelection() 而不是 setSelectedItemPosition()。似乎后者是供内部使用的:

        @Override
public void onClick(View v) {
indexxx = viewFlow.getSelectedItemPosition()-1;
if (indexxx>=0) {
viewFlow.setSelection(indexxx);
Log.i("indexxx", "indexxx : " + indexxx);
}
}
/* ... */
@Override
public void onClick(View v) {
indexxx = viewFlow.getSelectedItemPosition()+1;
if (indexxx<images.size()) { // Should be <, you have a wrong boundary check here!!!
viewFlow.setSelection(indexxx);
Log.i("indexxx", "indexxx : " + indexxx);
}
}

关于android viewflow 从 sdcard 刷卡图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7900770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com