標籤:android碎玻璃 android擊碎螢幕 android碎螢幕 android碎屏效果
本文介紹一個好玩的App
實現思路:在一個透明的Activity上用一個自訂View,然後在View上畫.9的碎玻璃圖片,加上音效。然後過一段時間消失。
主要用一個postInvalidate();方法,用一個集合去裝手指觸摸過的地方,在touch事件中調用postInvalidate();方法進行視圖重新繪製。
需要注意的是,這裡的播放音效和上篇部落格 Android 閃電效果 (Electric Screen,電動螢幕) 中的播放音效一樣,暫停音效的時候要傳開始播放時得到的int返回值,和停止音效不一樣。
可以通過限制集合長度來限制繪製的螢幕個數。
單獨開啟一個線程,過一段時間開始清除效果, 即直接操作集合,操作完了調用postInvalidate(); 重新繪圖就可以了。
範例程式碼:
public class CustomView extends View {private Paint mPaint;private SoundPool mSoundPool;private Map<Integer, Integer> mSoundMap = new HashMap<Integer, Integer>();private int mIndex;private Bitmap mBitmap;private ArrayList<Float> mXPointList;private ArrayList<Float> mYPointList;private int mCount = 0;// 點擊次數private int mLength = 30;// 繪製總數public CustomView(Context context, AttributeSet attrs) {super(context);mPaint = new Paint();mPaint.setAntiAlias(true);mPaint.setColor(Color.BLUE);// mPaint.setAlpha(127);mPaint.setStrokeWidth(2.0f);this.setKeepScreenOn(true);this.setFocusable(true);this.setLongClickable(true);this.mSoundPool = new SoundPool(5, AudioManager.STREAM_SYSTEM, 5);this.mSoundMap.put(1, mSoundPool.load(context, R.raw.cfokwowbfv, 1));this.mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.screen);mXPointList = new ArrayList<Float>();mYPointList = new ArrayList<Float>();new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubwhile (true) {try {Thread.sleep(4000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}// mG++;handler.sendEmptyMessage(0);}}}).start();}@Overridepublic boolean onTouchEvent(MotionEvent arg1) {// TODO Auto-generated method stubswitch (arg1.getAction()) {case MotionEvent.ACTION_DOWN:// drawBitmap(arg1.getX(), arg1.getY());playSound();mXPointList.add(arg1.getX());mYPointList.add(arg1.getY());postInvalidate();mCount++;if (mCount > mLength) {mXPointList.remove(0);mYPointList.remove(0);mLength++;}break;case MotionEvent.ACTION_UP:break;case MotionEvent.ACTION_MOVE:break;default:break;}return super.onTouchEvent(arg1);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);for (int i = 0; i < mXPointList.size(); ++i) {canvas.drawBitmap(mBitmap, mXPointList.get(i) - mBitmap.getWidth()/ 2, mYPointList.get(i) - mBitmap.getHeight() / 2, null);}}// 播放public void playSound() {mIndex = mSoundPool.play(mSoundMap.get(1), 1, 1, 0, 0, 1);}// 停止播放public void stopSound() {// Toast.makeText(getContext(), "zzzzz", 0).show();mSoundPool.stop(mIndex);}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// TODO Auto-generated method stubToast.makeText(getContext(), "keydown", Toast.LENGTH_SHORT).show();return super.onKeyDown(keyCode, event);}// 更新介面Handler handler = new Handler() {public void handleMessage(Message msg) {if (msg.what == 0) {if (mCount != 0 && mXPointList.size() != 0) {for (int i = 0; i < new Random().nextInt(mXPointList.size() + 1); i++) {mXPointList.remove(0);mYPointList.remove(0);mLength++;}}postInvalidate();}}};}
Github: https://github.com/OneHead/crack_screen2D
Weibo: http://weibo.com/2382477985
Android 碎屏效果 (Crack Screen,擊碎螢幕)