简体   繁体   English

在Android中使用GridView与图像

[英]using gridview with images in android

this is the problematic code: 这是有问题的代码:

public class Level1 extends Activity {

int[] logos = {
        R.drawable.arutz8,
        R.drawable.channel1,
        R.drawable.doctor_gav,
        R.drawable.foxgroup3,
        R.drawable.careline,
        R.drawable.golfnew,
        R.drawable.haaretz,
        R.drawable.hafenix,
        /*R.drawable.hando,
        R.drawable.bankleumi,
        R.drawable.jerusalempostred,
        R.drawable.laisha,
        R.drawable.logo,
        R.drawable.logodelta,
        R.drawable.maariv,
        R.drawable.pelephone,
        R.drawable.ravbariah,
        R.drawable.renuar,
        R.drawable.reshet_tv,
        R.drawable.sano,
        R.drawable.shilav,
        R.drawable.sport5,
        R.drawable.srigamish,
        R.drawable.steimatzky,
        R.drawable.superpharm,
        R.drawable.supersal,
        R.drawable.tambur,
        R.drawable.tzometsfarim,
        R.drawable.walla,
        R.drawable.yediot,*/
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.level1);
    ListAdapter adapter = (new ArrayAdapter<Integer>(this, R.layout.level1));

    GridView grid = (GridView) findViewById(R.id.gridview1);
    grid.setAdapter(new ImageAdapter(this));
}


private class ImageAdapter extends BaseAdapter 
{
    private Context context;

    public ImageAdapter(Context c) 
    {
        context = c;
    }

    //---returns the number of images---
    public int getCount() {
        return logos.length;
    }

    //---returns the ID of an item--- 
    public Object getItem(int position) {
        return position;
    }

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

    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(90, 90));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setPadding(5, 5, 5, 5);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(logos[position]);
        return imageView;
    }
} 

} }

This program works perfectly as long as the size of all images is about 60px per each image. 只要所有图像的大小每个图像约60像素,此程序就可以完美运行。 The problem in that size of image is that every image got constricted and its ugli!. 图像大小的问题在于,每个图像都受到限制,而且图像很丑陋! I tried to use in bigger images and for some reason only the first five images appears on the screen and when i try to load the rest images, the app crashes. 我尝试使用较大的图像,由于某种原因,只有前五个图像出现在屏幕上,而当我尝试加载其余图像时,应用程序崩溃。 I thought that happens because of the size of the images but than i realized that all the images are in the same big size and still 5 of them were drawn on the screen. 我以为是因为图像的大小而发生的,但后来我意识到所有图像都具有相同的大尺寸,仍然在屏幕上绘制了其中的5张。 Any ideas? 有任何想法吗?

Those are the logs from logCat: 这些是来自logCat的日志:

logs 日志

and this is the result when i press on the level 1 button: 这是我按1级按钮时的结果:

result 结果

There is a problem in your public View getView(...) method in your ImageAdapter. ImageAdapter的公共View getView(...)方法中存在问题。 When the convertView == null, you never link the convertView to your imageView. 当convertView == null时,您永远不会将convertView链接到您的imageView。 So there will be a problem in the else-statement. 因此,else语句中将存在问题。 You can do this in two ways: 您可以通过两种方式执行此操作:

  • By defining your ImageView in code like you did, in your case this will be the best choice: 通过像您一样在代码中定义ImageView,这将是最佳选择:

     ImageView imageView = (ImageView) convertView; 

    \n\n

    if (convertView == null) { convertView = new ImageView(context); 如果(convertView == null){convertView = new ImageView(context); imageView = (ImageView) convertView; imageView =(ImageView)convertView; // Set other parameters } //设置其他参数}

    \n\n

    // Set resource //设置资源

    \n\n

    return convertView; 返回convertView;

  • By defining your layout in a xml-layout file an using a layout inflater: 通过在xml-layout文件中定义布局,并使用布局充气工具:

Handler handler; if (convertView == null) { LayoutInflater li = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = li.inflate(yourLayoutResourceId, parent, false); handler = new Handler(); handler.itemIV = (ImageView) convertView.findViewById(imageViewLayoutId); convertView.setTag(handler); } else { handler = (Handler) convertView.getTag(); } handler.imageView.setImageResource(...); return convertView; Where yourLayoutResourceId is the id of the created xml-layout file (R.layout.exmaple), and imageViewLayoutId is the id of your imaeView in the layout (R.id.exmapleIV). As last step define an inner class Handler in your ImageAdapter: class Handler { ImageView imageView; }

 Handler handler;  if (convertView == null) { LayoutInflater li = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = li.inflate(yourLayoutResourceId, parent, false); handler = new Handler(); handler.itemIV = (ImageView) convertView.findViewById(imageViewLayoutId); convertView.setTag(handler); } else { handler = (Handler) convertView.getTag(); } handler.imageView.setImageResource(...); return convertView; Where yourLayoutResourceId is the id of the created xml-layout file (R.layout.exmaple), and imageViewLayoutId is the id of your imaeView in the layout (R.id.exmapleIV). As last step define an inner class Handler in your ImageAdapter: class Handler { ImageView imageView; } 

Handler handler; if (convertView == null) { LayoutInflater li = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = li.inflate(yourLayoutResourceId, parent, false); handler = new Handler(); handler.itemIV = (ImageView) convertView.findViewById(imageViewLayoutId); convertView.setTag(handler); } else { handler = (Handler) convertView.getTag(); } handler.imageView.setImageResource(...); return convertView; Where yourLayoutResourceId is the id of the created xml-layout file (R.layout.exmaple), and imageViewLayoutId is the id of your imaeView in the layout (R.id.exmapleIV). As last step define an inner class Handler in your ImageAdapter: class Handler { ImageView imageView; }

Handler handler; if (convertView == null) { LayoutInflater li = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = li.inflate(yourLayoutResourceId, parent, false); handler = new Handler(); handler.itemIV = (ImageView) convertView.findViewById(imageViewLayoutId); convertView.setTag(handler); } else { handler = (Handler) convertView.getTag(); } handler.imageView.setImageResource(...); return convertView; Where yourLayoutResourceId is the id of the created xml-layout file (R.layout.exmaple), and imageViewLayoutId is the id of your imaeView in the layout (R.id.exmapleIV). As last step define an inner class Handler in your ImageAdapter: class Handler { ImageView imageView; }

Good luck! 祝好运! Have nice development. 有良好的发展。

Kr r

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

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