android之ImageView

來源:互聯網
上載者:User

ImageView

首先確定下ImageView的全路徑:android.widget.ImageView.

描述: 

顯示任意映像,例標。ImageView類可以載入各種來源的圖片(如資源或圖片庫),需要計算映像的尺寸,比便它可以在其他布局中使用,並提供例如縮放和著色(渲染)各種顯示選項。

android:adjustViewBounds
 是否保持寬高比。需要與maxWidth、MaxHeight一起使用,否則單獨使用沒有效果。
 
android:cropToPadding
 是否截取指定地區用空白代替。單獨設定無效果,需要與scrollY一起使用,效果如下,實現代碼見代碼部分:

 

android:maxHeight
 設定View的最大高度,單獨使用無效,需要與setAdjustViewBounds一起使用。如果想設定圖片固定大小,又想保持圖片寬高比,需要如下設定:

1) 設定setAdjustViewBounds為true;

2) 設定maxWidth、MaxHeight;

3) 設定設定layout_width和layout_height為wrap_content。
 
android:maxWidth
 設定View的最大寬度。同上。
 
android:scaleType
 設定圖片的填充方式。

android:src
 設定View的drawable(片,也可以是顏色,但是需要指定View的大小)
 
android:tint
 將圖片渲染成指定的顏色。

 

程式碼範例:

點擊圖片時切換圖片並且將另一個圖片變暗(主要是對圖片透明度的利用)

package cn.com.chenzheng_java;</p><p>import android.app.Activity;<br />import android.content.res.Resources;<br />import android.graphics.drawable.Drawable;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.widget.Button;<br />import android.widget.ImageView;<br />import android.widget.ImageView.ScaleType;<br />/**<br /> *<br /> * @author chenzheng_java<br /> *<br /> */<br />public class ImageViewActivity extends Activity {</p><p>private ImageView image1;<br />private ImageView image2;<br />@Override<br />protected void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.imageview);</p><p>image1 = (ImageView) findViewById(R.id.imge1);<br />image2 = (ImageView) findViewById(R.id.imge2);<br />// resetButton = (Button)findViewById(R.id.resetButton);</p><p>final int [] ids = new int[]{<br />R.drawable.red_a,<br />R.drawable.blackk,<br />R.drawable.beauty<br />};</p><p>image1.setOnClickListener(new View.OnClickListener() {<br />@Override<br />public void onClick(View v) {<br />int id = ids[0];<br /> image1.setScaleType(ScaleType.CENTER);// 設定剪輯模式</p><p>// 設定imageView的背景,如果已經有圖片存在的話,則背景會被遮住<br />// image1.setBackgroundResource(id);</p><p>// 設定ImageView上顯示的圖片<br /> image1.setImageResource(id);</p><p> image2.setImageResource(R.drawable.icon);<br /> image2.setAlpha(100);// 設定透明度,255時為無透明度</p><p>}<br />});</p><p>}</p><p>}<br />

-----------------------------------------------------------

如何為ImageView設定圖片呢,主要方法有:

三種設定圖片源的方法
ImageView iv;

String fileName = "/data/data/com.test/aa.png;
Bitmap bm = BitmapFactory.decodeFile(fileName);
iv.setImageBitmap(bm);

ImageView iv = new ImageView(context);
iv.setImageResource(iv[position]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(136,88));

mImageView = (ImageView)this.findViewById(R.id.myImageView1);
mImageView.setImageDrawable(getResources().getDrawable(R.drawable.right));

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.