Android之十三刮刮卡中奖功能


Android之十三刮刮卡中奖功能_第1张图片
package com.zhy.view;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.Paint.Style;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.graphics.Rect;import android.graphics.RectF;import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.View;import com.example.zhy_path.R;public class GuaGuaKa extends View{/** * 绘制线条的Paint,即用户手指绘制Path */private Paint mOutterPaint = new Paint();/** * 记录用户绘制的Path */private Path mPath = new Path();/** * 内存中创建的Canvas */private Canvas mCanvas;/** * mCanvas绘制内容在其上 */private Bitmap mBitmap;/** * ------------------------以下是奖区的一些变量 */// private Bitmap mBackBitmap;private boolean isComplete;private Paint mBackPint = new Paint();private Rect mTextBound = new Rect();private String mText = "¥500";private int mLastX;private int mLastY;public GuaGuaKa(Context context){this(context, null);}public GuaGuaKa(Context context, AttributeSet attrs){this(context, attrs, 0);}public GuaGuaKa(Context context, AttributeSet attrs, int defStyle){super(context, attrs, defStyle);init();}private void init(){mPath = new Path();// mBackBitmap = BitmapFactory.decodeResource(getResources(),// R.drawable.t2);setUpOutPaint();setUpBackPaint();}/** * 初始化canvas的绘制用的画笔 */private void setUpBackPaint(){mBackPint.setStyle(Style.FILL);mBackPint.setTextScaleX(2f);mBackPint.setColor(Color.RED);mBackPint.setTextSize(32);mBackPint.getTextBounds(mText, 0, mText.length(), mTextBound);}@Overrideprotected void onDraw(Canvas canvas){// canvas.drawBitmap(mBackBitmap, 0, 0, null);// 绘制奖项canvas.drawText(mText, getWidth() / 2 - mTextBound.width() / 2,getHeight() / 2 + mTextBound.height() / 2, mBackPint);if (!isComplete){drawPath();canvas.drawBitmap(mBitmap, 0, 0, null);}}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){super.onMeasure(widthMeasureSpec, heightMeasureSpec);int width = getMeasuredWidth();int height = getMeasuredHeight();// 初始化bitmapmBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);mCanvas = new Canvas(mBitmap);// 绘制遮盖层// mCanvas.drawColor(Color.parseColor("#c0c0c0"));mOutterPaint.setStyle(Paint.Style.FILL);mCanvas.drawRoundRect(new RectF(0, 0, width, height), 30, 30,mOutterPaint);mCanvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.s_title), null, new RectF(0, 0, width, height), null);}/** * 设置画笔的一些参数 */private void setUpOutPaint(){// 设置画笔// mOutterPaint.setAlpha(0);mOutterPaint.setColor(Color.parseColor("#c0c0c0"));mOutterPaint.setAntiAlias(true);mOutterPaint.setDither(true);mOutterPaint.setStyle(Paint.Style.STROKE);mOutterPaint.setStrokeJoin(Paint.Join.ROUND); // 圆角mOutterPaint.setStrokeCap(Paint.Cap.ROUND); // 圆角// 设置画笔宽度mOutterPaint.setStrokeWidth(20);}/** * 绘制线条 */private void drawPath(){mOutterPaint.setStyle(Paint.Style.STROKE);mOutterPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));mCanvas.drawPath(mPath, mOutterPaint);}@Overridepublic boolean onTouchEvent(MotionEvent event){int action = event.getAction();int x = (int) event.getX();int y = (int) event.getY();switch (action){case MotionEvent.ACTION_DOWN:mLastX = x;mLastY = y;mPath.moveTo(mLastX, mLastY);break;case MotionEvent.ACTION_MOVE:int dx = Math.abs(x - mLastX);int dy = Math.abs(y - mLastY);if (dx > 3 || dy > 3)mPath.lineTo(x, y);mLastX = x;mLastY = y;break;case MotionEvent.ACTION_UP:new Thread(mRunnable).start();break;}invalidate();return true;}/** * 统计擦除区域任务 */private Runnable mRunnable = new Runnable(){private int[] mPixels;public void run(){int w = getWidth();int h = getHeight();float wipeArea = 0;float totalArea = w * h;Bitmap bitmap = mBitmap;mPixels = new int[w * h];/** * 拿到所有的像素信息 */bitmap.getPixels(mPixels, 0, w, 0, 0, w, h);/** * 遍历统计擦除的区域 */for (int i = 0; i < w; i++){for (int j = 0; j < h; j++){int index = i + j * w;if (mPixels[index] == 0){wipeArea++;}}}/** * 根据所占百分比,进行一些操作 */if (wipeArea > 0 && totalArea > 0){int percent = (int) (wipeArea * 100 / totalArea);Log.e("TAG", percent + "");if (percent > 70){Log.e("TAG", "清除区域达到70%,下面自动清除");isComplete = true;postInvalidate();}}}};}
MainActivity.java
package com.example.zhy_path;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}<span style="font-size:18px;color:#ff0000;">}</span>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <com.zhy.view.GuaGuaKa        android:layout_centerInParent="true"        android:layout_width="300dp"        android:layout_height="100dp" /></RelativeLayout>
Android之十三刮刮卡中奖功能_第2张图片


更多相关文章

  1. Android搜索自动提示功能 AutocompleteTextView
  2. Android设备功能之传感器教程篇
  3. Android表情功能
  4. Android 时间同步功能 NTP NITZ
  5. Android 图表开源框架之MPAndroidChart LineChart折线图(六)实现翻
  6. android 仿微信聊天界面,以及语音录制功能
  7. Android中通过NTP服务器获取时间功能源码分析

随机推荐

  1. 9.0 Ansible 图形界面-Tower
  2. JS获取指定字符串中出现次数最多的字符及
  3. vivo 应用商店中的断点续传技术剖析
  4. Konga面板安装
  5. 戴尔服务器安装centos,以及esxi虚拟化,给pe
  6. 如何处理Linux服务器内存过高?
  7. 获取input框输入值异常
  8. HTML选择器和JS基础
  9. 一篇文章带你快速入门JavaScript(实操代码
  10. 10 个最佳实践来改良你的 CSS