android Gallery之动画效果

Gallery是一款用PHP开发的基于WEB的相片管理系统,Gallery 非常易于使用,包括一个配置向导,对于相片的操作包括自动生成缩略图、相片的大小改变、选择、排序等

这个程序主要是加深对Gallery的进一步了解,

主程序有两大重点:第一,是ImageAdapter继承BaseAdapter class的未实现方法的重写构造;
         第二,则是动画效果

第一步,先写好main.xml文件,比较简单,贴上代码

View Code
 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:asg="http://schemas.android.com/apk/res/org.pskink"
5 android:layout_width="fill_parent"
6 android:layout_height="fill_parent"
7 android:orientation="vertical"
8 >
9 <org.pskink.AnimatedSizingGallery
10 android:layout_width="fill_parent"
11 android:layout_height="0dip"
12 android:layout_weight="2"
13 android:id="@+id/gallery0"
14 asg:animationScale="2.5"
15 asg:animationDuration="400"
16 asg:animationOffsetY="-0.5"
17 />
18 <org.pskink.AnimatedSizingGallery
19 android:layout_width="fill_parent"
20 android:layout_height="0dip"
21 android:layout_weight="1"
22 android:id="@+id/gallery1"
23 />
24 </LinearLayout>

第二步:修改strings.xml文件

View Code
1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <string name="app_name">Sizing Gallery</string>
4 </resources>


第三步:在values文件夹下添加atts.xml文件,具体代码如下

View Code
 1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <declare-styleable name="AnimatedSizingGallery">
4 <!-- defaults to 2.0 -->
5 <attr name="animationScale" format="float" />
6 <!-- defaults to 0.0 - no vertical movement-->
7 <attr name="animationOffsetY" format="float" />
8 <!-- defaults to 500 -->
9 <attr name="animationDuration" format="integer" />
10 </declare-styleable>
11 </resources>

第四步:添加照片

第五步:修改GalleryTest.xml文件,具体代码如下

View Code
 1 package org.pskink;
2
3 import android.app.Activity;
4 import android.content.Context;
5 import android.os.Bundle;
6 import android.view.View;
7 import android.view.ViewGroup;
8 import android.widget.BaseAdapter;
9 import android.widget.Gallery;
10 import android.widget.ImageView;
11
12 public class GalleryTest extends Activity {
13 @Override
14 public void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.main);
17
18 Gallery g = (Gallery) findViewById(R.id.gallery0);
19 ImageAdapter a = new ImageAdapter(this);
20 g.setAdapter(a);
21
22 g = (Gallery) findViewById(R.id.gallery1);
23 g.setAdapter(a);
24 }
25
26 public static class ImageAdapter extends BaseAdapter {
27 private Context mContext;
28 public ImageAdapter(Context c) {
29 mContext = c;
30 }
31
32 public int getCount() {
33 return mThumbIds.length;
34 }
35 //获取图片在库中的位置
36 public Object getItem(int position) {
37 return position;
38 }
39
40 public long getItemId(int position) {
41 return position;
42 }
43
44 public View getView(int position, View convertView, ViewGroup parent) {
45 ImageView imageView;
46 if (convertView == null) {
47 imageView = new ImageView(mContext);
48 imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
49 imageView.setAdjustViewBounds(false);
50 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
51 imageView.setPadding(18, 18, 18, 18);
52 } else {
53 imageView = (ImageView) convertView;
54 }
55 //给ImageView设置资源
56 imageView.setImageResource(mThumbIds[position]);
57
58 return imageView;
59 }
60 //定义整型数组 即图片源
61 private Integer[] mThumbIds = {
62 R.drawable.shao1,
63 R.drawable.shao2,
64 R.drawable.shao3,
65 R.drawable.shao4,
66 R.drawable.shao5,
67 R.drawable.accessories,
68 R.drawable.development,
69 R.drawable.education,
70 R.drawable.engineering,
71
72 };
73 }
74 }

第六步:也是最难的,都是些算法,不过不懂也没事,不影响你实现功能,代码如下

View Code
  1 import android.content.Context;
