大家好,我是你的好朋友思创斯。今天说一说android控件开发之gallery3d酷炫效果(带源码)[亲测有效],希望您对编程的造诣更进一步.
private integer mimagesid[] = {
r.drawable.ic_1,
r.drawable.ic_3,
r.drawable.ic_2,
r.drawable.ic_4,
r.drawable.ic_5
};
三:gallery 控件类
缩放,还有透明,等等都在这里设置
public class fancycoverflow extends gallery {
public static final int action_distance_auto = integer.max_value;
/**
- 图片向上突出,可以通过代码控制,也可以在xml上控制
*/
public static final float scaledown_gravity_top = 0.0f;
/**
- 图片中间突出
*/
public static final float scaledown_gravity_center = 0.5f;
/**
- 图片向下突出
*/
public static final float scaledown_gravity_bottom = 1.0f;
private float reflectionratio = 0.3f;
private int reflectiongap = 4;
private boolean reflectionenabled = false;
private float unselectedalpha;
private camera transformationcamera;
private int maxrotation = 0;
private float unselectedscale;
private float scaledowngravity = scaledown_gravity_center;
private int actiondistance;
private float unselectedsaturation;
public fancycoverflow(context context) {
super(context);
this.initialize();
}
public fancycoverflow(context context, attributeset attrs) {
super(context, attrs);
this.initialize();
this.applyxmlattributes(attrs);
}
@suppresslint(“newapi”)
public fancycoverflow(context context, attributeset attrs, int defstyle) {
super(context, attrs, defstyle);
if (build.version.sdk_int >= 11) {
this.setlayertype(layer_type_software, null);
}
this.initialize();
this.applyxmlattributes(attrs);
}
private void initialize() {
this.transformationcamera = new camera();
this.setspacing(0);
}
private void applyxmlattributes(attributeset attrs) {
typedarray a = getcontext().obtainstyledattributes(attrs,
r.styleable.fancycoverflow);
this.actiondistance = a
.getinteger(r.styleable.fancycoverflow_actiondistance,
action_distance_auto);
this.scaledowngravity = a.getfloat(
r.styleable.fancycoverflow_scaledowngravity, 0.5f);
this.maxrotation = a.getinteger(r.styleable.fancycoverflow_maxrotation,
0);
this.unselectedalpha = a.getfloat(
r.styleable.fancycoverflow_unselectedalpha, 0.5f);
this.unselectedsaturation = a.getfloat(
r.styleable.fancycoverflow_unselectedsaturation, 0.0f);
this.unselectedscale = a.getfloat(
r.styleable.fancycoverflow_unselectedscale, 0.75f);
}
public float getreflectionratio() {
return reflectionratio;
}
public void setreflectionratio(float reflectionratio) {
if (reflectionratio <= 0 || reflectionratio > 0.5f) {
throw new illegalargumentexception(
“reflectionratio may only be in the interval (0, 0.5]”);
}
this.reflectionratio = reflectionratio;
if (this.getadapter() != null) {
((fancycoverflowadapter) this.getadapter()).notifydatasetchanged();
}
}
public int getreflectiongap() {
return reflectiongap;
}
public void setreflectiongap(int reflectiongap) {
this.reflectiongap = reflectiongap;
if (this.getadapter() != null) {
((fancycoverflowadapter) this.getadapter()).notifydatasetchanged();
}
}
public boolean isreflectionenabled() {
return reflectionenabled;
}
public void setreflectionenabled(boolean reflectionenabled) {
this.reflectionenabled = reflectionenabled;
if (this.getadapter() != null) {
((fancycoverflowadapter) this.getadapter()).notifydatasetchanged();
}
}
@override
public void setadapter(spinneradapter adapter) {
if (!(adapter instanceof fancycoverflowadapter)) {
throw new classcastexception(fancycoverflow.class.getsimplename()
-
” only works in conjunction with a “
-
fancycoverflowadapter.class.getsimplename());
}
super.setadapter(adapter);
}
public int getmaxrotation() {
return maxrotation;
}
public void setmaxrotation(int maxrotation) {
this.maxrotation = maxrotation;
}
public float getunselectedalpha() {
return this.unselectedalpha;
}
public float getunselectedscale() {
return unselectedscale;
}
public void setunselectedscale(float unselectedscale) {
this.unselectedscale = unselectedscale;
}
public float getscaledowngravity() {
return scaledowngravity;
}
public void setscaledowngravity(float scaledowngravity) {
this.scaledowngravity = scaledowngravity;
}
public int getactiondistance() {
return actiondistance;
}
public void setactiondistance(int actiondistance) {
this.actiondistance = actiondistance;
}
@override
public void setunselectedalpha(float unselectedalpha) {
super.setunselectedalpha(unselectedalpha);
this.unselectedalpha = unselectedalpha;
}
public float getunselectedsaturation() {
return unselectedsaturation;
}
public void setunselectedsaturation(float unselectedsaturation) {
this.unselectedsaturation = unselectedsaturation;
}
public int preleftoffset = 0;
public int count = 0;
public boolean isplaydraw = true;
@override
protected boolean getchildstatictransformation(view child, transformation t) {
fancycoverflowitemwrapper item = (fancycoverflowitemwrapper) child;
preleftoffset = getchildat(0).getleft();
if (android.os.build.version.sdk_int >= 16) {
item.postinvalidate();
}
final int coverflowwidth = this.getwidth();
final int coverflowcenter = coverflowwidth / 2;
final int childwidth = item.getwidth();
final int childheight = item.getheight();
final int childcenter = item.getleft() childwidth / 2;
-
final int actiondistance = (this.actiondistance == action_distance_auto) ? (int) ((coverflowwidth childwidth) / 2.0f)
- this.actiondistance;
float effectsamount = math.min(
1.0f,
math.max(-1.0f, (1.0f / actiondistance)
- (childcenter – coverflowcenter)));
t.clear();
t.settransformationtype(transformation.type_both);
if (this.unselectedalpha != 1) {
final float alphaamount = (this.unselectedalpha – 1)
- math.abs(effectsamount) 1;
t.setalpha(alphaamount);
}
if (this.unselectedsaturation != 1) {
// pass over saturation to the wrapper.
final float saturationamount = (this.unselectedsaturation – 1)
- math.abs(effectsamount) 1;
item.setsaturation(saturationamount);
}
final matrix imagematrix = t.getmatrix();
// 旋转角度不为0则开始图片旋转.
if (this.maxrotation != 0) {
final int rotationangle = (int) (-effectsamount * this.maxrotation);
this.transformationcamera.save();
this.transformationcamera.rotatey(rotationangle);
this.transformationcamera.getmatrix(imagematrix);
this.transformationcamera.restore();
}
// 缩放.
if (this.unselectedscale != 1) {
final float zoomamount = 1f / 2f * (1 – math.abs(effectsamount))
-
(1 – math.abs(effectsamount))
-
(1 – math.abs(effectsamount)) 0.5f;
final float translatex = childwidth / 2.0f;
final float translatey = childheight * this.scaledowngravity;
imagematrix.pretranslate(-translatex, -translatey);
imagematrix.postscale(zoomamount, zoomamount);
imagematrix.posttranslate(translatex, translatey);
if (effectsamount != 0) {
double point = 0.4;
double translatefactor = (-1f / (point * point)
-
(math.abs(effectsamount) – point)
-
(math.abs(effectsamount) – point) 1)
-
(effectsamount > 0 ? 1 : -1);
imagematrix
.posttranslate(
(float) (viewutil.dp2px(getcontext(), 25) * translatefactor),
0);
}
}
return true;
}
// 绘制顺序,先从左到中间,再从右到中间
@override
protected int getchilddrawingorder(int childcount, int i) {
int selectedindex = getselecteditemposition()
- getfirstvisibleposition();
if (i < selectedindex) {
return i;
} else if (i >= selectedindex) {
return childcount – 1 – i selectedindex;
} else {
return i;
}
}
private boolean istouchable = true;
public void disabletouch() {
istouchable = false;
}
public void enabletouch() {
istouchable = true;
}
public boolean istouchable() {
return istouchable;
}
@override
public boolean ontouchevent(motionevent event) {
count = 0;
for (int i = 0; i < getchildcount(); i ) {
getchildat(i).invalidate();
}
if (istouchable) {
return super.ontouchevent(event);
} else {
return false;
}
}
@override
public boolean onintercepttouchevent(motionevent event) {
if (istouchable) {
return super.onintercepttouchevent(event);
} else {
return true;
}
}
//
// @override
// public boolean onsingletapup(motionevent e) {
// return false;
// }
// 使快速滑动失效
@override
public boolean onfling(motionevent e1, motionevent e2, float velocityx,
float velocityy) {
return false;
}
四:倒影
reflectiongap 处理图片和倒影之间的间距。
/**
- grallery 倒影放大操作类
*/
public class myimgview {
/**
-
添加倒影,原理,先翻转图片,由上到下放大透明度
-
@param originalimage
-
@return
*/
public static bitmap createreflectedimage(bitmap originalimage) {
// the gap we want between the reflection and the original image
final int reflectiongap = 4;
int width = originalimage.getwidth();
int height = originalimage.getheight();
// this will not scale but will flip on the y axis
matrix matrix = new matrix();
matrix.prescale(1, -1);
// create a bitmap with th 《android学习笔记总结 最新移动架构视频 大厂安卓面试真题 项目实战源码讲义》无偿开源 徽信搜索公众号【编程进阶路】 e flip matrix applied to it.
// we only want the bottom half of the image
bitmap reflectionimage = bitmap.createbitmap(originalimage, 0,
height / 2, width, height / 2, matrix, false);
// create a new bitmap with same width but taller to fit reflection
bitmap bitmapwithreflection = bitmap.createbitmap(width,
(height height /4), config.argb_8888);
// create a new canvas with the bitmap that’s big enough for
// the image plus gap plus reflection
文章由思创斯整理,转载请注明出处:https://ispacesoft.com/140969.html