您好,登錄后才能下訂單哦!
使用SurfaceView怎么實現紅包雨平移動畫?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
SurfaceViewDemo
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<MoveModel> 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 < 10; i++) { generateModel(); } drawHandler.sendEmptyMessage(DrawHandler.START_DRAW_KEY); } public void resume() { drawHandler.sendEmptyMessage(DrawHandler.START_DRAW_KEY); } public void pause() { drawHandler.sendEmptyMessage(DrawHandler.STOP_DRAW_KEY); } public void quit() { if (null != bitmap) { bitmap.recycle(); } if (null != drawHandler) { drawHandler.removeCallbacksAndMessages(null); drawHandler.getLooper().quit(); } } @Override public void startDraw() { SurfaceHolder holder = getHolder(); Canvas canvas = holder.lockCanvas(); if (null == canvas) { return; } Paint paint = new Paint(); paint.setAntiAlias(true); canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); for (MoveModel moveModel : moveList) { canvas.drawBitmap(bitmap, moveModel.x, moveModel.y, paint); if (moveModel.x > 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 < length; i++) { MoveModel moveModel = moveList.get(i); Rect rect = new Rect((int) moveModel.x, (int) moveModel.y, (int) moveModel.x + bitmapWidth, (int) moveModel.y + bitmapHeight); if (rect.contains(x, y)) { count++; resetMoveModel(moveModel); if (toast == null) { toast = Toast.makeText(getContext(), "", Toast.LENGTH_SHORT); } toast.setText("搶到紅包" + count); toast.show(); break; } } } private void resetMoveModel(MoveModel moveModel) { Random random = new Random(); moveModel.x = random.nextInt(11) * (width / 10); moveModel.y = 0; moveModel.randomY = (random.nextInt(5) + 2) * getResources().getDisplayMetrics().density * 1.4f; } private void generateModel() { Random random = new Random(); MoveModel moveModel = new MoveModel(); moveModel.moveId = moveList.size() + 1; moveModel.x = random.nextInt(11) * (width / 10); moveModel.y = 0; moveModel.randomY = (random.nextInt(5) + 2) * getResources().getDisplayMetrics().density * 1.4f; moveList.add(moveModel); } }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。