Android 碎屏效果 (Crack Screen,擊碎螢幕)

來源:互聯網
上載者:User

標籤: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,擊碎螢幕)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.