To update image in ImageView which method is better setImageResource() or setImageDrawable()

huangapple go评论70阅读模式
英文:

To update image in ImageView which method is better setImageResource() or setImageDrawable()

问题

以下是您要翻译的内容:

我是Android新手,在学习ImageView小部件时,我学到了两种可能的方法来更新ImageView中的图像。

这是教程建议的方法:

onButtonClick(View view) {
    ImageView myImageView = (ImageView) findViewById(R.id.imageView);
    myImageView.setImageResource(R.drawable.cn);
}

另一种是我自己想出来的:

onButtonClick(View view) {
    ImageView myImageView = (ImageView) findViewById(R.id.imageView);
    newDrawable = getResources().getDrawable(R.drawable.cn);
    myImageView.setImageDrawable(null);
    myImageView.setImageDrawable(newDrawable);
}

我想更好地理解一些事情:

  • 在我这种情况下,推荐使用哪种方法?
  • 使用setImageResource()与setImageDrawable()各有什么优缺点?
英文:

I am Android Newbie, while learning ImageView widgets I learned 2 possible methods to update image in ImageView.

This is the way the Instructor suggested:

onButtonClick(View view) {
    ImageView myImageView = (ImageView) findViewById(R.id.imageView);
    myImageView.setImageResource(R.drawable.cn);
}

The other one is something I came up myself:

onButtonClick(View view) {
    ImageView myImageView = (ImageView) findViewById(R.id.imageView);
    newDrawable = getResources().getDrawable(R.drawable.cn);
    myImageView.setImageDrawable(null);
    myImageView.setImageDrawable(newDrawable);
}

I would like to understand a few things better:

  • Which method is recommended in my case?
  • What are pros/cons of using setImageResource() vs setImageDrawable()

答案1

得分: 1

在你的情况下,setimageResource会更好,因为你只需要设置图片。

getResources().getDrawable(R.drawable.cn);

如果你需要对drawable进行更改,比如颜色过滤、调整大小等,这将是值得的。

英文:

In your case, setimageResource will be better because you just need to set the image.

getResources().getDrawable(R.drawable.cn);

It will be worthwhile in case you need to do changes in drawable, like color filtering, resizing, etc.

huangapple
  • 本文由 发表于 2020年10月27日 16:04:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64550292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定