gpt4 book ai didi

android - 在 gridview 数组上叠加单个图像

转载 作者:行者123 更新时间:2023-11-30 00:36:32 26 4
gpt4 key购买 nike

我创建了一个 gridview,其中填充了一组图像,现在它有 9 张图像,但计划制作更大的图像。我正在尝试设置它,所以当我点击一个图像时,它将用单个图像(红色 x)覆盖该图像 我发现了几个在 gridview 顶部覆盖图像的示例,但它们似乎都使用另一个大批。我尝试将我的单个图像设置为数组,但这似乎不起作用。我有 onitemclicklistner 也在工作,我可以让图像显示为第一张图像。正在考虑尝试用 x 填充整个网格并将其隐藏(不确定这是否是最好的方法)

现在它不起作用,因为我正在尝试添加第二张图片。

我的网格适配器

public class EasyGridAdapter extends BaseAdapter {
private int[] easyPics;
private int[] overlayPics; //used for overlay ...this is just created here

Context myContext;

public EasyGridAdapter(Context myContext, int easyPics[], int overlayPics[]){
//sets gridadapter to use pics , if had text under pic would need to add after int[] pics
// name EasyGridAdapter is name ot the java class
this.myContext = myContext;
this.easyPics = easyPics;
this.overlayPics = overlayPics;

}

@Override
public int getCount() {
return easyPics.length;

}

@Override
public Object getItem(int position) {
return easyPics[position];

}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid = convertView;
if (convertView == null){

LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //not sure what for
grid = inflater.inflate(R.layout.custom_easy, null);

}
//main view
ImageView iv = (ImageView) grid.findViewById(R.id.ivPics); //ivPics is android id name of imageviewer in custom easy xml
ImageView ol = (ImageView) grid.findViewById(R.id.overlayImage);

iv.setImageResource(easyPics[position]); //easypics is the interger from above
ol.setImageResource(overlayPics[position]);



return grid; //returns grid which is name of grid on activity_main.xml (with all stuff in it)
}



**my custom xml file**

<ImageView
android:id="@+id/ivPics"
android:layout_width="150dp"
android:layout_height="150dp"
app:srcCompat="@drawable/e1"
/>

<!--added to overlay image when item clicked.-->
<ImageView
android:id="@+id/overlayImage"
android:layout_width="150dp"
android:layout_height="150dp"
app:srcCompat="@drawable/x"
android:visibility="gone"
/>

如果您需要查看更多内容,请告诉我我主要 Activity 的一部分

  public void onClick(View v) {
switch (v.getId()){
case R.id.btnEasy:

creatArray();
btnEasy.setVisibility(View.INVISIBLE); //hides button on the second activity
btnMed.setVisibility(View.INVISIBLE);
btnHard.setVisibility(View.INVISIBLE);
grid=(GridView)findViewById(R.id.grid);
// EasyGridAdapter adapter = new EasyGridAdapter(MainActivity.this, easyArray);
EasyGridAdapter adapter = new EasyGridAdapter(MainActivity.this, randomPics, overlay); //have to add all images (arrays here)

grid.setAdapter(adapter);

//listens for click on grid
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//sets what to do if each grid is pressed
if(position == 0){
Toast.makeText(MainActivity.this, "clicked 1!", Toast.LENGTH_SHORT).show();
// shows that was clicked. shows x
// ImageView overlay = (ImageView)findViewById(R.id.overlay);
// overlay.setVisibility(View.VISIBLE);

}
if(position == 1){

ImageView overlayImage = (ImageView)findViewById(R.id.overlayImage);
overlayImage.setVisibility(View.VISIBLE);
}
if(position == 2){

ImageView overlay = (ImageView)findViewById(R.id.overlayImage);
overlay.setVisibility(View.VISIBLE);

编辑自定义 xml

public class EasyGridAdapter extends BaseAdapter {
private int[] easyPics;
private int[] overlayPic; //used for overlay ...this is just created here

Context myContext;

public EasyGridAdapter(Context myContext, int easyPics[], int overlayPic []){
//sets gridadapter to use pics , if had text under pic would need to add after int[] pics
// name EasyGridAdapter is name ot the java class
this.myContext = myContext;
this.easyPics = easyPics;
this.overlayPic = overlayPic;

}

@Override
public int getCount() {
return easyPics.length;
// return overlayPic.length;

}

@Override
public Object getItem(int position) {
// return easyPics[position]; //wored with just easypics
return position;

}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid = convertView;
if (convertView == null){

LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //not sure what for
grid = inflater.inflate(R.layout.custom_easy, null);

}
//main view
ImageView iv = (ImageView) grid.findViewById(R.id.easyPics); //EasyPics is android id name of imageviewer in custom easy xml
ImageView ol = (ImageView) grid.findViewById(R.id.overlayPic);

iv.setImageResource(easyPics[position]); //easypics is the interger from above
ol.setImageResource(overlayPic[position]);



return grid; //returns grid which is name of grid on activity_main.xml (with all stuff in it)
}

最佳答案

1) 从您的 GridView 中删除/评论 onItemClickListener:

2) 我假设您使用 RelativeLayout 作为 custom_easy.xml 的父级。 修改 getView()方法如下:

 public class Holder
{
RelativeLayout rlParent;
ImageView iv;
ImageView ivOverlay;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid = convertView;
final Holder holder=new Holder();
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //not sure what for
grid = inflater.inflate(R.layout.custom_easy, null);

}
//main view
holder.rlParent = (RelativeLayout) grid.findViewById(R.id.yourParentLayout);
holder.iv = (ImageView) grid.findViewById(R.id.ivPics); //ivPics is android id name of imageviewer in custom easy xml
holder.ol = (ImageView) grid.findViewById(R.id.overlayImage);

holder.iv.setImageResource(easyPics[position]); //easypics is the interger from above
holder.relParent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.ol.setVisibility(View.VISIBLE);
}
});

return grid; //returns grid which is name of grid on activity_main.xml (with all stuff in it)
}

希望这对您有所帮助。

关于android - 在 gridview 数组上叠加单个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43336324/

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