Open “res/layout/main. xml” file, just add an ImageView and Button for demonstration. By default, imageView1 will display “android. png”.
Bitmap bm=((BitmapDrawable)imageView. getDrawable()). getBitmap();
src: src is an attribute used to set a source file or you can say image in your imageview to make your layout attractive.
If you created imageview using xml file then follow the steps.
Solution 1:
Step 1: Create an XML file
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#cc8181"
>
<ImageView
android:id="@+id/image"
android:layout_width="50dip"
android:layout_height="fill_parent"
android:src="@drawable/icon"
android:layout_marginLeft="3dip"
android:scaleType="center"/>
</LinearLayout>
Step 2: create an Activity
ImageView img= (ImageView) findViewById(R.id.image);
img.setImageResource(R.drawable.my_image);
Solution 2:
If you created imageview from Java Class
ImageView img = new ImageView(this);
img.setImageResource(R.drawable.my_image);
Have a look at the ImageView API. There are several setImage*
methods. Which one to use depends on the image you provide. If you have the image as resource (e.g. file res/drawable/my_image.png)
ImageView img = new ImageView(this); // or (ImageView) findViewById(R.id.myImageView);
img.setImageResource(R.drawable.my_image);
Just to go a little bit further in the matter, you can also set a bitmap directly, like this:
ImageView imageView = new ImageView(this);
Bitmap bImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.my_image);
imageView.setImageBitmap(bImage);
Of course, this technique is only useful if you need to change the image.
if (android.os.Build.VERSION.SDK_INT >= 21) {
storeViewHolder.storeNameTextView.setImageDrawable(context.getResources().getDrawable(array[position], context.getTheme()));
} else {
storeViewHolder.storeNameTextView.setImageDrawable(context.getResources().getDrawable(array[position]));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With