gpt4 book ai didi

android - 根据gridview android中的日期和时间对图像进行排序

转载 作者:行者123 更新时间:2023-12-02 01:41:40 24 4
gpt4 key购买 nike

//图片保存路径

/storage/emulated/0/Pictures/CameraExample/JPEG_20150107_152640.jpeg

//这是文件路径之一。

//我需要对图像进行排序,例如最后拍摄的照片作为第一个显示//在 GridView 中显示图像,最近的照片在//最后。

下面是我的代码

 public class ImageAdapter extends BaseAdapter{


private Context context;
private int imageWidth;
private Activity activity;
ArrayList<String> itemList = new ArrayList<String>();
Bitmap bitmap;

public ImageAdapter(Activity activity,ArrayList<String> itemList,int imageWidth) {
this.activity = activity;
this.itemList = itemList;
this.imageWidth = imageWidth;

}

@Override
public int getCount() {

return this.itemList.size();
}

void add(String path) {
itemList.add(path);

}

@Override
public Object getItem(int position) {
return this.itemList.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;

if (convertView == null) {

imageView = new ImageView(activity);


}
else {
imageView = (ImageView) convertView;

}

imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

imageView.setLayoutParams(new GridView.LayoutParams(imageWidth ,imageWidth ));


bitmap = decodeFile(itemList.get(position), imageWidth, imageWidth);

imageView.setImageBitmap(bitmap);

imageView.setOnClickListener( new OnImageClickListener(position));


return imageView;
}


class OnImageClickListener implements View.OnClickListener {

int position;

public OnImageClickListener(int position){
this.position = position;
}

@Override
public void onClick(View v) {
Intent intent = new Intent(activity, ImageDisplayActivity.class);

intent.putExtra("position", position);

Log.d("ImageAdapter","Intent.putExtra ");
activity.startActivity(intent);

}
}

public Bitmap decodeFile(String path, int reqWidth, int reqHeight) {
try {
File f = new File(path);
BitmapFactory.Options o = new BitmapFactory.Options();

o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
final int REQUIRED_WIDTH = reqWidth;
final int REQUIRED_HEIGHT = reqHeight;
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_WIDTH && o.outHeight / scale / 2 >= REQUIRED_HEIGHT)
scale *= 2;

BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);

} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
}

//最后当我滚动我的 GridView 时。滚动不是那么流畅。请帮我解决两个问题。

//我的 GridView Activity 类

public class GridViewActivity extends Activity {

private ImageAdapter imageAdapter;
private HelperUtils utils;
private ArrayList<String> itemList = new ArrayList<String>();
private GridView gridView;
private int columnWidth;
private Activity activity;


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gridview);

gridView = (GridView) findViewById(R.id.gridView);

utils = new HelperUtils(this);

InitilizeGridLayout();

itemList = utils.getFilePaths();

imageAdapter = new ImageAdapter(this, itemList, columnWidth);

gridView.setAdapter(imageAdapter);

}

private void InitilizeGridLayout() {

Resources r = getResources();
float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, HelperAppConstant.GRID_PADDING, r.getDisplayMetrics());
columnWidth = (int) ((utils.getScreenWidth() - ((HelperAppConstant.NUM_OF_COLUMNS + 1) * padding)) / HelperAppConstant.NUM_OF_COLUMNS);

gridView.setNumColumns(HelperAppConstant.NUM_OF_COLUMNS);
gridView.setColumnWidth(columnWidth);
gridView.setStretchMode(GridView.NO_STRETCH);
gridView.setPadding((int) padding, (int) padding, (int) padding, (int) padding);
gridView.setHorizontalSpacing((int) padding);
gridView.setVerticalSpacing((int) padding);


}
}

最佳答案

getView 方法中删除 Collections.sort(itemList);

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;

if (convertView == null) {
Log.d("ImageAdapter","Inside the if condition");
imageView = new ImageView(activity);

}
else {
imageView = (ImageView) convertView;
}

imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(imageWidth ,imageWidth ));

// bitmap = decodeFile(itemList.get(position), imageWidth, imageWidth);

bitmap = decodeFile(getItem(position), imageWidth, imageWidth);

imageView.setImageBitmap(bitmap);

return imageView;
}

在 onCreate 方法中填充 GridView 之前对列表进行排序。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gridview);

gridView = (GridView) findViewById(R.id.gridView);

utils = new HelperUtils(this);

InitilizeGridLayout();

itemList = utils.getFilePaths();

Collections.sort(itemList);

imageAdapter = new ImageAdapter(this, itemList, columnWidth);

gridView.setAdapter(imageAdapter);

}

你可以按照这个例子in this link通过创建包含图像文件路径和图像位图的项目列表。这样,您就不必在每次调用 getView 时都对位图进行解码。您可以在填充 gridview 之前对其解码一次。

由于图像是按升序排列的,您可以将 getItem 方法替换为以下方法:

    @Override
public Object getItem(int position) {
return this.itemList.get(itemList.size() - 1 - position);
}

我更新了 getView 方法。您应该使用 getItem(position) 而不是 itemList.get(position) 来解码位图。

关于android - 根据gridview android中的日期和时间对图像进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28161743/

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