gallery擴展2——animation基於java代碼——實現點擊放大縮小

 

 

一、activity代碼

package com.liudan.activity;

 

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;

import com.liudan.adapter.GalleryAdapter;

public class GalleryActivity extends Activity {
 
    private Gallery gallery ;
    private List<Integer> galleryList;
    private AnimationSet manimationSet;

  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        initData();
        gallery = (Gallery)findViewById(R.id.gallery);
        GalleryAdapter galleryAdapter = new GalleryAdapter(this,galleryList);
        gallery.setAdapter(galleryAdapter);
        //設置顯示在屏幕中間的圖片,默認是把第一張圖片顯示在屏幕中間,那麼在屏幕的左邊就是空白。
        gallery.setSelection(galleryList.size()/2);
        gallery.setOnItemSelectedListener(new GalleryOnItemSelectedListener());
       
    }
 private void initData() {
  galleryList = new ArrayList<Integer>();
  for(int i=0;i<8;i++){
   galleryList.add(R.drawable.icon);
  }
  
 }
 
 private class GalleryOnItemSelectedListener implements OnItemSelectedListener{

  @Override
  public void onItemSelected(AdapterView<?> parent, View view,
    int position, long id) {
   
   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;
  }

  @Override
  public void onNothingSelected(AdapterView<?> parent) {
   // TODO Auto-generated method stub

  }
  
 }
 
 
}

 

二、adapter代碼

package com.liudan.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class GalleryAdapter extends BaseAdapter {
 private List<Integer> galleryList;
 private Context context;

 public GalleryAdapter(Context context,List<Integer> galleryList) {
  this.galleryList = galleryList;
  this.context = context;
 }

 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  return galleryList.size();
 }

 @Override
 public Object getItem(int position) {
  // TODO Auto-generated method stub
  return galleryList.get(position);
 }

 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  View view = null;
  if(convertView == null){
   ImageView imageView = new ImageView(context);
   imageView.setImageResource(galleryList.get(position));
   view = imageView;
  }else{
   view = convertView;
  }
  // TODO Auto-generated method stub
  return view;
 }

}

 

三、xml代碼

<?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/gallery"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:layout_gravity="center_horizontal"
   android:layout_centerHorizontal="true"
   android:spacing="10dip"/>
</LinearLayout>

發佈了55 篇原創文章 · 獲贊 28 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章