Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android Gallery 選中放大效果實作

多數 Android 入門教學在講 Gallery 時,圖片一律同樣大小,看起來有點呆板。其實只要多幾行互動,就能做出「選中放大」的效果,讓瀏覽起來更有層次。

原理非常單純:在自訂 BaseAdapter 的 getView() 裡判斷目前位置是否等於選中項,選中的給大一點的 LayoutParams,其餘給小一點的,再搭配一個縮放動畫即可。

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;
    }

    public int getCount() {
        return Integer.MAX_VALUE;  // 讓圖片可以無限循環瀏覽
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public void setSelectItem(int selectItem) {
        if (this.selectItem != selectItem) {
            this.selectItem = selectItem;
            notifyDataSetChanged();
        }
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        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.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;

public class ContactsActivity extends Activity implements OnItemSelectedListener {
    private galleryAdapter adapter;
    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);  // 滑動時觸發
    }
    public void onNothingSelected(AdapterView<?> arg0) {
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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>

最後別忘了 res/anim 底下那份 my_scale_action.xml 縮放動畫定義檔,它才是放大效果真正的主體。核心觀念就一句:getCount 回傳一個極大值可以換來無限循環,而 getView 中的尺寸差異加上動畫,就構成了「選中放大」的視覺回饋。

Copyright © Linux教程網 All Rights Reserved