Android刮刮卡功能具體實(shí)現(xiàn)代碼
今天整理之前的代碼,忽然看到之前自己寫的一個(gè)刮刮卡,整理下以便以后使用,同時(shí)分享給需要的朋友,如有錯(cuò)誤,還請(qǐng)多多指正。
實(shí)現(xiàn)的步驟,其實(shí)就是徒手畫三個(gè)圖層疊加在一起,最上層是繪制需要的問(wèn)題,就是以上所述的“騷年,刮我吧”,第二層就是覆蓋寬高的灰層,第三層是結(jié)果層,多的不啰嗦了,具體實(shí)現(xiàn)如下,附上詳細(xì)注釋。
/** * * created by zero on 2016-9-9 * * 刮刮卡 * */ public class ScratchView extends View { public ScratchView(Context context) { super(context); init(); } private Canvas mCanvas = null; private Path mPath = null; private Paint mPaint = null; // 定義畫布的寬和高 private int screenWidth = 720; private int screenHeight = 360; private Bitmap bitmap = null; private void init() { // TODO Auto-generated method stub mPath = new Path(); bitmap = Bitmap.createBitmap(screenWidth, screenHeight, Config.ARGB_8888); // 對(duì)mPaint的設(shè)置 mPaint = new Paint(); mPaint.setFlags(Paint.ANTI_ALIAS_FLAG); mPaint.setAntiAlias(true); mCanvas = new Canvas(); mPaint.setDither(true); // 設(shè)置畫筆為空心 mPaint.setStyle(Style.STROKE); // 設(shè)置線寬,即每次擦除的寬度 mPaint.setStrokeWidth(10); mPaint.setStrokeCap(Cap.ROUND); mPaint.setStrokeJoin(Join.ROUND); // 設(shè)置圖形重疊時(shí)的處理方式,一共有16種方式,有興趣可自己查閱 mPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); mPaint.setAlpha(0); mCanvas = new Canvas(bitmap); mCanvas.drawColor(Color.parseColor("#c0c0c0")); setBitmapText(); } private void setBitmapText() { Paint paint = new Paint(); paint.setTextSize(40); paint.setColor(Color.parseColor("#9f9fa0")); paint.setFlags(Paint.ANTI_ALIAS_FLAG); paint.setAntiAlias(true); paint.setTextAlign(Paint.Align.CENTER); paint.setFakeBoldText(true); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.alpha(0)); canvas.rotate(-20); // 遍歷繪制文字 for (int i = 0; i < screenWidth + 200; i += 300) { for (int j = 0; j < screenHeight + 200; j += 60) { canvas.drawText("刮我吧,騷年!", i, j, paint); } } setScratchBackground("一等獎(jiǎng)"); } // 接收后臺(tái)傳來(lái)的文字,即中獎(jiǎng)或者未中獎(jiǎng)的文字 public void setScratchBackground(String txt_win) { // TODO Auto-generated method stub Paint paint = new Paint(); Bitmap bitmap = Bitmap.createBitmap(screenWidth, screenHeight, Config.ARGB_8888); paint.setTextSize(40); paint.setColor(Color.BLACK); paint.setFlags(Paint.ANTI_ALIAS_FLAG); paint.setAntiAlias(true); paint.setTextAlign(Paint.Align.CENTER); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.alpha(0)); canvas.drawText(txt_win, screenWidth / 2, 60, paint); setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap)); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); mCanvas.drawPath(mPath, mPaint); canvas.drawBitmap(bitmap, 0, 0, null); } int x = 0; int y = 0; @SuppressLint("ClickableViewAccessibility") @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); int currX = (int) event.getX(); int currY = (int) event.getY(); switch (action) { case MotionEvent.ACTION_DOWN: { mPath.reset(); x = currX; y = currY; mPath.moveTo(x, y); } break; case MotionEvent.ACTION_MOVE: { mPath.quadTo(x, y, currX, currY); x = currX; y = currY; postInvalidate(); } break; case MotionEvent.ACTION_UP: { new Thread(mRunnable).start(); } case MotionEvent.ACTION_CANCEL: { mPath.reset(); } break; } return true; } private Runnable mRunnable = new Runnable() { private int[] mPixels; @Override public void run() { float wipeArea = 0; float totalArea = screenWidth * screenHeight; Bitmap mBitmap = bitmap; mPixels = new int[screenWidth * screenHeight]; /** * 拿到所有的像素信息 */ mBitmap.getPixels(mPixels, 0, screenWidth, 0, 0, screenWidth, screenHeight); /** * 遍歷統(tǒng)計(jì)擦除的區(qū)域 */ for (int i = 0; i < screenWidth; i++) { for (int j = 0; j < screenHeight; j++) { int index = i + j * screenWidth; if (mPixels[index] == 0) { wipeArea++; } } } /** * 根據(jù)所占百分比,進(jìn)行一些操作 */ if (wipeArea > 0 && totalArea > 0) { int percent = (int) (wipeArea * 100 / totalArea); /** * 設(shè)置達(dá)到多少百分比的時(shí)候,彈窗提醒是否中獎(jiǎng)此處設(shè)置為20 */ if (percent > 20) { /** * 刮開獎(jiǎng)以后的操作,此處在子線程toast,可能會(huì)發(fā)生線程阻塞,只為測(cè)試使用 */ Looper.prepare(); Toast.makeText(getContext(), "已刮開" + percent + "%", Toast.LENGTH_LONG).show(); Looper.loop(); } } } }; }
發(fā)的是公司需要的效果,以上代碼只是一個(gè)實(shí)現(xiàn),各種樣式還需要自己去實(shí)現(xiàn)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android?PowerManagerService?打開省電模式
這篇文章主要介紹了Android?PowerManagerService打開省電模式,文章通告省電模式的打開過(guò)程、什么是?battery?saver?sticky?模式兩部分展開詳情,感興趣的朋友可以參考一下2022-08-08Android開發(fā)之自定義view實(shí)現(xiàn)通訊錄列表A~Z字母提示效果【附demo源碼下載】
這篇文章主要介紹了Android開發(fā)之自定義view實(shí)現(xiàn)通訊錄列表A~Z字母提示效果,結(jié)合完整實(shí)例形式分析了Android獲取通訊錄列表及采用自定義view排列顯示的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07Android5.1 取消錄制屏幕跳出的權(quán)限對(duì)話框問(wèn)題
這篇文章主要介紹了Android5.1 取消錄制屏幕跳出的權(quán)限對(duì)話框問(wèn)題,需要的朋友可以參考下2017-04-04Android實(shí)現(xiàn)購(gòu)物車添加商品特效
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)購(gòu)物車添加商品特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06簡(jiǎn)單說(shuō)說(shuō)Android中如何使用攝像頭和相冊(cè)
本篇文章主要介紹了簡(jiǎn)單說(shuō)說(shuō)Android中如何使用攝像頭和相冊(cè),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05android實(shí)現(xiàn)session保持簡(jiǎn)要概述及實(shí)現(xiàn)
其實(shí)sesion在瀏覽器和web服務(wù)器直接是通過(guò)一個(gè)叫做name為sessionid的cookie來(lái)傳遞的,所以只要在每次數(shù)據(jù)請(qǐng)求時(shí)保持sessionid是同一個(gè)不變就可以用到web的session了,感興趣的你可以參考下本文或許對(duì)你有所幫助2013-03-03