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(){@Overridepublic 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; }@Overridepublic int getCount() {return data.length;}@Overridepublic Object getItem(int position) {return data[position];}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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>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); } } };