简介
本篇是对实现图片处理功能:【鬼怪贴图】实现的记录。
实现原理
本功能实现比较简单,主要就是使用Android的Canvas模块。根据最底下鬼怪贴图菜单,来选择对应贴图,接着touch函数根据移动操作,利用Canvas 来更新鬼怪贴图在原图像中的位置。
具体代码
public class ButtonListener implements OnClickListener, OnTouchListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ghostVariable.curGhostBtnNumber = arg0.getId() + ghostVariable.ghostOffsetNumber;
ghostVariable.ghostNowPic = ((BitmapDrawable) getResources().getDrawable(
ghostVariable.ghostPicture[ghostVariable.curGhostBtnNumber])).getBitmap();
ghostVariable.ghostFunction.saveMyBitmap(ghostVariable.tmpMatName, ghostVariable.ghostBitmapBackground);
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_MOVE ){
ghostVariable.ghostBitmapBackground = ghostVariable.ghostFunction.getLoacalBitmap(ghostVariable.tmpMatName)
.copy(Bitmap.Config.ARGB_8888, true);
ghostVariable.myGhostcanvas.setBitmap(ghostVariable.ghostBitmapBackground);
ghostVariable.myGhostcanvas.drawBitmap(ghostVariable.ghostNowPic,
event.getX() - ghostVariable.ghostWidthStart) * ghostVariable.ghostPicWidthScale -
(ghostVariable.ghostNowPic.getWidth() / 2), event.getY() * ghostVariable.ghostPicHeightScale -
(ghostVariable.ghostNowPic.getHeight() / 2), null);
ghostVariable.mypicGhostImageView_1.setImageBitmap(ghostVariable.ghostBitmapBackground);
}
return true;
}
public class ButtonListener implements OnClickListener, OnTouchListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ghostVariable.curGhostBtnNumber = arg0.getId() + ghostVariable.ghostOffsetNumber;
ghostVariable.ghostNowPic = ((BitmapDrawable) getResources().getDrawable(
ghostVariable.ghostPicture[ghostVariable.curGhostBtnNumber])).getBitmap();
ghostVariable.ghostFunction.saveMyBitmap(ghostVariable.tmpMatName, ghostVariable.ghostBitmapBackground);
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_MOVE ){
ghostVariable.ghostBitmapBackground = ghostVariable.ghostFunction.getLoacalBitmap(ghostVariable.tmpMatName)
.copy(Bitmap.Config.ARGB_8888, true);
ghostVariable.myGhostcanvas.setBitmap(ghostVariable.ghostBitmapBackground);
ghostVariable.myGhostcanvas.drawBitmap(ghostVariable.ghostNowPic,
event.getX() - ghostVariable.ghostWidthStart) * ghostVariable.ghostPicWidthScale -
(ghostVariable.ghostNowPic.getWidth() / 2), event.getY() * ghostVariable.ghostPicHeightScale -
(ghostVariable.ghostNowPic.getHeight() / 2), null);
ghostVariable.mypicGhostImageView_1.setImageBitmap(ghostVariable.ghostBitmapBackground);
}
return true;
}
在鬼怪贴图菜单栏中,点击对应选项之后,onClick将根据ghostVariable.curGhostBtnNumber,来更新当前贴图操作会被使用的鬼怪图片到ghostVariable.ghostNowPic中。用户在图片上滑动的时候,将触发onTouch函数,该函数根据滑动坐标位置,利用myGhostcanvas来实现贴图操作,并通过更新mypicGhostImageView_1,将结果实时显示出来。
效果演示
对应的效果图片如下:
贴图效果
具体演示下载:http://download.youkuaiyun.com/detail/u011630458/9261617