Search this blog

How to access image resources programatically android

Images resources are encapsulated in the class BitmapDrawable. To access a graphic resource file called /res/drawable/logo.png, you would use the getDrawable() method, as follows:

BitmapDrawable logoBitmap =
(BitmapDrawable)getResources().getDrawable(R.drawable.logo);

Most of the time, however, you don’t need to load a graphic directly. Instead, you
can use the resource identifier as an attribute on a control such as an ImageView
control. The following code, for example, sets and loads the logo.png graphic into
an ImageView control named LogoImageView, which must be defined within the
layout:

ImageView logoView = (ImageView)findViewById(R.id.LogoImageView);
logoView.setImageResource(R.drawable.logo);

0 comments:

Post a Comment