英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论