简体   繁体   English

缩略图与gridview中的实际图像不同

[英]thumbnail different from actual image in gridview

I have created a simple GridView application in android in which the images when clicked opens up in a new window as fullscreen . 我在android中创建了一个简单的GridView应用程序,单击该图像时在新窗口中以全屏方式打开图像。

Application image : 应用图片:

在此处输入图片说明

在此处输入图片说明

I also put the random image generation code in the getView method as follows in ImageAdapter Class: 我还在ImageAdapter类中将随机图像生成代码放入getView方法中,如下所示:

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
    ImageView iv;
    shuffleArray(images);
    if(arg1!=null){
        iv=(ImageView)arg1;
    }else{
        iv=new ImageView(context);
        iv.setLayoutParams(new GridView.LayoutParams(80,80)); //resizing the picture according to the params
        iv.setScaleType(ScaleType.CENTER_CROP);//images will be cropped towards center
        iv.setPadding(8, 8, 8, 8); //padding of all sides if images vary then less padding more crop to fit imageview
    }

    iv.setImageResource( images[arg0]);
    return iv;
}

static void shuffleArray(int[] images2) {
    // TODO Auto-generated method stub
    Random rnd=new Random();
    for(int i=images2.length-1;i>=0;i--){
        int index=rnd.nextInt(i+1);
        int temp=images2[index];
        images2[index]=images2[i];
        images2[i]=temp;
    }

}

Every time I open app the pictures get shuffled with some problems. 每次我打开应用程序时,图片都会出现一些问题。

Now the problem is as follows : 现在问题如下:

  • Whenever I click on the image thumbnail a different image opens in a new window . 每当我单击图像缩略图时,就会在新窗口中打开另一幅图像。

  • Same image thumbnail is showing in GridView Activity for two or more different images. 相同的图像缩略图显示在GridView活动中,用于两个或多个不同的图像。

  • I am also trying to put a condition that the app when run should have all Hardware buttons Disabled 我还试图提出一个条件,即应用程序在运行时应禁用所有硬件按钮

AND

  • whenever user clicks on a specific NO of images the app should close automatically with message . 每当用户单击特定的图像编号时,应用程序应自动关闭并显示消息。

How should I achieve solutions of problems mentioned above ? 我应该如何解决上述问题

my whole source code: Main Activity.java 我的整个源代码: Main Activity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GridView gv=(GridView)findViewById(R.id.gridView);
    gv.setAdapter(new ImageAdapter(getApplicationContext()));

    gv.setOnItemClickListener(new OnItemClickListener(){

        private boolean[] isSpecificImage;{
            isSpecificImage[0]=true;
        }





        @Override
        public void onItemClick(AdapterView<?> parentView, View iv, int position, long id) {
            // TODO Auto-generated method stub

                Toast.makeText(getApplicationContext(), ""+position, Toast.LENGTH_SHORT).show();
                Intent i=new Intent(getApplicationContext(),ImageDetail.class);  // for click open that image as new activity
                i.putExtra("id", position);
                startActivity(i);



        }

    });

ImageAdapter class ImageAdapter类

public class ImageAdapter extends BaseAdapter {

private Context context; 私有上下文上下文; public static int[] images={ R.drawable.goku,R.drawable.gohan,R.drawable.vegeta,R.drawable.broly,R.drawable.buu }; 公共静态int []图片= {R.drawable.goku,R.drawable.gohan,R.drawable.vegeta,R.drawable.broly,R.drawable.buu};

public ImageAdapter(Context applicationContext) {
     context = applicationContext;
        shuffleArray(images);
}

@Override
public int getCount() {
    // no of items to be displayed
    return images.length;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}


@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
    ImageView iv;

    if(arg1!=null){
        iv=(ImageView)arg1;
    }else{
        iv=new ImageView(context);
        iv.setLayoutParams(new GridView.LayoutParams(80,80)); //resizing the picture according to the params
        iv.setScaleType(ScaleType.CENTER_CROP);//images will be cropped towards center
        iv.setPadding(8, 8, 8, 8); //padding of all sides if images vary then less padding more crop to fit imageview
    }

    iv.setImageResource( images[arg0]);
    return iv;
}

 void shuffleArray(int[] images2) {
    // TODO Auto-generated method stub
    Random rnd=new Random();
    for(int i=images2.length-1;i>=0;i--){
        int index=rnd.nextInt(i+1);
        int temp=images2[index];
        images2[index]=images2[i];
        images2[i]=temp;
    }

}

} }

ImageDetail class ImageDetail类

public class ImageDetail extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.imagedetail);
    Intent i=getIntent();
    int position =(Integer) i.getExtras().get("id");
    ImageView iv=(ImageView)findViewById(R.id.ivdbz);
    iv.setImageResource(ImageAdapter.images[position]);
}

} }

在此处输入图片说明

shuffleArray(images); 

That statement does not belong in getView() . 该语句不属于getView() You should not shuffle every time getView() is called. 您不应在每次调用getView()时都进行随机播放。

Call it somewhere else. 叫别的地方。

Well you can work with the positions in the OnClickItemListener to put finish() 好吧,您可以使用OnClickItemListener中的位置放置finish()

condition in your gridView 您的gridView中的条件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM