最近也在一點點學習,還是老樣子,把新學的知識總結一下,方便以後參考用。
現在大多Android入門教程中,都給大家教了gallery的基本用法,浏覽圖片時大小一樣,比較死板。咱們這裡稍微加一點點效果:選中放大。
其實也非常簡單,就是在適配器中public View getView(int position, View convertView, ViewGroup parent) {}這個抽象方法中做相應處理即可:選中的設置大一點,未選中的設置小一點!
效果實現如下:
閒話少說,貼代碼:
galleryAdapter.java
- package com.contacts;
-
- import android.content.Context;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.BaseAdapter;
- import android.widget.Gallery;
- import android.widget.ImageView;
-
- public class galleryAdapter extends BaseAdapter{
- Context mContext;
- private int selectItem;
- private int drawable1[]=new int[] {R.drawable.center,R.drawable.left,R.drawable.right};
- public galleryAdapter(Context mContext){
- this.mContext=mContext;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return Integer.MAX_VALUE; //這裡的目的是可以讓圖片循環浏覽
- }
-
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return position;
- }
-
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- public void setSelectItem(int selectItem) {
-
- if (this.selectItem != selectItem) {
- this.selectItem = selectItem;
- notifyDataSetChanged();
- }
- }
-
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- ImageView imageView=new ImageView(mContext);
- imageView.setImageResource(drawable1[position%drawable1.length]);
- //取余,讓圖片循環浏覽
-
- if(selectItem==position){
- Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.my_scale_action); //實現動畫效果
- imageView.setLayoutParams(new Gallery.LayoutParams(105,120));
- imageView.startAnimation(animation); //選中時,這是設置的比較大
- }
- else{
- imageView.setLayoutParams(new Gallery.LayoutParams(75,90));
- //未選中
- }
- return imageView;
- }
-
- }
ContactsActivity.java
- package com.contacts;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.Gallery;
- import android.widget.ImageSwitcher;
- import android.widget.ImageView;
- import android.widget.LinearLayout.LayoutParams;
-
- public class ContactsActivity extends Activity implements OnItemSelectedListener {
- /** Called when the activity is first created. */
- private galleryAdapter adapter;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Gallery gallery=(Gallery)findViewById(R.id.gallery);
- adapter=new galleryAdapter(this);
- gallery.setAdapter(adapter);
- gallery.setSpacing(5);
- gallery.setOnItemSelectedListener(this);
- }
- public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
-
- adapter.setSelectItem(position); //當滑動時,事件響應,調用適配器中的這個方法。
-
-
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {//抽象方法,必須實現
- // TODO Auto-generated method stub
-
- }
-
- }
Main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
-
- <Gallery
- android:id="@+id/gallery"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="30dp"
- />
- </LinearLayout>
還有個動畫的配置文件,這裡就不貼了,感興趣的可以下載全部工程。
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2012年資料/4月/30日/Android Gallery實現選中放大的效果/