gpt4 book ai didi

java - Android 中用于突出显示网格布局的选定网格的 setForeground 的替代方法

转载 作者:数据小太阳 更新时间:2023-10-29 02:44:17 25 4
gpt4 key购买 nike

我正在使用 Glide 在 GridView 中显示图像列表。当用户点击任何图像时,我需要用一个刻度线来突出显示它。

我使用 setForeground 成功地做到了,但它只适用于 Android 6.0 及更高版本。

我尝试了选择器但没有奏效,因为颜色出现在不可见的图像后面。

现在如何突出显示所选图像?

GridView gallery = (GridView) findViewById(R.id.galleryGridView);

gallery.setAdapter(new ImageAdapter(this));

gallery.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
if (null != images && !images.isEmpty())

arg1.setForeground(getDrawable(R.drawable.tick)); //Min API 23
}
});
}

图像适配器

private class ImageAdapter extends BaseAdapter {


public ImageAdapter(Activity localContext) {
context = localContext;
images = getAllShownImagesPath(context);
}

public int getCount() {
return images.size();
}

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

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

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

if (convertView == null) {
picturesView = new ImageView(context);
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView
.setLayoutParams(new GridView.LayoutParams(270, 270));

} else {
picturesView = (ImageView) convertView;
}

Glide.with(context).load(images.get(position))
.placeholder(R.mipmap.ic_launcher).centerCrop()
.into(picturesView);

return picturesView;
}

private ArrayList<String> getAllShownImagesPath(Activity activity) {
Uri uri;
Cursor cursor;
int column_index_data, column_index_folder_name;
ArrayList<String> listOfAllImages = new ArrayList<String>();
String absolutePathOfImage = null;
String orderBy = MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC";


// Get relevant columns for use later.
String[] projection = {
MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.DATE_ADDED,
MediaStore.Files.FileColumns.MEDIA_TYPE,
MediaStore.Files.FileColumns.MIME_TYPE,
MediaStore.Files.FileColumns.TITLE
};

// Return only video and image metadata.
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
+ " OR "
+ MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;

Uri queryUri = MediaStore.Files.getContentUri("external");

cursor = activity.getContentResolver().query(queryUri,
projection,
selection,
null, // Selection args (none).
MediaStore.Files.FileColumns.DATE_ADDED + " DESC");

column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
// column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data);

listOfAllImages.add(absolutePathOfImage);
}
return listOfAllImages;
}
}

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<GridView
android:id="@+id/galleryGridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:gravity="center"
android:horizontalSpacing="2dp"
android:numColumns="3"
android:padding="2dp"

android:verticalSpacing="2dp"

android:background="#303030">
</GridView>

</RelativeLayout>

最佳答案

使用来自 libraryForegroundImageView代替 ImageView。因为 setForeground 方法仅适用于 API >= 23。

关于java - Android 中用于突出显示网格布局的选定网格的 setForeground 的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37560572/

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