天天看点

android-gallery游览图片点击图片放大

package com.hua.com;  

import android.app.activity;  

import android.content.context;  

import android.os.bundle;  

import android.view.view;  

import android.view.viewgroup;  

import android.view.animation.animation;  

import android.view.animation.animationset;  

import android.view.animation.scaleanimation;  

import android.widget.adapterview;  

import android.widget.adapterview.onitemclicklistener;  

import android.widget.adapterview.onitemselectedlistener;  

import android.widget.baseadapter;  

import android.widget.gallery;  

import android.widget.imageview;  

public class testgalleryactivity extends activity {  

    private gallery gallery;  

    private animationset manimationset;  

    private int[] imgs;  

    @override  

    public void oncreate(bundle savedinstancestate) {  

        super.oncreate(savedinstancestate);  

        setcontentview(r.layout.main);  

        init();  

    }  

    private void init(){  

        imgs=new int[]{r.drawable.a,r.drawable.c,r.drawable.d,r.drawable.y,r.drawable.f};  

        gallery = (gallery)findviewbyid(r.id.gy_main);  

        imageadapter imgadapter = new imageadapter(this,imgs);  

        gallery.setadapter(imgadapter);  

        gallery.setselection(imgs.length/2);  

        gallery.setonitemclicklistener(listener);  

    private onitemclicklistener listener = new  onitemclicklistener(){  

        @override  

        public void onitemclick(adapterview<?> parent, view view, int position,  

                long id) {  

            if(position!=0&&position!=4){  

                animationset animationset = new animationset(true);  

                if(manimationset!=null&&manimationset!=animationset){  

                     scaleanimation scaleanimation = new scaleanimation(2,0.5f,2,0.5f,  

                             animation.relative_to_self,0.5f,   //使用动画播放图片     

                              animation.relative_to_self,0.5f);  

                            scaleanimation.setduration(1000);  

                            manimationset.addanimation(scaleanimation);  

                            manimationset.setfillafter(true); //让其保持动画结束时的状态。     

                           view.startanimation(manimationset);  

                }  

                     scaleanimation scaleanimation = new scaleanimation(1,2f,1,2f,  

                             animation.relative_to_self,0.5f,   

                             animation.relative_to_self,0.5f);  

                           scaleanimation.setduration(1000);  

                           animationset.addanimation(scaleanimation);  

                           animationset.setfillafter(true);   

                           view.startanimation(animationset);  

                           manimationset = animationset;  

                }else{  

                    if(null!=manimationset)manimationset.setfillafter(false);  

        }  

    };  

    class imageadapter extends baseadapter{  

        private context ct;  

        private int[] data;  

        public imageadapter(context ct,int[] data){  

            this.ct=ct;  

            this.data=data;  

        public int getcount() {  

            return data.length;  

        public object getitem(int position) {  

            return data[position];  

        public long getitemid(int position) {  

            return position;  

        public view getview(int position, view convertview, viewgroup parent) {  

            view view = null;  

            if(convertview==null){  

                imageview img = new imageview(ct);  

                img.setimageresource(data[position]);  

                view=img;  

            }else{  

                view=convertview;  

            }  

            return view;  

}

view plainprint?

<?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"  

    >  

<textview    

    android:layout_width="fill_parent"   

    android:layout_height="wrap_content"   

    android:text="@string/hello"  

    />  

    <gallery  

    android:id="@+id/gy_main"  

    android:layout_height="fill_parent"   

    android:layout_gravity="center_horizontal"  

    android:spacing="10dip"  

    android:layout_centerhorizontal="true"   

</linearlayout> 

继续阅读