Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 選中GridView 中 的item 時進行圖片放大的操作

android 選中GridView 中 的item 時進行圖片放大的操作

編輯:關於Android編程

1、在GridView 中我們會遇到當選中某個Item 時想讓它處於高亮或者是放大的操作,我們只需要在setOnItemClickListener() 方法中把相應的position 給傳過去。

package com.example.sgridview;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class MainActivity extends Activity {
    private GridView gridView;
    private MyAdapter adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lists.add(R.drawable.ic_launcher);
        lists.add(R.drawable.ic_launcher);
        lists.add(R.drawable.ic_launcher);
        this.gridView = (GridView) this.findViewById(R.id.gridView);
        adapter = new MyAdapter();
        this.gridView.setAdapter(adapter);

        this.gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView arg0, View arg1, int id, long arg3) {
                // TODO Auto-generated method stub
                MyAdapter ad = (MyAdapter) arg0.getAdapter();
                ad.setNotifyDataChange(id);
            }
        });
    }

    private List lists = new ArrayList();

    private class MyAdapter extends BaseAdapter {
        private int selectPic = -1;

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return lists.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return lists.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public void setNotifyDataChange(int id) {
            selectPic = id;
            super.notifyDataSetChanged();
        }

        @SuppressLint("NewApi")
        @Override
        public View getView(int position, View view, ViewGroup arg2) {
            ImageView img = new ImageView(MainActivity.this);
            img.setBackgroundResource(lists.get(position));
            if (selectPic == position) {

                Animation testAnim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim);

                img.startAnimation(testAnim);

            } else {
                // the rest
                img.setScaleType(ScaleType.CENTER_INSIDE);
            }

            return img;
        }
    }

}

2、anim.xml 文件



    



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved