Android深入淺出系列之執行個體應用—簡單的手指拖動圖片,圖片滑來滑去顯示應用Gallery和BaseAdapter以及ImageView的使用

來源:互聯網
上載者:User

  前言
  我們現在在隨便一個手機上用手指在螢幕上滑來滑都可以去拖動圖片,其實在Android裡這很簡單,下面我就給大傢具體講解一下。

  思路
   我們首先需要Gallery這個對象,俗稱畫廊對象,大家都知道畫廊吧,在現實生活中畫廊裡面放置的都是一個個畫家畫的具體的畫,畫廊有了,還需要什麼呢?還需要的就是一幅幅具體的畫了,具體的畫其實就是ImageView對象了。如何把畫好的畫填充到畫廊上呢?這裡就需要一個填充器了,就是BaseAdapter。

  實現步驟

  一:布局檔案編寫

  1.1:布局檔案main.xml

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" android:layout_width="fill_parent"
     android:layout_height="fill_parent">
   <Gallery  
      android:id="@+id/gallery" 
        android:layout_width="fill_parent" 
      android:layout_height="fill_parent"
   />
  </LinearLayout>

  二:代碼檔案編寫

  2. 1:MainActivity.java

  package com.menglin.gallery;

  import android.app.Activity;
  import android.content.Context;
  import android.os.Bundle;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.Gallery;
  import android.widget.ImageView;

  public class MainActivity extends Activity
  {
     @Override
     public void onCreate(Bundle savedInstanceState)
     {
        super.onCreate(savedInstanceState);
        //載入布局檔案main.xml
        setContentView(R.layout.main);
        //通過findViewById()方法得到Gallery對象
        Gallery gallery = (Gallery)findViewById(R.id.gallery);  
        //添加一個ImageAdapter並設定給Gallery
        gallery.setAdapter(new ImageAdapter(this));    
     }
 
     public class ImageAdapter extends BaseAdapter
     {
        private Context context;
        //使用系統的表徵圖圖片作為圖庫源
        private int[] imageids=
        {
           android.R.drawable.btn_minus,
           android.R.drawable.btn_radio,
           android.R.drawable.ic_lock_idle_low_battery,
           android.R.drawable.btn_radio,
           android.R.drawable.btn_dialog
        };

        //建構函式 此建構函式只有一個參數就是要存數的Context
        public ImageAdapter(Context c)
        {   
           this.context = c;
        }
  
        //得到已定義的圖片的總數量
        public int getCount()
        {
           return imageids.length;
        }

        //得到目前容器中圖片的數組
        public Object getItem(int position)
        {
           return position;
        }

        //得到目前容器中圖片的數組ID
        public long getItemId(int position)
        {   
           return position;
        }

        //取得目前欲顯示的圖片view,傳入數組ID使之讀取成映像
        public View getView(int position, View convertView, ViewGroup parent)
        {
           //建立一個ImageView對象
           ImageView imageview = new ImageView(context);
           //設定圖片給ImageView對象
           imageview.setImageResource(imageids[position]);
           //重新設定圖片的寬高
           imageview.setScaleType(ImageView.ScaleType.FIT_XY);
           //重新設定Layout的寬高
           imageview.setLayoutParams(new Gallery.LayoutParams(120, 120));
           return imageview;
        }
  
      //根據距離中央的位移量,利用getScale()返回view的大小

      public float getScale(boolean focused,int offset)
       {   
          return Math.max(0, 1.0f/(float)Math.pow(2, Math.abs(offset)));
       }  
     }
  }

  運行效果如下 

  我們用手指從右向左拖動

  

  

  

  

 

相關文章

聯繫我們

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