Android UI之ImageView圖片視圖

來源:互聯網
上載者:User

ImageView是一個顯示圖片的組件,用一個例子介紹該組件的簡單運用:

在樣式檔案中:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <!-- android:src設定ImageView所顯示的Drawable對象的ID --> 
    <ImageView 
        android:id="@+id/img1" 
        android:layout_width="fill_parent" 
        android:layout_height="300dp" 
        android:background="#cccccc" 
        android:src="@drawable/pig" /> 
    <!--android:scaleType設定所顯示的圖片如何縮放或移動以適應ImageView的大小  其值在中文API上有詳細的說明 --> 
    <ImageView 
        android:id="@+id/img2" 
        android:layout_width="100dp" 
        android:layout_height="100dp" 
        android:background="#cccccc" 
        android:scaleType="fitStart" 
        android:layout_marginTop="20dp" 
        /> 
 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <!-- android:src設定ImageView所顯示的Drawable對象的ID -->
    <ImageView
        android:id="@+id/img1"
        android:layout_width="fill_parent"
        android:layout_height="300dp"
        android:background="#cccccc"
        android:src="@drawable/pig" />
    <!--android:scaleType設定所顯示的圖片如何縮放或移動以適應ImageView的大小  其值在中文API上有詳細的說明 -->
    <ImageView
        android:id="@+id/img2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#cccccc"
        android:scaleType="fitStart"
        android:layout_marginTop="20dp"
        />

</LinearLayout>

該樣式檔案中有兩個ImageView組件,第一個用來顯示我們想要展示的圖片,第二ImageView用來顯示當點擊圖片上某一位置時在該組件上顯示部分圖片,代碼實現該功能:


package cn.class3g.activity; 
 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.drawable.BitmapDrawable; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.ImageView; 
 
public class ImageViewDemo extends Activity implements OnTouchListener { 
    ImageView imageView1, imageView2; 
 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        this.setContentView(R.layout.imageview_layout); 
        findViews(); 
    } 
 
    private void findViews() { 
        imageView1 = (ImageView) findViewById(R.id.img1); 
        imageView2 = (ImageView) findViewById(R.id.img2); 
        //為imageView添加觸摸監聽事件 
        imageView1.setOnTouchListener(this); 
    } 
 
    public boolean onTouch(View v, MotionEvent event) { 
        float scale = 412 / 320; 
<span style="white-space:pre">        </span>//擷取需要顯示的圖片的開始點 
        int x = (int) (event.getX() * scale); 
        int y = (int) (event.getY() * scale); 
         
        //需要考慮邊界問題 
        int width = (int) (100 * scale); 
        int height = (int) (100 * scale); 
        //擷取圖片顯示框中的位元影像 
        BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView1.getDrawable(); 
        //顯示圖片指定的地區 
        imageView2.setImageBitmap(Bitmap.createBitmap(bitmapDrawable.getBitmap(), 
                x,y, width, height)); 
         
        return false; 
    } 
} 
package cn.class3g.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class ImageViewDemo extends Activity implements OnTouchListener {
 ImageView imageView1, imageView2;

 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.setContentView(R.layout.imageview_layout);
  findViews();
 }

 private void findViews() {
  imageView1 = (ImageView) findViewById(R.id.img1);
  imageView2 = (ImageView) findViewById(R.id.img2);
  //為imageView添加觸摸監聽事件
  imageView1.setOnTouchListener(this);
 }

 public boolean onTouch(View v, MotionEvent event) {
  float scale = 412 / 320;
<span style="white-space:pre">  </span>//擷取需要顯示的圖片的開始點
  int x = (int) (event.getX() * scale);
  int y = (int) (event.getY() * scale);
  
  //需要考慮邊界問題
  int width = (int) (100 * scale);
  int height = (int) (100 * scale);
  //擷取圖片顯示框中的位元影像
  BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView1.getDrawable();
  //顯示圖片指定的地區
  imageView2.setImageBitmap(Bitmap.createBitmap(bitmapDrawable.getBitmap(),
    x,y, width, height));
  
  return false;
 }
}

在模擬器上的效果為

 

作者 jjaze3344

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.