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

本文介绍了一个基于Android的Gallery组件实现的图片展示案例,通过点击事件触发图片的缩放动画效果,展示了如何自定义Adapter以及设置动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值