`
hualikejava
  • 浏览: 169731 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

gallery游览图片点击图片放大

 
阅读更多
之前听说gallery做图片浏览很不错。最近也听到一朋友要做这个,听他说要改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;
    	}
		@Override
		public int getCount() {
			return data.length;
		}
		@Override
		public Object getItem(int position) {
			return data[position];
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
		@Override
		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;
		}
    }
}


布局文件

<?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_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center_horizontal"
    android:spacing="10dip"
  	android:layout_centerHorizontal="true" 
    
    
    />
</LinearLayout>
  • 大小: 9.4 KB
  • 大小: 9.8 KB
分享到:
评论
2 楼 flyingsir_zw 2012-03-17  
我这样修改了一下。

int i = 10;
private OnItemClickListener listener = new  OnItemClickListener(){  
        @Override  
        public void onItemClick(AdapterView<?> parent, View view, int position,  
                long id) {  
            if(position!=i){
                   i = position;
               manimationSet.setFillAfter(false); 
                }  
                     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);  
                }  
        }  
    }; 
1 楼 flyingsir_zw 2012-03-17  
position!=0&&position!=4
这样的话,图片位置是0 或者4 时候,点击就没有反应了,
尝试修改了一下
!(position<0) && position!=4  
结果,点击第一张可以放大了,但是点击空白的位置,图片不会回复原来状态了。


可以帮忙说下原因不。
flyingsir.zw@gmail.com   谢谢。

相关推荐

Global site tag (gtag.js) - Google Analytics