阅读705 返回首页    go 阿里云 go 技术社区[云栖]


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

  1. package com.hua.com;     
  2. import android.app.Activity;     
  3. import android.content.Context;     
  4. import android.os.Bundle;     
  5. import android.view.View;     
  6. import android.view.ViewGroup;     
  7. import android.view.animation.Animation;     
  8. import android.view.animation.AnimationSet;     
  9. import android.view.animation.ScaleAnimation;     
  10. import android.widget.AdapterView;     
  11. import android.widget.AdapterView.OnItemClickListener;     
  12. import android.widget.AdapterView.OnItemSelectedListener;     
  13. import android.widget.BaseAdapter;     
  14. import android.widget.Gallery;     
  15. import android.widget.ImageView;     
  16. public class TestGalleryActivity extends Activity {     
  17.     private Gallery gallery;     
  18.     private AnimationSet manimationSet;     
  19.     private int[] imgs;     
  20.     @Override     
  21.     public void onCreate(Bundle savedInstanceState) {     
  22.         super.onCreate(savedInstanceState);     
  23.         setContentView(R.layout.main);     
  24.         init();     
  25.     }     
  26.     private void init(){     
  27.         imgs=new int[]{R.drawable.a,R.drawable.c,R.drawable.d,R.drawable.y,R.drawable.f};     
  28.         gallery = (Gallery)findViewById(R.id.gy_main);     
  29.         ImageAdapter imgAdapter = new ImageAdapter(this,imgs);     
  30.         gallery.setAdapter(imgAdapter);     
  31.         gallery.setSelection(imgs.length/2);     
  32.         gallery.setOnItemClickListener(listener);     
  33.              
  34.     }     
  35.     private OnItemClickListener listener = new  OnItemClickListener(){     
  36.         @Override     
  37.         public void onItemClick(AdapterView<?> parent, View view, int position,     
  38.                 long id) {     
  39.             if(position!=0&&position!=4){     
  40.                 AnimationSet animationSet = new AnimationSet(true);     
  41.                 if(manimationSet!=null&&manimationSet!=animationSet){     
  42.                      ScaleAnimation scaleAnimation = new ScaleAnimation(2,0.5f,2,0.5f,     
  43.                              Animation.RELATIVE_TO_SELF,0.5f,   //使用动画播放图片     
  44.                               Animation.RELATIVE_TO_SELF,0.5f);     
  45.                             scaleAnimation.setDuration(1000);     
  46.                             manimationSet.addAnimation(scaleAnimation);     
  47.                             manimationSet.setFillAfter(true); //让其保持动画结束时的状态。     
  48.                            view.startAnimation(manimationSet);     
  49.                 }     
  50.                      ScaleAnimation scaleAnimation = new ScaleAnimation(1,2f,1,2f,     
  51.                              Animation.RELATIVE_TO_SELF,0.5f,      
  52.                              Animation.RELATIVE_TO_SELF,0.5f);     
  53.                            scaleAnimation.setDuration(1000);     
  54.                            animationSet.addAnimation(scaleAnimation);     
  55.                            animationSet.setFillAfter(true);      
  56.                            view.startAnimation(animationSet);     
  57.                            manimationSet = animationSet;     
  58.                      
  59.                 }else{     
  60.                     if(null!=manimationSet)manimationSet.setFillAfter(false);     
  61.                 }     
  62.         }     
  63.     };     
  64.     class ImageAdapter extends BaseAdapter{     
  65.         private Context ct;     
  66.         private int[] data;     
  67.         public ImageAdapter(Context ct,int[] data){     
  68.             this.ct=ct;     
  69.             this.data=data;     
  70.         }     
  71.         @Override     
  72.         public int getCount() {     
  73.             return data.length;     
  74.         }     
  75.         @Override     
  76.         public Object getItem(int position) {     
  77.             return data[position];     
  78.         }     
  79.         @Override     
  80.         public long getItemId(int position) {     
  81.             return position;     
  82.         }     
  83.         @Override     
  84.         public View getView(int position, View convertView, ViewGroup parent) {     
  85.             View view = null;     
  86.             if(convertView==null){     
  87.                 ImageView img = new ImageView(ct);     
  88.                 img.setImageResource(data[position]);     
  89.                 view=img;     
  90.             }else{     
  91.                 view=convertView;     
  92.             }     
  93.             return view;     
  94.         }     
  95.     }     
  96. }     


  1. view plainprint?   
  2. <?xml version="1.0" encoding="utf-8"?>     
  3. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"     
  4.     android:orientation="vertical"     
  5.     android:layout_width="fill_parent"     
  6.     android:layout_height="fill_parent"     
  7.     >     
  8. <TextView       
  9.     android:layout_width="fill_parent"      
  10.     android:layout_height="wrap_content"      
  11.     android:text="@string/hello"     
  12.     />     
  13.     <Gallery     
  14.     android:id="@+id/gy_main"     
  15.     android:layout_width="fill_parent"      
  16.     android:layout_height="fill_parent"      
  17.     android:layout_gravity="center_horizontal"     
  18.     android:spacing="10dip"     
  19.     android:layout_centerHorizontal="true"      
  20.          
  21.          
  22.     />     
  23. </LinearLayout>    

最后更新:2017-04-02 17:28:39

  上一篇:go Android-----使用Button特效selector+shape
  下一篇:go c语言基础(三)之数据交换