.
This tutorial adapted from http://android-coding.blogspot.my/2013/12/setimagedrawable-vs-setimageresource-vs.html 
1) Create a New Empty Android Project.
2) Edit Layout File
activity_main.xml
<ScrollView 
   xmlns:android="http://schemas.android.com/apk/res/android" 
   android:id="@+id/scroll" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content"> 
<LinearLayout 
   android:layout_width="match_parent" 
   android:layout_height="match_parent" 
   android:orientation="vertical" 
   android:scrollbars="vertical" 
   > 
   <!-- 
       android:src 
           Sets a drawable as the content of this ImageView. 
           May be a reference to another resource, in the form "@[+][package:]type:name" 
           or to a theme attribute in the form "?[package:][type:]name". 
           May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb". 
           This corresponds to the global attribute resource symbol src. 
   --> 
   <!-- Set ImageView image by XML--> 
   <ImageView 
       android:id="@+id/iv" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/android" 
       android:padding="3dp" 
       /> 
   <!-- Set ImageView image programmatically--> 
   <ImageView 
       android:id="@+id/iv2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:padding="3dp" 
       /> 
   <!-- Set ImageView image as drawable programmatically--> 
   <ImageView 
       android:id="@+id/iv3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:padding="3dp" 
       /> 
   <!-- Set ImageView image as bitmap programmatically--> 
   <ImageView 
       android:id="@+id/iv4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:padding="3dp" 
       /> 
</LinearLayout> 
   </ScrollView> 
 | 
3) Edit Controller File
MainActivity.java
package com.notarazi.myimageview1; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.widget.ImageView; 
public class MainActivity extends Activity { 
   @Override 
   protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_main); 
       // Get the widgets reference from XML layout 
       ImageView iv = (ImageView) findViewById(R.id.iv); 
       ImageView iv2 = (ImageView) findViewById(R.id.iv2); 
       ImageView iv3 = (ImageView) findViewById(R.id.iv3); 
       ImageView iv4 = (ImageView) findViewById(R.id.iv4); 
       /* 
           setImageResource (int resId) 
               Sets a drawable as the content of this ImageView. 
               Parameters 
                   resId : the resource identifier of the drawable 
               This does Bitmap reading and decoding on the UI thread, which can cause 
               a latency hiccup. If that's a concern, consider using 
               setImageDrawable(android.graphics.drawable.Drawable) or 
               setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead. 
        */ 
       // Set an image for second ImageView 
       iv2.setImageResource(R.drawable.android); 
       // Set image as drawable for third ImageView 
       iv3.setImageDrawable(getResources().getDrawable(R.drawable.android)); 
       /* 
            Bitmap decodeResource (Resources res, int id) 
               Synonym for decodeResource(Resources, int, android.graphics.BitmapFactory.Options) with null Options. 
               Parameters 
                   res : The resources object containing the image data 
                   id : The resource id of the image data 
               Returns 
                   The decoded bitmap, or null if the image could not be decoded. 
        */ 
       Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.android); 
       // Set image as bitmap for fourth ImageView 
       iv4.setImageBitmap(bitmap); 
   } 
} 
 | 
4) Outcome.
5) Further Reading
1) src=Sets a drawable as the content of this ImageView. Refer http://developer.android.com/reference/android/widget/ImageView.html#attr_android:src 
2) setImageResource = Sets a drawable as the content of this ImageView. (This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup) Referhttp://developer.android.com/reference/android/widget/ImageView.html#setImageResource(int) 
3) setImageDrawable = Sets a drawable as the content of this ImageView. Refer http://developer.android.com/reference/android/widget/ImageView.html#setImageDrawable(android.graphics.drawable.Drawable) 
4) setBitmapDrawable = Sets a Bitmap as the content of this ImageView. Referhttp://developer.android.com/reference/android/widget/ImageView.html#setImageBitmap(android.graphics.Bitmap) 
Congratulations guys, quality information you have given!!!..Its really useful blog. Thanks for sharing this useful information..Android Training institute in chennai with placement | Android Training in chennai |Android Training in Velachery | android development course fees in chennai
ReplyDeleteThis information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..Android Training in chennai | Android Training|Android Training in chennai with placement | Android Training in velachery
ReplyDelete