最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
SurfaceView实现红包雨平移动画
时间:2022-06-25 22:54:54 编辑:袖梨 来源:一聚教程网
使用SurfaceView实现简单的红包雨动画,供大家参考,具体内容如下
具体代码:
public class TranslateSurfaceView extends SurfaceView implements DrawInterface {
private DrawHandler drawHandler;
private int width;
private int height;
private Bitmap bitmap;
private int bitmapWidth;
private int bitmapHeight;
private Toast toast;
private int count = 0;
private ArrayList moveList = new ArrayList();
public TranslateSurfaceView(Context context) {
this(context, null);
}
public TranslateSurfaceView(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}
public TranslateSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
SurfaceHolder holder = getHolder();
setZOrderOnTop(true);
holder.setFormat(PixelFormat.TRANSLUCENT);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getMeasuredWidth();
height = getMeasuredHeight();
}
public void prepare() {
DrawThread drawThread = new DrawThread();
drawThread.start();
drawHandler = new DrawHandler(drawThread.getLooper(), this);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_redenvelope2);
bitmapWidth = bitmap.getWidth();
bitmapHeight = bitmap.getHeight();
}
public void addMoveModel(MoveModel moveModel) {
moveList.add(moveModel);
}
public void start() {
count = 0;
moveList.clear();
for (int i = 0; i width || moveModel.y > height) {
resetMoveModel(moveModel);
} else {
moveModel.y += moveModel.randomY;
}
}
holder.unlockCanvasAndPost(canvas);
drawHandler.sendEmptyMessage(DrawHandler.START_DRAW_KEY);
}
@Override
public void stopDraw() {
drawHandler.removeMessages(DrawHandler.START_DRAW_KEY);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getActionMasked();
switch (action) {
case MotionEvent.ACTION_DOWN:
checkInRect((int) event.getX(), (int) event.getY());
break;
}
return true;
}
/**
* 是否点击在红包区域
* @param x
* @param y
*/
private void checkInRect(int x, int y) {
int length = moveList.size();
for (int i = 0; i
相关文章
- 播种委员会公园解锁方法 公园怎么解锁 11-29
- pcl2启动器怎么加模组 模组添加方法汇总 11-29
- dnf神界版本影舞者装备搭配毕业攻略 影舞者装备怎么选 11-29
- dnf神界版本死灵术士装备搭配毕业攻略 死灵术士装备怎么选 11-29
- 洛克王国世界伏地兽进化路线是什么 洛克王国世界伏地兽进化路线攻略 11-29
- dnf神界版本刺客装备搭配毕业攻略 刺客装备怎么选 11-29
