安卓開發實現畫廊效果

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   sp   

畫廊 滑動顯示多個圖片

 

layout

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6  7  8  9     <Gallery  10         android:id="@+id/gallery"  11         android:spacing="5px"  12         android:unselectedAlpha="0.5"  13         android:layout_width="match_parent"  14         android:layout_height="wrap_content" />15 16    17 </LinearLayout>


Activity

 1 package tea_xqx; 2  3 import com.example.tea.R; 4  5 import android.app.Activity; 6 import android.content.res.TypedArray; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.view.ViewGroup;10 import android.widget.AdapterView;11 import android.widget.AdapterView.OnItemClickListener;12 import android.widget.BaseAdapter;13 import android.widget.Gallery;14 import android.widget.ImageView;15 import android.widget.TextView;16 import android.widget.Toast;17 18 public class MenuTeaTools extends Activity{19     //設定畫廊圖片20     private int[] imageId = new int[] { R.drawable.menu_tea_lvcha,R.drawable.menu_tea_hongcha,R.drawable.menu_tea_baicha,R.drawable.menu_tea_heicha,R.drawable.menu_tea_huangcha,R.drawable.menu_tea_wulongcha};  21     @Override22     protected void onCreate(Bundle savedInstanceState) {23         // TODO Auto-generated method stub24         super.onCreate(savedInstanceState);25         setContentView(R.layout.menu_tools);26         27         Gallery gallery = (Gallery) findViewById(R.id.gallery);28          BaseAdapter adapter = new BaseAdapter() {  29                 @Override  30                 public View getView(int position, View convertView, ViewGroup parent) {  31                     ImageView imageview;  32                     if (convertView == null) {  33                         imageview = new ImageView(MenuTeaTools.this);  34                         imageview.setScaleType(ImageView.ScaleType.FIT_XY);  35                         imageview.setLayoutParams(new Gallery.LayoutParams(500, 400));  36                         TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);  37                         imageview.setBackgroundResource(typedArray.getResourceId(  38                                 R.styleable.Gallery_android_galleryItemBackground,  39                                 0));  40                         imageview.setPadding(5, 0, 5, 0);  41                     } 42                     else 43                     {  44                         imageview = (ImageView) convertView;  45                     }  46                     imageview.setImageResource(imageId[position]);  47                     return imageview;  48                 }  49       50                 @Override  51                 public long getItemId(int position) {  52                     return position;  53                 }  54       55                 @Override  56                 public Object getItem(int position) {  57                     return position;  58                 }  59       60                 @Override  61                 public int getCount() {  62                     return imageId.length;  63                 }  64             };  65             gallery.setAdapter(adapter);  66             gallery.setSelection(imageId.length / 2); //預設顯示的圖片的id  67             //畫廊圖片的點擊事件68             gallery.setOnItemClickListener(new OnItemClickListener() {  69                 @Override  70                 public void onItemClick(AdapterView<?> parent, View view,  71                         int position, long id) {72                 73                     Toast.makeText(MenuTeaTools.this,  74                             "第" + String.valueOf(position+1) + "張圖片被選中",  75                             Toast.LENGTH_SHORT).show();  76                 }  77             });  78         }   79 80     81 82 }


最後在res/values/string.xml中添加一段代碼

1 <declare-styleable name="Gallery">  2         <attr name="android:galleryItemBackground" />  3     </declare-styleable>

 

這樣便完成了一個畫廊的效果

安卓開發實現畫廊效果

聯繫我們

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