android中图片翻页效果简单的实现方法
public class pagewidget extends view {
private bitmap foreimage;
private bitmap bgimage;
private pointf touchpt;
private int screenwidth;
private int screenheight;
private gradientdrawable shadowdrawablerl;
private gradientdrawable shadowdrawablelr;
private colormatrixcolorfilter mcolormatrixfilter;
private scroller mscroller;
private int lasttouchx;
public pagewidget(context context) {
super(context);
// todo auto-generated constructor stub
touchpt = new pointf(-1,-1);
//argb a(0-透明,255-不透明)
int[] color = { 0xb0333333 ,0x00333333};
shadowdrawablerl = new gradientdrawable(gradientdrawable.orientation.right_left, color);
shadowdrawablerl.setgradienttype(gradientdrawable.linear_gradient);
shadowdrawablelr = new gradientdrawable(gradientdrawable.orientation.left_right, color);
shadowdrawablelr.setgradienttype(gradientdrawable.linear_gradient);
float array[] = { 0.55f, 0, 0, 0, 80.0f,
,0.55f, 0, 0, 80.0f,
, 0,0.55f, 0, 80.0f,
, 0, 0, 0.2f, 0};
colormatrix cm = new colormatrix();
cm.set(array);
/*
* |a*0.55 + 80|
* |r*0.55 + 80|
* |g*0.55 + 80|
* |b*0.2|
*/
// cm.setsaturation(0);
mcolormatrixfilter = new colormatrixcolorfilter(cm);
//利用滚动条来实现接触点放开后的动画效果
mscroller = new scroller(context);
}
@override
public void computescroll() {
// todo auto-generated method stub
if (mscroller.computescrolloffset()) {
touchpt.x = mscroller.getcurrx();
touchpt.y = mscroller.getcurry();
postinvalidate();
}
else{
// touchpt.x = -1;
// touchpt.y = -1;
}
super.computescroll();
}
public void setscreen(int screenwidth,int screenheight){
this.screenwidth = screenwidth;
this.screenheight = screenheight;
}
public bitmap getforeimage() {
return foreimage;
}
public void setforeimage(bitmap foreimage) {
this.foreimage = foreimage;
}
public bitmap getbgimage() {
return bgimage;
}
public void setbgimage(bitmap bgimage) {
this.bgimage = bgimage;
}
@override
protected void ondraw(canvas canvas) {
// todo auto-generated method stub
drawpageeffect(canvas);
super.ondraw(canvas);
}
/**
* 画前景图片
* @param canvas
*/
private void drawforceimage(canvas canvas) {
// todo auto-generated method stub
paint mpaint = new paint();
if (foreimage!=null) {
canvas.drawbitmap(foreimage, 0, 0, mpaint);
}
}
/**
* 画背景图片
* @param canvas
*/
private void drawbgimage(canvas canvas,path path) {
// todo auto-generated method stub
paint mpaint = new paint();
if (bgimage!=null) {
canvas.save();
//只在与路径相交处画图
canvas.clippath(path,op.intersect);
canvas.drawbitmap(bgimage, 0, 0, mpaint);
canvas.restore();
}
}
/**
* 画翻页效果
* @param canvas
*/
private void drawpageeffect(canvas canvas) {
// todo auto-generated method stub
drawforceimage(canvas);
paint mpaint = new paint();
if (touchpt.x!=-1 && touchpt.y!=-1) {
//翻页左侧书边
canvas.drawline(touchpt.x, 0, touchpt.x,screenheight, mpaint);
//左侧书边画阴影
shadowdrawablerl.setbounds((int)touchpt.x - 20, 0 ,(int)touchpt.x, screenheight);
shadowdrawablerl.draw(canvas);
//翻页对折处
float halfcut = touchpt.x + (screenwidth - touchpt.x)/2;
canvas.drawline(halfcut, 0, halfcut, screenheight, mpaint);
//对折处左侧画翻页页图片背面
rect backarea = new rect((int)touchpt.x,0,(int)halfcut,screenheight);
paint backpaint = new paint();
backpaint.setcolor(0xffdacab0);
canvas.drawrect(backarea, backpaint);
//将翻页图片正面进行处理水平翻转并平移到touchpt.x点
paint fbpaint = new paint();
fbpaint.setcolorfilter(mcolormatrixfilter);
matrix matrix = new matrix();
matrix.prescale(-1,1);
matrix.posttranslate(foreimage.getwidth() + touchpt.x,0);
canvas.save();
canvas.cliprect(backarea);
canvas.drawbitmap(foreimage, matrix, fbpaint);
canvas.restore();
//对折处画左侧阴影
shadowdrawablerl.setbounds((int)halfcut - 50, 0 ,(int)halfcut, screenheight);
shadowdrawablerl.draw(canvas);
path bgpath = new path();
//可以显示背景图的区域
bgpath.addrect(new rectf(halfcut,0,screenwidth,screenheight), direction.cw);
//对折出右侧画背景
drawbgimage(canvas,bgpath);
//对折处画右侧阴影
shadowdrawablelr.setbounds((int)halfcut, 0 ,(int)halfcut + 50, screenheight);
shadowdrawablelr.draw(canvas);
}
}
@override
public boolean ontouchevent(motionevent event) {
// todo auto-generated method stub
if (event.getaction() == motionevent.action_down) {
touchpt.x = event.getx();
touchpt.y = event.gety();
}
else if(event.getaction() == motionevent.action_move){
lasttouchx = (int)touchpt.x;
touchpt.x = event.getx();
touchpt.y = event.gety();
postinvalidate();
}
else if(event.getaction() == motionevent.action_up){
int dx,dy;
dy = 0;
//向右滑动
if (lasttouchx<touchpt.x) {
dx = foreimage.getwidth() - (int)touchpt.x + 30;
}
else{
//向左滑动
dx = -(int)touchpt.x - foreimage.getwidth();
}
mscroller.startscroll((int)touchpt.x,(int)touchpt.y,dx,dy,1000);
postinvalidate();
}
//必须为true,否则无法获取action_move及action_up事件
return true;
}
}
public class pageactivity extends activity {
protected void oncreate(android.os.bundle savedinstancestate) {
requestwindowfeature(window.feature_no_title);
pagewidget pagewidget = new pagewidget(this);
display display = getwindowmanager().getdefaultdisplay();
int width = display.getwidth();
int height = display.getheight();
pagewidget.setscreen(width, height);
bitmap bm1 = bitmapfactory.decoderesource(getresources(), r.drawable.pre7);
bitmap bm2 = bitmapfactory.decoderesource(getresources(), r.drawable.after7);
bitmap foreimage = bitmap.createscaledbitmap(bm1, width, height,false);
bitmap bgimage = bitmap.createscaledbitmap(bm2, width, height,false);
pagewidget.setbgimage(bgimage);
pagewidget.setforeimage(foreimage);
setcontentview(pagewidget);
super.oncreate(savedinstancestate);
};
}
上一篇: Photoshop 抽象的海螺效果
下一篇: Photoshop 可爱的闹钟图标
推荐阅读
-
android中图片翻页效果简单的实现方法
-
Android自定义View设定到FrameLayout布局中实现多组件显示的方法 分享
-
android中实现在ImageView上随意画线涂鸦的方法
-
Android 实现图片生成卷角和圆角缩略图的方法
-
Android编程实现给Button添加图片和文字的方法
-
pyqt 实现在Widgets中显示图片和文字的方法
-
vue2.0 使用element-ui里的upload组件实现图片预览效果方法
-
WinForm中实现picturebox自适应图片大小的方法
-
android中知道图片name时获取图片的简单方法
-
Android中Market的Loading效果实现方法