android烟花动画效果图,Android实现烟花效果

本文介绍了在Android中实现烟花动画效果的详细步骤,包括创建SurfaceView、设置绘图基础类DrawTopBase以及继承该类的DrawYH类,通过在DrawYH类中绘制烟花并处理触摸事件来实现动画效果。主要涉及SurfaceView、Canvas和触摸事件的处理。
摘要由CSDN通过智能技术生成

HolderSurfaceView.java

package com.lyc;

import android.graphics.PixelFormat;

import android.view.SurfaceView;

public class HolderSurfaceView {

private HolderSurfaceView() {

}

private SurfaceView mSurfaceView;

private static HolderSurfaceView mHolderSurfaceView = null;

public static HolderSurfaceView getInstance() {

if (mHolderSurfaceView == null)

mHolderSurfaceView = new HolderSurfaceView();

return mHolderSurfaceView;

}

public void setSurfaceView(SurfaceView view) {

mSurfaceView = view;

mSurfaceView.setZOrderOnTop(true);

mSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);

}

public SurfaceView getSurfaceView() {

return mSurfaceView;

}

}

DrawTopBase.java

package com.lyc;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.PorterDuff.Mode;

import android.graphics.PorterDuffXfermode;

import android.graphics.Rect;

import android.view.MotionEvent;

import android.view.SurfaceHolder;

import android.view.SurfaceHolder.Callback;

import android.view.SurfaceView;

import android.view.View;

import android.view.View.OnTouchListener;

public class DrawTopBase implements Runnable, Callback ,OnTouchListener{

protected DrawStatus mStatus = DrawStatus.NoWork;

protected SurfaceView mSurfaceView;

protected SurfaceHolder mSurfaceHolder;

protected Context mContext;

protected Rect mSurfaceRect = new Rect(0, 0, 0, 0); //前两个参数是左上角的坐标,后两个参数是右下角的坐标(不是宽度和高度)

public DrawTopBase() {

setSurfaceView(HolderSurfaceView.getInstance().getSurfaceView());

}

public void setSurfaceView(SurfaceView view) {

mSurfaceView = view;

mContext = mSurfaceView.getContext();

mSurfaceHolder = mSurfaceView.getHolder();

mSurfaceHolder.addCallback(this);

// mSurfaceRect.set(new Rect(0, 0, mSurfaceView.getWidth(), mSurfaceView.getHeight()));

mSurfaceRect.set(new Rect(100, 100, 250, 250));

set();

}

public void set() {

setRect(mSurfaceRect);

}

protected Thread mThread = null;

public void begin() {

if (mThread == null) {

mThread = new Thread(this);

mThread.start();

}

}

public void end() {

mStatus = DrawStatus.Ending;

}

protected void doWork(Canvas canvas) {

}

protected void endWork() {

}

protected Paint mPaint = new Paint();

protected void clear(Canvas canvas) {

mPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));

canvas.drawPaint(mPaint);

mPaint.setXfermode(new PorterDuffXfermode(Mode.SRC));

}

protected void clear() {

synchronized (mSurfaceHolder) {

Canvas canvas = this.mSurfaceHolder.lockCanvas();

try {

clear(canvas);

} catch (Exception e) {

e.printStackTrace();

} finally {

if (canvas != null)

mSurfaceHolder.unlockCanvasAndP

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值