Android控件Gallery3D效果
来源:互联网 发布:淘宝怎么搜烟 编辑:程序博客网 时间:2024/03/29 05:35
贴上代码:
1.扩展Gallery:
001
public
class
GalleryFlow
extends
Gallery {
002
private
Camera mCamera =
new
Camera();
//相机类
003
private
int
mMaxRotationAngle =
60
;
//最大转动角度
004
private
int
mMaxZoom = -
300
;
////最大缩放值
005
private
int
mCoveflowCenter;
//半径值
006
public
GalleryFlow(Context context) {
007
super
(context);
008
//支持转换 ,执行getChildStaticTransformation方法
009
this
.setStaticTransformationsEnabled(
true
);
010
}
011
public
GalleryFlow(Context context, AttributeSet attrs) {
012
super
(context, attrs);
013
this
.setStaticTransformationsEnabled(
true
);
014
}
015
public
GalleryFlow(Context context, AttributeSet attrs,
int
defStyle) {
016
super
(context, attrs, defStyle);
017
this
.setStaticTransformationsEnabled(
true
);
018
}
019
public
int
getMaxRotationAngle() {
020
return
mMaxRotationAngle;
021
}
022
public
void
setMaxRotationAngle(
int
maxRotationAngle) {
023
mMaxRotationAngle = maxRotationAngle;
024
}
025
public
int
getMaxZoom() {
026
return
mMaxZoom;
027
}
028
public
void
setMaxZoom(
int
maxZoom) {
029
mMaxZoom = maxZoom;
030
}
031
private
int
getCenterOfCoverflow() {
032
return
(getWidth() - getPaddingLeft() - getPaddingRight()) /
2
033
+ getPaddingLeft();
034
}
035
private
static
int
getCenterOfView(View view) {
036
System.out.println(
"view left :"
+view.getLeft());
037
System.out.println(
"view width :"
+view.getWidth());
038
return
view.getLeft() + view.getWidth() /
2
;
039
}
040
041
042
//控制gallery中每个图片的旋转(重写的gallery中方法)
043
protected
boolean
getChildStaticTransformation(View child, Transformation t) {
044
//取得当前子view的半径值
045
final
int
childCenter = getCenterOfView(child);
046
System.out.println(
"childCenter:"
+childCenter);
047
final
int
childWidth = child.getWidth();
048
//旋转角度
049
int
rotationAngle =
0
;
050
//重置转换状态
051
t.clear();
052
//设置转换类型
053
t.setTransformationType(Transformation.TYPE_MATRIX);
054
//如果图片位于中心位置不需要进行旋转
055
if
(childCenter == mCoveflowCenter) {
056
transformImageBitmap((ImageView) child, t,
0
);
057
}
else
{
058
//根据图片在gallery中的位置来计算图片的旋转角度
059
rotationAngle = (
int
) (((
float
) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
060
System.out.println(
"rotationAngle:"
+rotationAngle);
061
//如果旋转角度绝对值大于最大旋转角度返回(-mMaxRotationAngle或mMaxRotationAngle;)
062
if
(Math.abs(rotationAngle) > mMaxRotationAngle) {
063
rotationAngle = (rotationAngle <
0
) ? -mMaxRotationAngle : mMaxRotationAngle;
064
}
065
transformImageBitmap((ImageView) child, t, rotationAngle);
066
}
067
return
true
;
068
}
069
protected
void
onSizeChanged(
int
w,
int
h,
int
oldw,
int
oldh) {
070
mCoveflowCenter = getCenterOfCoverflow();
071
super
.onSizeChanged(w, h, oldw, oldh);
072
}
073
private
void
transformImageBitmap(ImageView child, Transformation t,
074
int
rotationAngle) {
075
//对效果进行保存
076
mCamera.save();
077
final
Matrix imageMatrix = t.getMatrix();
078
//图片高度
079
final
int
imageHeight = child.getLayoutParams().height;
080
//图片宽度
081
final
int
imageWidth = child.getLayoutParams().width;
082
083
//返回旋转角度的绝对值
084
final
int
rotation = Math.abs(rotationAngle);
085
086
// 在Z轴上正向移动camera的视角,实际效果为放大图片。
087
// 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。
088
mCamera.translate(
0
.0f,
0
.0f,
100
.0f);
089
// As the angle of the view gets less, zoom in
090
if
(rotation < mMaxRotationAngle) {
091
float
zoomAmount = (
float
) (mMaxZoom + (rotation *
1.5
));
092
mCamera.translate(
0
.0f,
0
.0f, zoomAmount);
093
}
094
// 在Y轴上旋转,对应图片竖向向里翻转。
095
// 如果在X轴上旋转,则对应图片横向向里翻转。
096
mCamera.rotateY(rotationAngle);
097
mCamera.getMatrix(imageMatrix);
098
imageMatrix.preTranslate(-(imageWidth /
2
), -(imageHeight /
2
));
099
imageMatrix.postTranslate((imageWidth /
2
), (imageHeight /
2
));
100
mCamera.restore();
101
}
102
}
103
104
2
.填充图片容器(BaseAdapter):
105
public
class
ImageAdapter
extends
BaseAdapter {
106
int
mGalleryItemBackground;
107
private
Context mContext;
108
private
Integer[] mImageIds;
109
private
ImageView[] mImages;
110
111
public
ImageAdapter(Context c, Integer[] ImageIds) {
112
mContext = c;
113
mImageIds = ImageIds;
114
mImages =
new
ImageView[mImageIds.length];
115
}
116
/**
117
* 创建倒影效果
118
* @return
119
*/
120
public
boolean
createReflectedImages() {
121
//倒影图和原图之间的距离
122
final
int
reflectionGap =
4
;
123
int
index =
0
;
124
for
(
int
imageId : mImageIds) {
125
//返回原图解码之后的bitmap对象
126
Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), imageId);
127
int
width = originalImage.getWidth();
128
int
height = originalImage.getHeight();
129
//创建矩阵对象
130
Matrix matrix =
new
Matrix();
131
132
//指定一个角度以0,0为坐标进行旋转
133
// matrix.setRotate(30);
134
135
//指定矩阵(x轴不变,y轴相反)
136
matrix.preScale(
1
, -
1
);
137
138
//将矩阵应用到该原图之中,返回一个宽度不变,高度为原图1/2的倒影位图
139
Bitmap reflectionImage = Bitmap.createBitmap(originalImage,
0
,
140
height/
2
, width, height/
2
, matrix,
false
);
141
142
//创建一个宽度不变,高度为原图+倒影图高度的位图
143
Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
144
(height + height /
2
), Config.ARGB_8888);
145
146
//将上面创建的位图初始化到画布
147
Canvas canvas =
new
Canvas(bitmapWithReflection);
148
canvas.drawBitmap(originalImage,
0
,
0
,
null
);
149
150
Paint deafaultPaint =
new
Paint();
151
deafaultPaint.setAntiAlias(
false
);
152
// canvas.drawRect(0, height, width, height + reflectionGap,deafaultPaint);
153
canvas.drawBitmap(reflectionImage,
0
, height + reflectionGap,
null
);
154
Paint paint =
new
Paint();
155
paint.setAntiAlias(
false
);
156
157
/**
158
* 参数一:为渐变起初点坐标x位置,
159
* 参数二:为y轴位置,
160
* 参数三和四:分辨对应渐变终点,
161
* 最后参数为平铺方式,
162
* 这里设置为镜像Gradient是基于Shader类,所以我们通过Paint的setShader方法来设置这个渐变
163
*/
164
LinearGradient shader =
new
LinearGradient(
0
,originalImage.getHeight(),
0
,
165
bitmapWithReflection.getHeight() + reflectionGap,
0x70ffffff
,
0x00ffffff
, TileMode.MIRROR);
166
//设置阴影
167
paint.setShader(shader);
168
paint.setXfermode(
new
PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
169
//用已经定义好的画笔构建一个矩形阴影渐变效果
170
canvas.drawRect(
0
, height, width, bitmapWithReflection.getHeight()+ reflectionGap, paint);
171
172
//创建一个ImageView用来显示已经画好的bitmapWithReflection
173
ImageView imageView =
new
ImageView(mContext);
174
imageView.setImageBitmap(bitmapWithReflection);
175
//设置imageView大小 ,也就是最终显示的图片大小
176
imageView.setLayoutParams(
new
GalleryFlow.LayoutParams(
300
,
400
));
177
//imageView.setScaleType(ScaleType.MATRIX);
178
mImages[index++] = imageView;
179
}
180
return
true
;
181
}
182
@SuppressWarnings
(
"unused"
)
183
private
Resources getResources() {
184
return
null
;
185
}
186
public
int
getCount() {
187
return
mImageIds.length;
188
}
189
public
Object getItem(
int
position) {
190
return
position;
191
}
192
public
long
getItemId(
int
position) {
193
return
position;
194
}
195
public
View getView(
int
position, View convertView, ViewGroup parent) {
196
return
mImages[position];
197
}
198
public
float
getScale(
boolean
focused,
int
offset) {
199
return
Math.max(
0
,
1
.0f / (
float
) Math.pow(
2
, Math.abs(offset)));
200
}
201
}
202
203
3
.创建Activity:
204
public
class
Gallery3DActivity
extends
Activity {
205
public
void
onCreate(Bundle savedInstanceState) {
206
super
.onCreate(savedInstanceState);
207
208
209
setContentView(R.layout.layout_gallery);
210
211
Integer[] images = { R.drawable.img0001, R.drawable.img0030,
212
R.drawable.img0100, R.drawable.img0130, R.drawable.img0200,
213
R.drawable.img0230, R.drawable.img0330,R.drawable.img0354 };
214
215
ImageAdapter adapter =
new
ImageAdapter(
this
, images);
216
adapter.createReflectedImages();
//创建倒影效果
217
GalleryFlow galleryFlow = (GalleryFlow)
this
.findViewById(R.id.Gallery01);
218
galleryFlow.setFadingEdgeLength(
0
);
219
galleryFlow.setSpacing(-
100
);
//图片之间的间距
220
galleryFlow.setAdapter(adapter);
221
222
galleryFlow.setOnItemClickListener(
new
OnItemClickListener() {
223
public
void
onItemClick(AdapterView parent, View view,
224
int
position,
long
id) {
225
Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
226
}
227
228
});
229
galleryFlow.setSelection(
4
);
230
}
231
}
以上实现代码里面我都做了注释相信大家完全可以看懂。稍微解释下,在BaseAdapter中主要做了图片的倒影效果以及创建了对原始图片和倒影的显示区域。GalleryFlow中主要做了对图片的旋转和缩放操作,根据图片的屏幕中的位置对其进行旋转缩放操作
- Android控件Gallery3D效果
- Android控件Gallery3D效果
- Android控件Gallery3D效果
- Android控件Gallery3D效果 .
- Android控件Gallery3D效果
- Android控件Gallery3D效果
- Android控件Gallery3D效果
- Android控件Gallery3D效果
- Android控件Gallery3D效果
- Android控件Gallery3D效果
- Android控件Gallery3D效果
- Android控件Gallery3D效果
- Android控件Gallery3D效果
- Android控件开发之Gallery3D效果
- Android控件开发之Gallery3D效果
- Android控件开发之Gallery3D效果
- Android控件开发之Gallery3D效果
- Android控件开发之Gallery3D效果
- 散列表(HashTable)探秘 --中
- 修改Tomcat7的/webapps/ROOT发布路径
- 散列表(HashTable)探秘 --下
- android-仿美丽说具有按下效果的顶部导航栏
- IOS多线程读写Sqlite问题解决
- Android控件Gallery3D效果
- rpm的用法
- Eclipse下配置使用glib库的方法【linux版本】
- 算法技巧总结
- 微信高仿导航页开门效果(送上源码)
- 网络设备驱动 和 DM9000 驱动程序分析
- find用法
- MySQL dbcp 下连接空闲8小时自动断开问题解决方案
- 在c# / ASP.net中获取当前日期