2 import android.content.res.TypedArray;
3 import android.graphics.Matrix;
4 import android.os.Handler;
5 import android.os.Message;
6 import android.util.AttributeSet;
7 import android.util.Log;
8 import android.view.View;
9 import android.view.animation.Animation;
10 import android.view.animation.Transformation;
11 import android.widget.AdapterView;
12 import android.widget.Gallery;
13 import android.widget.ImageView;
14 import android.widget.AdapterView.OnItemSelectedListener;
15
16 public class AnimatedSizingGallery extends Gallery implements OnItemSelectedListener {
17 public static final String TAG = "AnimatedSizingGallery";
18 private static final int MSG_ZOOM_IN = 1;
19 private static final long DELAY = 100;
20
21 private View mPrev;
22 private float mAnimationScale;
23 private float mAnimationOffsetY;
24 private int mAnimationDuration;
25
26 public AnimatedSizingGallery(Context context, AttributeSet attrs) {
27 super(context, attrs);
28 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AnimatedSizingGallery);
29 mAnimationScale = a.getFloat(R.styleable.AnimatedSizingGallery_animationScale, 2);
30 mAnimationOffsetY = a.getFloat(R.styleable.AnimatedSizingGallery_animationOffsetY, 0);
31 mAnimationDuration = a.getInteger(R.styleable.AnimatedSizingGallery_animationDuration, 500);
32 a.recycle();
33 setOnItemSelectedListener(this);
34 }
35
36 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
37 if (mPrev != null) {
38 zoomOut();
39 }
40 mPrev = view;
41 mGalleryHandler.removeCallbacksAndMessages(view);
42 Message msg = Message.obtain(mGalleryHandler, MSG_ZOOM_IN, view);
43 mGalleryHandler.sendMessageDelayed(msg, DELAY);
44 }
45
46 public void onNothingSelected(AdapterView<?> parent) {
47 Log.d(TAG, "onNothingSelected called !!!");
48 if (mPrev != null) {
49 zoomOut();
50 mPrev = null;
51 }
52 }
53
54 private void zoomOut() {
55 if (mGalleryHandler.hasMessages(MSG_ZOOM_IN, mPrev)) {
56 mGalleryHandler.removeCallbacksAndMessages(mPrev);
57 } else {
58 ZoomAnimation a = (ZoomAnimation) mPrev.getAnimation();
59 a.resetForZoomOut();
60 mPrev.startAnimation(a);
61 }
62 }
63
64 Handler mGalleryHandler = new Handler() {
65 @Override
66 public void dispatchMessage(Message msg) {
67 if (msg.what == MSG_ZOOM_IN) {
68 ImageView view = (ImageView) msg.obj;
69 Animation a = new ZoomAnimation(view, 1, mAnimationScale, mAnimationOffsetY, mAnimationDuration);
70 view.startAnimation(a);
71 }
72 }
73 };
74
75 class ZoomAnimation extends Animation {
76 private float mFrom;
77 private float mTo;
78 private float mOffsetY;
79 private int mPivotX;
80 private int mPivotY;
81 private float mInterpolatedTime;
82
83 public ZoomAnimation(View v, float from, float to, float offsetY, int duration) {
84 super();
85 mFrom = from;
86 mTo = to;
87 mOffsetY = offsetY * v.getHeight();
88 setDuration(duration);
89 setFillAfter(true);
90 mPivotX = v.getWidth() / 2;
91 mPivotY = v.getHeight() / 2;
92 }
93
94 public void resetForZoomOut() {
95 reset();
96 mOffsetY = 0;
97 mFrom = mFrom + (mTo - mFrom) * mInterpolatedTime;
98 mTo = 1;
99 }
100
101 @Override
102 protected void applyTransformation(float interpolatedTime, Transformation t) {
103 mInterpolatedTime = interpolatedTime;
104 float s = mFrom + (mTo - mFrom) * interpolatedTime;
105 Matrix matrix = t.getMatrix();
106 if (mOffsetY != 0) {
107 matrix.preTranslate(0, mOffsetY * interpolatedTime);
108 }
109 if (mTo == 1) {
110 matrix.preRotate(360 * interpolatedTime, mPivotX, mPivotY);
111 }
112 matrix.preScale(s, s, mPivotX, mPivotY);
113 }
114 }
115 }

下面在看效果图:





原文地址:https://www.cnblogs.com/shaoyangjiang/p/2368038.html