欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android控件Gallery3D效果实例代码

程序员文章站 2023-08-12 20:32:54
贴上代码: 1.扩展gallery: 复制代码 代码如下: public class galleryflow extends gallery { private camer...
贴上代码:
1.扩展gallery:
复制代码 代码如下:

public class galleryflow extends gallery {
private camera mcamera = new camera();//相机类
private int mmaxrotationangle = 60;//最大转动角度
private int mmaxzoom = -300;////最大缩放值
private int mcoveflowcenter;//半径值
public galleryflow(context context) {
super(context);
//支持转换 ,执行getchildstatictransformation方法
this.setstatictransformationsenabled(true);
}
public galleryflow(context context, attributeset attrs) {
super(context, attrs);
this.setstatictransformationsenabled(true);
}
public galleryflow(context context, attributeset attrs, int defstyle) {
super(context, attrs, defstyle);
this.setstatictransformationsenabled(true);
}
public int getmaxrotationangle() {
return mmaxrotationangle;
}
public void setmaxrotationangle(int maxrotationangle) {
mmaxrotationangle = maxrotationangle;
}
public int getmaxzoom() {
return mmaxzoom;
}
public void setmaxzoom(int maxzoom) {
mmaxzoom = maxzoom;
}
private int getcenterofcoverflow() {
return (getwidth() - getpaddingleft() - getpaddingright()) / 2
+ getpaddingleft();
}
private static int getcenterofview(view view) {
system.out.println("view left :"+view.getleft());
system.out.println("view width :"+view.getwidth());
return view.getleft() + view.getwidth() / 2;
}

复制代码 代码如下:

//控制gallery中每个图片的旋转(重写的gallery中方法)
protected boolean getchildstatictransformation(view child, transformation t) {
//取得当前子view的半径值
final int childcenter = getcenterofview(child);
system.out.println("childcenter:"+childcenter);
final int childwidth = child.getwidth();
//旋转角度
int rotationangle = 0;
//重置转换状态
t.clear();
//设置转换类型
t.settransformationtype(transformation.type_matrix);
//如果图片位于中心位置不需要进行旋转
if (childcenter == mcoveflowcenter) {
transformimagebitmap((imageview) child, t, 0);
} else {
//根据图片在gallery中的位置来计算图片的旋转角度
rotationangle = (int) (((float) (mcoveflowcenter - childcenter) / childwidth) * mmaxrotationangle);
system.out.println("rotationangle:" +rotationangle);
//如果旋转角度绝对值大于最大旋转角度返回(-mmaxrotationangle或mmaxrotationangle;)
if (math.abs(rotationangle) > mmaxrotationangle) {
rotationangle = (rotationangle < 0) ? -mmaxrotationangle : mmaxrotationangle;
}
transformimagebitmap((imageview) child, t, rotationangle);
}
return true;
}
protected void onsizechanged(int w, int h, int oldw, int oldh) {
mcoveflowcenter = getcenterofcoverflow();
super.onsizechanged(w, h, oldw, oldh);
}
private void transformimagebitmap(imageview child, transformation t,
int rotationangle) {
//对效果进行保存
mcamera.save();
final matrix imagematrix = t.getmatrix();
//图片高度
final int imageheight = child.getlayoutparams().height;
//图片宽度
final int imagewidth = child.getlayoutparams().width;
//返回旋转角度的绝对值
final int rotation = math.abs(rotationangle);
// 在z轴上正向移动camera的视角,实际效果为放大图片。
// 如果在y轴上移动,则图片上下移动;x轴上对应图片左右移动。
mcamera.translate(0.0f, 0.0f, 100.0f);
// as the angle of the view gets less, zoom in
if (rotation < mmaxrotationangle) {
float zoomamount = (float) (mmaxzoom + (rotation * 1.5));
mcamera.translate(0.0f, 0.0f, zoomamount);
}
// 在y轴上旋转,对应图片竖向向里翻转。
// 如果在x轴上旋转,则对应图片横向向里翻转。
mcamera.rotatey(rotationangle);
mcamera.getmatrix(imagematrix);
imagematrix.pretranslate(-(imagewidth / 2), -(imageheight / 2));
imagematrix.posttranslate((imagewidth / 2), (imageheight / 2));
mcamera.restore();
}
}

2.填充图片容器(baseadapter):
复制代码 代码如下:

public class imageadapter extends baseadapter {
int mgalleryitembackground;
private context mcontext;
private integer[] mimageids;
private imageview[] mimages;
public imageadapter(context c, integer[] imageids) {
mcontext = c;
mimageids = imageids;
mimages = new imageview[mimageids.length];
}
/**
* 创建倒影效果
* @return
*/
public boolean createreflectedimages() {
//倒影图和原图之间的距离
final int reflectiongap = 4;
int index = 0;
for (int imageid : mimageids) {
//返回原图解码之后的bitmap对象
bitmap originalimage = bitmapfactory.decoderesource(mcontext.getresources(), imageid);
int width = originalimage.getwidth();
int height = originalimage.getheight();
//创建矩阵对象
matrix matrix = new matrix();
//指定一个角度以0,0为坐标进行旋转
// matrix.setrotate(30);
//指定矩阵(x轴不变,y轴相反)
matrix.prescale(1, -1);
//将矩阵应用到该原图之中,返回一个宽度不变,高度为原图1/2的倒影位图
bitmap reflectionimage = bitmap.createbitmap(originalimage, 0,
height/2, width, height/2, matrix, false);
//创建一个宽度不变,高度为原图+倒影图高度的位图
bitmap bitmapwithreflection = bitmap.createbitmap(width,
(height + height / 2), config.argb_8888);
//将上面创建的位图初始化到画布
canvas canvas = new canvas(bitmapwithreflection);
canvas.drawbitmap(originalimage, 0, 0, null);
paint deafaultpaint = new paint();
deafaultpaint.setantialias(false);
// canvas.drawrect(0, height, width, height + reflectiongap,deafaultpaint);
canvas.drawbitmap(reflectionimage, 0, height + reflectiongap, null);
paint paint = new paint();
paint.setantialias(false);
/**
* 参数一:为渐变起初点坐标x位置,
* 参数二:为y轴位置,
* 参数三和四:分辨对应渐变终点,
* 最后参数为平铺方式,
* 这里设置为镜像gradient是基于shader类,所以我们通过paint的setshader方法来设置这个渐变
*/
lineargradient shader = new lineargradient(0,originalimage.getheight(), 0,
bitmapwithreflection.getheight() + reflectiongap,0x70ffffff, 0x00ffffff, tilemode.mirror);
//设置阴影
paint.setshader(shader);
paint.setxfermode(new porterduffxfermode(android.graphics.porterduff.mode.dst_in));
//用已经定义好的画笔构建一个矩形阴影渐变效果
canvas.drawrect(0, height, width, bitmapwithreflection.getheight()+ reflectiongap, paint);
//创建一个imageview用来显示已经画好的bitmapwithreflection
imageview imageview = new imageview(mcontext);
imageview.setimagebitmap(bitmapwithreflection);
//设置imageview大小 ,也就是最终显示的图片大小
imageview.setlayoutparams(new galleryflow.layoutparams(300, 400));
//imageview.setscaletype(scaletype.matrix);
mimages[index++] = imageview;
}
return true;
}
@suppresswarnings("unused")
private resources getresources() {
return null;
}
public int getcount() {
return mimageids.length;
}
public object getitem(int position) {
return position;
}
public long getitemid(int position) {
return position;
}
public view getview(int position, view convertview, viewgroup parent) {
return mimages[position];
}
public float getscale(boolean focused, int offset) {
return math.max(0, 1.0f / (float) math.pow(2, math.abs(offset)));
}
}

3.创建activity:
复制代码 代码如下:

public class gallery3dactivity extends activity {
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.layout_gallery);
integer[] images = { r.drawable.img0001, r.drawable.img0030,
r.drawable.img0100, r.drawable.img0130, r.drawable.img0200,
r.drawable.img0230, r.drawable.img0330,r.drawable.img0354 };
imageadapter adapter = new imageadapter(this, images);
adapter.createreflectedimages();//创建倒影效果
galleryflow galleryflow = (galleryflow) this.findviewbyid(r.id.gallery01);
galleryflow.setfadingedgelength(0);
galleryflow.setspacing(-100); //图片之间的间距
galleryflow.setadapter(adapter);
galleryflow.setonitemclicklistener(new onitemclicklistener() {
public void onitemclick(adapterview<?> parent, view view,
int position, long id) {
toast.maketext(getapplicationcontext(), string.valueof(position), toast.length_short).show();
}
});
galleryflow.setselection(4);
}
}

以上实现代码里面我都做了注释相信大家完全可以看懂。稍微解释下,在baseadapter中主要做了图片的倒影效果以及创建了对原始图片和倒影的显示区域。galleryflow中主要做了对图片的旋转和缩放操作,根据图片的屏幕中的位置对其进行旋转缩放操作。
效果图如下
Android控件Gallery3D效果实例代码